作者 yangfu

main 修改

1 package main 1 package main
2 2
3 import ( 3 import (
  4 + "context"
4 "flag" 5 "flag"
  6 + "github.com/zeromicro/go-zero/rest/httpx"
5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
6 "net/http" 11 "net/http"
7 "strings" 12 "strings"
8 13
9 - "github.com/zeromicro/go-zero/core/logx"  
10 - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"  
11 -  
12 "github.com/golang-jwt/jwt/v4/request" 14 "github.com/golang-jwt/jwt/v4/request"
13 "github.com/zeromicro/go-zero/core/conf" 15 "github.com/zeromicro/go-zero/core/conf"
  16 + "github.com/zeromicro/go-zero/core/logx"
14 "github.com/zeromicro/go-zero/rest" 17 "github.com/zeromicro/go-zero/rest"
15 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/config" 18 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/config"
16 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler" 19 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler"
@@ -20,27 +23,16 @@ import ( @@ -20,27 +23,16 @@ import (
20 var configFile = flag.String("f", "cmd/discuss/api/etc/core.yaml", "the config file") 23 var configFile = flag.String("f", "cmd/discuss/api/etc/core.yaml", "the config file")
21 24
22 func main() { 25 func main() {
  26 + // 配置加载
23 flag.Parse() 27 flag.Parse()
24 -  
25 var c config.Config 28 var c config.Config
26 conf.MustLoad(*configFile, &c) 29 conf.MustLoad(*configFile, &c)
27 30
28 - // 默认的token头 Authorization 修改未 x-token  
29 - request.AuthorizationHeaderExtractor = &request.PostExtractionFilter{  
30 - request.HeaderExtractor{"x-mmm-accesstoken"}, func(tok string) (string, error) {  
31 - // Should be a bearer token  
32 - if len(tok) > 6 && strings.ToUpper(tok[0:7]) == "BEARER " {  
33 - return tok[7:], nil  
34 - }  
35 - return tok, nil  
36 - },  
37 - }  
38 -  
39 - // 初始化Domain里面的配置  
40 - domain.ProjectName = c.Name 31 + // 系统设置
  32 + systemSetup(c)
41 33
  34 + // 服务初始化
42 opts := make([]rest.RunOption, 0) 35 opts := make([]rest.RunOption, 0)
43 - // cors  
44 opt := rest.WithCustomCors(func(header http.Header) { 36 opt := rest.WithCustomCors(func(header http.Header) {
45 header.Set("Access-Control-Allow-Headers", "*") 37 header.Set("Access-Control-Allow-Headers", "*")
46 }, func(writer http.ResponseWriter) { 38 }, func(writer http.ResponseWriter) {
@@ -49,14 +41,40 @@ func main() { @@ -49,14 +41,40 @@ func main() {
49 opts = append(opts, opt) 41 opts = append(opts, opt)
50 server := rest.MustNewServer(c.RestConf, opts...) 42 server := rest.MustNewServer(c.RestConf, opts...)
51 defer server.Stop() 43 defer server.Stop()
52 -  
53 ctx := svc.NewServiceContext(c) 44 ctx := svc.NewServiceContext(c)
54 handler.RegisterHandlers(server, ctx) 45 handler.RegisterHandlers(server, ctx)
55 46
  47 + // 数据迁移
56 if c.Migrate { 48 if c.Migrate {
57 db.Migrate(ctx.DB) 49 db.Migrate(ctx.DB)
58 } 50 }
59 51
  52 + // 服务启动
60 logx.Infof("Starting server at %s:%d... \n", c.Host, c.Port) 53 logx.Infof("Starting server at %s:%d... \n", c.Host, c.Port)
61 server.Start() 54 server.Start()
62 } 55 }
  56 +
  57 +func systemSetup(c config.Config) {
  58 + // 初始化Domain里面的配置
  59 + domain.ProjectName = c.Name
  60 +
  61 + // 默认的token头 Authorization 修改为 x-mmm-accesstoken
  62 + request.AuthorizationHeaderExtractor = &request.PostExtractionFilter{
  63 + request.HeaderExtractor{"x-mmm-accesstoken"}, func(tok string) (string, error) {
  64 + // Should be a bearer token
  65 + if len(tok) > 6 && strings.ToUpper(tok[0:7]) == "BEARER " {
  66 + return tok[7:], nil
  67 + }
  68 + return tok, nil
  69 + },
  70 + }
  71 +
  72 + // 系统错误应答包装
  73 + httpx.SetErrorHandlerCtx(func(ctx context.Context, err error) (int, any) {
  74 + return http.StatusOK, result.Error(xerr.ServerCommonError, err.Error())
  75 + })
  76 + // 系统成功应答包装
  77 + httpx.SetOkHandler(func(ctx context.Context, a any) any {
  78 + return result.Success(a)
  79 + })
  80 +}
@@ -84,9 +84,6 @@ type( @@ -84,9 +84,6 @@ type(
84 } 84 }
85 MiniUserInfoResponse { 85 MiniUserInfoResponse {
86 User *UserItem `json:"user,omitempty"` // 用户信息 86 User *UserItem `json:"user,omitempty"` // 用户信息
87 - TotalArticle int64 `json:"totalArticle"` // 累计信息发布  
88 - TotalLoved int64 `json:"totalLoved"` // 累计收到的赞  
89 - TotalAccepted int64 `json:"totalAccepted"` // 累计被采纳  
90 Accounts []Account `json:"accounts"` // 公司账号 87 Accounts []Account `json:"accounts"` // 公司账号
91 Auths []Auth `json:"auths"` // 权限列表 88 Auths []Auth `json:"auths"` // 权限列表
92 } 89 }
@@ -303,9 +303,6 @@ type MiniUserInfoRequest struct { @@ -303,9 +303,6 @@ type MiniUserInfoRequest struct {
303 303
304 type MiniUserInfoResponse struct { 304 type MiniUserInfoResponse struct {
305 User *UserItem `json:"user,omitempty"` // 用户信息 305 User *UserItem `json:"user,omitempty"` // 用户信息
306 - TotalArticle int64 `json:"totalArticle"` // 累计信息发布  
307 - TotalLoved int64 `json:"totalLoved"` // 累计收到的赞  
308 - TotalAccepted int64 `json:"totalAccepted"` // 累计被采纳  
309 Accounts []Account `json:"accounts"` // 公司账号 306 Accounts []Account `json:"accounts"` // 公司账号
310 Auths []Auth `json:"auths"` // 权限列表 307 Auths []Auth `json:"auths"` // 权限列表
311 } 308 }