-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmc.go
89 lines (72 loc) · 1.83 KB
/
smc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"flag"
"io"
"log"
. "mcache/action"
. "mcache/conf"
"net/http"
"runtime"
"time"
)
type myHandler struct{}
var mux map[string]func(w http.ResponseWriter, r *http.Request)
var allowedHosts map[string]int
//otsmid -c "F:\mygo\src\Middleware\1.conf"
var c *string = flag.String("c", "", "is configfile path")
func main() {
//初始化配置文件
flag.Parse()
configpath := *c
ParseConf(configpath)
runtime.GOMAXPROCS(runtime.NumCPU())
s := &http.Server{
Addr: GlobalConf.Host,
Handler: &myHandler{},
ReadTimeout: 200 * time.Second,
WriteTimeout: 200 * time.Second,
MaxHeaderBytes: 1 << 20,
}
mux = make(map[string]func(w http.ResponseWriter, r *http.Request))
mux["/add"] = ApiAdd
mux["/get"] = ApiGet
mux["/del"] = ApiDel
mux["/search"] = ApiSearch
mux["/getqs"] = ApiGetQs
mux["/getcid"] = ApiGetCid
mux["/saveact"] = ApiSaveAct
mux["/passact"] = ApiPassAct
mux["/getuserinfo"] = ApiGetUser
//返回所有用户数量
mux["/count"] = ApiCount
//删除属性
mux["/setattrs"] = ApiSetAttrs
mux["/delattrs"] = ApiDelAttrs
mux["/delattr"] = ApiDelAttr
//备份与测试
mux["/syncmongo"] = ApiSyncMongo
//备份数据到硬盘
mux["/push"] = ApiPush
//添加测试用户
mux["/testadds"] = ApiTestAdds
//从mongo备份与恢复
mux["/pushtomongo"] = ApiPushToMongo
mux["/mreduction"] = ApiMReduction
allowedHosts = GlobalConf.AllowedHosts
allowedHosts["127.0.0.1"] = 1
log.Fatal(s.ListenAndServe())
}
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if _, ok := allowedHosts[r.Host]; ok {
w.WriteHeader(403)
io.WriteString(w, "非法请求"+r.URL.String())
return
}
if h, ok := mux[r.URL.String()]; ok {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
h(w, r)
return
}
io.WriteString(w, r.URL.String())
return
}