/
main.go
147 lines (115 loc) · 2.99 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"net/http"
"strings"
"./models"
"./models/user"
"./modules/api"
"./modules/setting"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"github.com/weisd/cache"
_ "github.com/weisd/cache/redis"
"github.com/weisd/echo-statistics"
"github.com/weisd/jwt"
"github.com/weisd/log"
)
const (
VER = "0.1.0.0811"
)
func version(c *echo.Context) error {
return c.JSON(200, api.ResOk(VER))
}
func main() {
bootstraps()
e := echo.New()
if setting.Cfg.Debug {
log.Info("SetDebug on")
e.SetDebug(setting.Cfg.Debug)
}
// Middleware
e.Use(mw.Logger())
e.Use(mw.Recover())
e.Use(cache.EchoCacher(cache.Options{Adapter: "redis", AdapterConfig: `{"Addr":":6379"}`, Section: "test", Interval: 5}))
e.Use(statistics.Statisticser())
// 固定返回值
e.SetHTTPErrorHandler(func(err error, c *echo.Context) {
code := http.StatusInternalServerError
msg := http.StatusText(code)
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code()
msg = he.Error()
}
if e.Debug() {
msg = err.Error()
}
log.Error(4, "http err %v", err)
c.JSON(200, api.ResErr(code, msg))
})
// Routes
e.Get("/", version)
e.Get("/test/add", func(c *echo.Context) error {
u := new(user.User)
u.Name = "weisd"
act, err := user.Create(u)
if err != nil {
return c.JSON(200, api.ResErr(500, err.Error()))
}
return c.JSON(200, api.ResOk(act))
})
e.Get("/test/stat", func(c *echo.Context) error {
status := statistics.StatisticsMap.GetMap()
html := strings.Join(status["Fields"].([]string), " ")
html += "<br>"
data := status["Data"].([][]string)
for i, l := 0, len(data); i < l; i++ {
html += strings.Join(data[i], " ")
html += "<br>"
}
return c.HTML(200, html)
})
e.Get("/test/cache/put", func(c *echo.Context) error {
err := cache.Store(c).Put("name", "weisd", 10)
if err != nil {
return err
}
return c.String(200, "store ok")
})
e.Get("/test/cache/get", func(c *echo.Context) error {
name := cache.Store(c).Get("name")
return c.String(200, "get name %s", name)
})
var jwtSigningKeys = map[string]string{"da": "weisd"}
j := e.Group("/jwt")
j.Use(jwt.EchoJWTAuther(func(c *echo.Context) (key string, err error) {
// get the clientId from header
clientId := c.Request().Header.Get("client-id")
key, ok := jwtSigningKeys[clientId]
if !ok {
return "", echo.NewHTTPError(http.StatusUnauthorized)
}
return key, nil
}))
j.Get("", func(c *echo.Context) error {
return c.String(200, "jwt Access ok with claims %v", jwt.Claims(c))
})
e.Get("/test/jwt/token", func(c *echo.Context) error {
claims := map[string]interface{}{"token": "weisd"}
token, err := jwt.NewToken("weisd", claims)
if err != nil {
return err
}
// show the token use for test
return c.String(200, "token : %s", token)
})
// Start server
e.Run(":1323")
}
func bootstraps() {
setting.InitConfig()
setting.InitServices()
models.InitDatabaseConn()
models.InitRedisPools()
models.RedisCheckConn()
log.Debug("%v", setting.Cfg)
}