-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseserver.go
115 lines (100 loc) · 3.03 KB
/
baseserver.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package service
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
routing "github.com/xybstone/fasthttp-routing"
"gitlab.gaodun.com/golib/filetool"
)
//BaseResponse 返回参数
type BaseResponse struct {
HTTPCode int `json:"http_code,omitempty"`
Status int `json:"status"`
Info string `json:"info"`
Result interface{} `json:"result"`
}
// BaseServer 服务基类
type BaseServer struct {
Logger func(title string, msg map[string]interface{})
}
// GetRouterMap 路由表
func (bs *BaseServer) GetRouterMap() map[string]Route {
AddRote("/", Route{[]string{"GET", "POST"}, bs.HandleRoot})
AddRote("/status", Route{[]string{"GET"}, bs.GetStatus})
AddRote("/cors", Route{[]string{"GET"}, bs.Cors})
return routes
}
// Cors 跨域
func (bs *BaseServer) Cors(ctx *routing.Context) (err error) {
ctx.Write([]byte(`{"http_code":200,"status":0,"info":"NOT_ALLOW_CORS","result":null}`))
return
}
//HandleRoot 测试页
func (bs *BaseServer) HandleRoot(ctx *routing.Context) (err error) {
v := fmt.Sprintf("{\"version\":\"%s\"}", ServiceVersion)
ctx.Write([]byte(v))
return
}
//GetStatus 状态页
func (bs *BaseServer) GetStatus(ctx *routing.Context) (err error) {
fmt.Println("1")
dir, _ := os.Getwd()
fileOperate := filetool.FileOperate{Filename: dir + string(os.PathSeparator) + "DEPLOY"}
if fileOperate.CheckFileExist(fileOperate.Filename) {
text, _ := fileOperate.ReadFile(fileOperate.Filename)
lineList := strings.Split(string(text), "\n")
begin := "{"
tmpStr := ""
for _, line := range lineList {
if len(line) == 0 {
break
}
n := strings.Split(line, "|")
jsonKey := "\"" + n[0] + "\""
jsonValue := ":\"" + n[1] + "\""
tmpStr += jsonKey + jsonValue + ","
}
mStr := strings.TrimRight(tmpStr, ",")
end := "}"
info := begin + mStr + end
ctx.Write([]byte("{\"status\": \"1\",\"data\":" + info + "}"))
return
}
ctx.Write([]byte("{\"status\": \"1\"}"))
return
}
var routes map[string]Route
// BindRote 注册绑定路由
func BindRote(path string, methods []string, handle func(*routing.Context) error) {
AddRote(path, Route{method: methods, handle: handle})
}
// AddRote 添加路由
func AddRote(path string, r Route) {
if routes == nil {
routes = make(map[string]Route)
}
routes[path] = r
}
//ServerJSON 服务器返回
func (bs *BaseServer) ServerJSON(ctx *routing.Context, v interface{}, status int) {
if b, err := json.Marshal(v); err == nil {
if status == 0 {
XLogger.Printf("ServerJSON:%v", map[string]interface{}{"Respose OK": string(b)})
} else {
XLogger.Printf("ServerJSON ERR:%v", map[string]interface{}{"Respose ERR": string(b), "PATH:": string(ctx.Path())})
}
ctx.SetContentType("application/json")
ctx.Write(b)
}
}
// FormString 获取字符串
func (bs *BaseServer) FormString(ctx *routing.Context, key string) string {
return strings.TrimSpace(string(ctx.FormValue(key)))
}
// FormInt64 获取int
func (bs *BaseServer) FormInt64(ctx *routing.Context, key string) int64 {
i, _ := strconv.ParseInt(string(ctx.FormValue(key)), 10, 64)
return i
}