-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
165 lines (134 loc) · 4.63 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"GZHU-Pi/env"
"GZHU-Pi/models"
"GZHU-Pi/routers"
"github.com/astaxie/beego/logs"
"github.com/gorilla/mux"
"github.com/prest/adapters/postgres"
"github.com/prest/cmd"
"github.com/prest/config"
"github.com/prest/config/router"
"github.com/prest/middlewares"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"log"
"net/http"
_ "net/http/pprof"
"os"
"regexp"
"strings"
)
func main() {
err := env.InitConfigure()
if err != nil {
log.Fatal(err)
}
err = models.InitDb()
if err != nil {
log.Fatal(err)
}
err = env.InitRedis()
if err != nil {
log.Fatal(err)
}
go env.MemoryCollector()
r := mux.NewRouter()
//获取阿里云函数计算容器内部端口
port := os.Getenv("FC_SERVER_PORT")
if port == "" {
port = "9000"
logs.Info("自主部署 Server on port: " + port)
_ = env.InitLogger("./log/")
r = r.PathPrefix("/api/v1").Subrouter()
} else {
logs.Info("阿里云云函数部署 Server on port: " + port)
_ = env.InitLogger("/tmp/log/")
r = r.PathPrefix("/2016-08-15/proxy/GZHU-API/go/api/v1").Subrouter()
}
if env.Conf.App.PRest == true {
logs.Info("启用pRest接口服务")
runWithPRest(r)
} else {
customRouter(r)
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatal(err)
}
}
}
func runWithPRest(r *mux.Router) {
//同步应用配置 覆盖pRest部分配置
viper.Set("pg.host", viper.GetString("db.host"))
viper.Set("pg.user", viper.GetString("db.user"))
viper.Set("pg.pass", viper.GetString("db.password"))
viper.Set("pg.port", viper.GetInt("db.port"))
viper.Set("pg.database", viper.GetString("db.dbname"))
viper.Set("ssl.mode", viper.GetString("db.sslmode"))
viper.Set("jwt.key", viper.GetString("secret.jwt"))
// load config for pREST
config.Load()
// Load Postgres Adapter
postgres.Load()
// Get pREST app
n := middlewares.GetApp()
// Register custom middleware
n.UseFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
//该中间件用于消除路由前缀对pRest内部路由的影响
if !strings.Contains(r.URL.Path, "/api/v") {
_, _ = w.Write([]byte("path must contains /api/v\\d"))
return
}
reg := regexp.MustCompile(`[\s\S]*/api/v\d`)
match := reg.FindStringSubmatch(r.URL.Path)
if len(match) > 0 {
r.URL.Path = strings.ReplaceAll(r.URL.Path, match[0], "")
}
if r.URL.Path == "" {
r.URL.Path = "/"
}
next(w, r)
})
n.UseFunc(routers.TableAccessHandle)
// Get pREST router
r = router.Get()
// just for test
r.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Pong!"))
})
// Register custom routes
customRouter(r)
os.Args = []string{os.Args[0]}
// Call pREST cmd
cmd.Execute()
}
func customRouter(r *mux.Router) *mux.Router {
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello!"))
})
r.HandleFunc("/auth", routers.PanicMV(routers.Auth)).Methods("POST")
//微信公众号接口
//r.HandleFunc("/wx/check", routers.PanicMV(routers.WeChatCheck))
//r.HandleFunc("/wx/check", routers.PanicMV(routers.Hello))
//教务系统
r.HandleFunc("/jwxt/course", routers.PanicMV(routers.JWMiddleWare(routers.Course))).Methods("GET", "POST")
r.HandleFunc("/jwxt/exam", routers.PanicMV(routers.JWMiddleWare(routers.Exam))).Methods("GET", "POST")
r.HandleFunc("/jwxt/grade", routers.PanicMV(routers.JWMiddleWare(routers.Grade))).Methods("GET", "POST")
r.HandleFunc("/jwxt/classroom", routers.PanicMV(routers.JWMiddleWare(routers.EmptyRoom))).Methods("GET", "POST")
r.HandleFunc("/jwxt/achieve", routers.PanicMV(routers.JWMiddleWare(routers.Achieve))).Methods("GET", "POST")
r.HandleFunc("/jwxt/all-course", routers.PanicMV(routers.JWMiddleWare(routers.AllCourse))).Methods("GET", "POST")
r.HandleFunc("/jwxt/rank", routers.PanicMV(routers.Rank)).Methods("GET")
//图书馆
r.HandleFunc("/library/search", routers.PanicMV(routers.BookSearch)).Methods("GET")
r.HandleFunc("/library/holdings", routers.PanicMV(routers.BookHoldings)).Methods("GET")
//第二课堂学分系统
r.HandleFunc("/second/my", routers.PanicMV(routers.SecondMiddleWare(routers.MySecond))).Methods("GET", "POST")
r.HandleFunc("/second/search", routers.PanicMV(routers.SecondMiddleWare(routers.SecondSearch))).Methods("GET", "POST")
r.HandleFunc("/second/image", routers.PanicMV(routers.SecondMiddleWare(routers.SecondImage))).Methods("GET", "POST")
//物理实验平台
//r.HandleFunc("/exp", test).Methods("POST")
//四六级、普通话考试查询
//r.HandleFunc("/exam/cet", test).Methods("POST")
//r.HandleFunc("/exam/chinese", test).Methods("POST")
return r
}