-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
78 lines (54 loc) · 1.06 KB
/
server.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
package nutshttp
import (
"github.com/spf13/viper"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/xujiajun/nutsdb"
)
type NutsHTTPServer struct {
core *core
r *gin.Engine
}
func NewNutsHTTPServer(db *nutsdb.DB) (*NutsHTTPServer, error) {
c := &core{db}
r := gin.Default()
s := &NutsHTTPServer{
core: c,
r: r,
}
err := s.InitConfig()
if err != nil {
return nil, err
}
s.initRouter()
return s, nil
}
func (s *NutsHTTPServer) InitConfig() error {
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetDefault("port", "8080")
err := viper.ReadInConfig()
if err != nil {
return err
}
err = viper.UnmarshalKey("JWT", &jwtSetting)
jwtSetting.Expire *= time.Minute
if err != nil {
return err
}
return nil
}
func (s *NutsHTTPServer) Run(addr string) error {
return http.ListenAndServe(addr, s.r)
}
func (s *NutsHTTPServer) initRouter() {
s.r.Use(Cors())
s.initSetRouter()
s.initListRouter()
s.initStringRouter()
s.initZSetRouter()
s.initLoginRouter()
s.initCommonRouter()
}