package main
import (
"context"
"crypto/tls"
"encoding/json"
"etcdkeeper/session"
_ "etcdkeeper/session/providers/memory"
"flag"
"fmt"
"github.com/coreos/etcd/client"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/transport"
"io"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
)
var (
sep = flag.String("sep", "/", "separator")
separator = ""
usetls = flag.Bool("usetls", false, "use tls")
cacert = flag.String("cacert", "", "verify certificates of TLS-enabled secure servers using this CA bundle")
cert = flag.String("cert", "", "identify secure client using this TLS certificate file")
keyfile = flag.String("key", "", "identify secure client using this TLS key file")
useAuth = flag.Bool("auth", false, "use auth")
rootUsers = make(map[string]*userInfo) // host:rootUser
rootUesrsV2 = make(map[string]*userInfo) // host:rootUser
sessmgr *session.Manager
mu sync.Mutex
)
type userInfo struct {
host string
uname string
passwd string
}
func main() {
host := flag.String("h","0.0.0.0","host name or ip address")
port := flag.Int("p", 8080, "port")
flag.CommandLine.Parse(os.Args[1:])
separator = *sep
middleware := func(fns ...func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
for _, fn := range fns {
fn(w, r)
}
}
}
// v2
//http.HandleFunc(*name, v2request)
http.HandleFunc("/v2/separator", middleware(nothing, getSeparator))
http.HandleFunc("/v2/connect", middleware(nothing, connectV2))
http.HandleFunc("/v2/put", middleware(nothing, putV2))
http.HandleFunc("/v2/get", middleware(nothing, getV2))
http.HandleFunc("/v2/delete", middleware(nothing, delV2))
// dirctory mode
http.HandleFunc("/v2/getpath", middleware(nothing, getPathV2))
// v3
http.HandleFunc("/v3/separator", middleware(nothing, getSeparator))
http.HandleFunc("/v3/connect", middleware(nothing, connect))
http.HandleFunc("/v3/put", middleware(nothing, put))
http.HandleFunc("/v3/get", middleware(nothing, get))
http.HandleFunc("/v3/delete", middleware(nothing, del))
// dirctory mode
http.HandleFunc("/v3/getpath", middleware(nothing, getPath))
wd, err := os.Executable()
if err != nil {
log.Fatal(err)
}
rootPath := filepath.Dir(wd)
// Session management
sessmgr, err = session.NewManager("memory", "_etcdkeeper_session", 86400)
if err != nil {
log.Fatal(err)
}
time.AfterFunc(86400*time.Second, func() {
sessmgr.GC()
})
//log.Println(http.Dir(rootPath + "/assets"))
http.Handle("/", http.FileServer(http.Dir(rootPath + "/assets"))) // view static directory
log.Printf("listening on %s:%d\n", *host, *port)
err = http.ListenAndServe(*host + ":" + strconv.Itoa(*port), nil)
if err != nil {
log.Fatal(err)
}
}
func nothing(_ http.ResponseWriter, _ *http.Request) {
// Nothing
}
//func v2request(w http.ResponseWriter, r *http.Request){
// if err := r.ParseForm(); err != nil {
// log.Println(err.Error())
// }
// log.Println(r.Method, "v2", r.FormValue("url"), r.PostForm.Encode())
//
// body := strings.NewReader(r.PostForm.Encode())
// req, err := http.NewRequest(r.Method, r.Form.Get("url"), body)
// if err != nil {
// io.WriteString(w, err.Error())
// return
// }
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// client := &http.Client{Timeout: 10*time.Second} // important!!!
// resp, err := client.Do(req)
// if err != nil {
// io.WriteString(w, err.Error())
// }else {
// result, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// io.WriteString(w, "Get data failed: " + err.Error())
// } else {
// io.WriteString(w, string(result))
// }
// }
//}
// v2 api
func connectV2(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
sess := sessmgr.SessionStart(w, r)
host := strings.TrimSpace(r.FormValue("host"))
uname := r.FormValue("uname")
passwd := r.FormValue("passwd")
if !strings.HasPrefix(host, "http") {
host = "http://" + host
}
if *useAuth {
_, ok := rootUesrsV2[host]
if !ok && uname != "root" {
b, _ := json.Marshal(map[string]interface{}{"status":"root"})
io.WriteString(w, string(b))
return
}
if uname == "" || passwd == "" {
b, _ := json.Marshal(map[string]interface{}{"status":"login"})
io.WriteString(w, string(b))
return
}
}
if uinfo, ok := sess.Get("uinfov2").(*userInfo); ok {
if host == uinfo.host && uname == uinfo.uname && passwd == uinfo.passwd {
info := getInfoV2(host)
b, _ := json.Marshal(map[string]interface{}{"status":"running", "info":info})
io.WriteString(w, string(b))
return
}
}
uinfo := &userInfo{host:host, uname:uname, passwd:passwd}
_, err := newClientV2(uinfo)
if err != nil {
log.Println(r.Method, "v2", "connect fail.")
b, _ := json.Marshal(map[string]interface{}{"status":"error", "message":err.Error()})
io.WriteString(w, string(b))
return
}
_ = sess.Set("uinfov2", uinfo)
if *useAuth {
if uname == "root" {
rootUesrsV2[host] = uinfo
}
} else {
rootUesrsV2[host] = uinfo
}
log.Println(r.Method, "v2", "connect success.")
info := getInfoV2(host)
b, _ := json.Marshal(map[string]interface{}{"status":"running", "info":info})
io.WriteString(w, string(b))
}
func putV2(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
value := r.FormValue("value")
ttl := r.FormValue("ttl")
dir := r.FormValue("dir")
log.Println("PUT", "v2", key)
kapi := client.NewKeysAPI(getClientV2(w, r))
var isDir bool
if dir != "" {
isDir, _ = strconv.ParseBool(dir)
}
var err error
data := make(map[string]interface{})
if ttl != "" {
var sec int64
sec, err = strconv.ParseInt(ttl, 10, 64)
if err != nil {
log.Println(err.Error())
}
_, err = kapi.Set(context.Background(), key, value, &client.SetOptions{TTL:time.Duration(sec)*time.Second, Dir:isDir})
} else {
_, err = kapi.Set(context.Background(), key, value, &client.SetOptions{Dir:isDir})
}
if err != nil {
data["errorCode"] = 500
data["message"] = err.Error()
} else {
if resp, err := kapi.Get(context.Background(), key, &client.GetOptions{Recursive:true, Sort:true}); err != nil {
data["errorCode"] = err.Error()
} else {
if resp.Node != nil {
node := make(map[string]interface{})
node["key"] = resp.Node.Key
node["value"] = resp.Node.Value
node["dir"] = resp.Node.Dir
node["ttl"] = resp.Node.TTL
node["createdIndex"] = resp.Node.CreatedIndex
node["modifiedIndex"] = resp.Node.ModifiedIndex
data["node"] = node
}
}
}
var dataByte []byte
if dataByte, err = json.Marshal(data);err != nil {
io.WriteString(w, err.Error())
} else {
io.WriteString(w, string(dataByte))
}
}
func getV2(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
data := make(map[string]interface{})
log.Println("GET", "v2", key)
var cli client.Client
sess := sessmgr.SessionStart(w, r)
v := sess.Get("uinfov2")
var uinfo *userInfo
if v != nil {
uinfo = v.(*userInfo)
cli, _ = newClientV2(uinfo)
kapi := client.NewKeysAPI(cli)
var permissions [][]string
if r.FormValue("prefix") == "true" {
var e error
permissions, e = getPermissionPrefixV2(uinfo.host, uinfo.uname, key)
if e != nil {
io.WriteString(w, e.Error())
return
}
} else {
permissions = [][]string{{key, ""}}
}
var (
min, max int
)
if key == separator {
min = 1
} else {
min = len(strings.Split(key, separator))
}
max = min
all := make(map[int][]map[string]interface{})
if key == separator {
all[min] = []map[string]interface{}{{"key":key, "value":"", "dir":true, "nodes":make([]map[string]interface{}, 0)}}
}
for _, p := range permissions {
pKey, pRange := p[0], p[1]
var opt *client.GetOptions
if pRange != "" {
if pRange == "c" {
pKey += separator
}
opt = &client.GetOptions{Recursive:true, Sort:true}
}
if resp, err := kapi.Get(con
没有合适的资源?快使用搜索试试~ 我知道了~
管理系统系列--基于gin+vue+element搭建的商城管理系统,欢迎PR.zip
共1366个文件
js:547个
png:324个
vue:146个
需积分: 5 0 下载量 4 浏览量
2024-02-26
01:19:38
上传
评论
收藏 32.58MB ZIP 举报
温馨提示
管理系统系列--基于gin+vue+element搭建的商城管理系统,欢迎PR
资源推荐
资源详情
资源评论
收起资源包目录
管理系统系列--基于gin+vue+element搭建的商城管理系统,欢迎PR.zip (1366个子文件)
.browserslistrc 21B
my.cnf 275B
redis.conf 61KB
logstash.conf 509B
axure-chrome-extension.crx 31KB
index.css 414KB
easyui.css 57KB
easyui.css 57KB
easyui.css 54KB
styles.css 44KB
styles.css 44KB
styles.css 40KB
styles.css 30KB
jquery-ui-themes.css 28KB
styles.css 27KB
styles.css 27KB
styles.css 22KB
styles.css 21KB
styles.css 20KB
styles.css 19KB
github-markdown.css 16KB
styles.css 15KB
styles.css 15KB
styles.css 13KB
styles.css 12KB
styles.css 11KB
styles.css 10KB
styles.css 9KB
sitemap.css 8KB
styles.css 8KB
styles.css 7KB
axure_rp_page.css 5KB
default.css 4KB
page_notes.css 4KB
icon.css 3KB
debug.css 2KB
styles.css 2KB
recordplay.css 2KB
index.css 1KB
waves.css 825B
reset.css 479B
style.css 310B
.env.development 541B
Dockerfile 2KB
Dockerfile 792B
Dockerfile 458B
Dockerfile 400B
Dockerfile 324B
Dockerfile 322B
Dockerfile 304B
Dockerfile 239B
Dockerfile 239B
Dockerfile 239B
Dockerfile 220B
Dockerfile 219B
Dockerfile 96B
.editorconfig 244B
env-example 2KB
.eslintignore 34B
shoppingCart.gif 1.73MB
401.gif 160KB
allow_access.gif 9KB
extensions_menu.gif 9KB
axure_logo.gif 3KB
loading.gif 2KB
loading.gif 2KB
loading.gif 2KB
splitter.gif 1KB
plus.gif 834B
minus.gif 830B
newwindow.gif 112B
note.gif 98B
transparent.gif 43B
blank.gif 43B
blank.gif 43B
blank.gif 43B
blank.gif 43B
.gitattributes 66B
.gitattributes 27B
.gitignore 267B
.gitignore 213B
.gitignore 61B
.gitignore 20B
.gitignore 13B
.gitignore 6B
.gitignore 1B
main.go 27KB
redis.go 5KB
order.go 3KB
goods.go 3KB
menu.go 3KB
session.go 3KB
casbin.go 3KB
coupon.go 3KB
memory.go 2KB
main.go 2KB
jwt.go 2KB
redis_test.go 2KB
web.go 2KB
mw_recover.go 2KB
共 1366 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
Kwan的解忧杂货铺
- 粉丝: 2w+
- 资源: 3686
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功