-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.go
More file actions
66 lines (54 loc) · 1.41 KB
/
Copy pathbackend.go
File metadata and controls
66 lines (54 loc) · 1.41 KB
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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"runtime/debug"
"tapi/common/varx"
"tapi/internal/config"
"tapi/internal/handler"
"tapi/internal/svc"
"tapi/internal/types"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
)
var configFile = flag.String("f", "etc/backend.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
httpx.SetErrorHandlerCtx(func(ctx context.Context, err error) (int, interface{}) {
fmt.Println(err.Error())
return http.StatusOK, &types.CodeErrorResponse{
Code: 500,
Msg: err.Error(),
}
})
// 全局recover中间件
server.Use(func(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if result := recover(); result != nil {
log.Println(fmt.Sprintf("%v\n%s", result, debug.Stack()))
httpx.OkJson(w, &types.CodeErrorResponse{
Code: 500,
Msg: "服务器错误", //string(debug.Stack()),
})
}
}()
next.ServeHTTP(w, r)
})
})
// 路由列表
varx.RouterList = server.Routes()
varx.Ctx = ctx.BkModel
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}