作者 tangxvhui
@@ -12,6 +12,7 @@ info( @@ -12,6 +12,7 @@ info(
12 @server( 12 @server(
13 prefix: v1/mini 13 prefix: v1/mini
14 group: article 14 group: article
  15 + middleware: LogRequest
15 jwt: MiniAuth 16 jwt: MiniAuth
16 ) 17 )
17 service Core { 18 service Core {
@@ -86,7 +87,7 @@ service Core { @@ -86,7 +87,7 @@ service Core {
86 @server( 87 @server(
87 prefix: v1/system 88 prefix: v1/system
88 group: article 89 group: article
89 - middleware: LoginStatusCheck 90 + middleware: LoginStatusCheck,LogRequest
90 jwt: SystemAuth 91 jwt: SystemAuth
91 ) 92 )
92 service Core { 93 service Core {
@@ -11,7 +11,7 @@ info( @@ -11,7 +11,7 @@ info(
11 @server( 11 @server(
12 prefix: v1/system 12 prefix: v1/system
13 group: tags 13 group: tags
14 - middleware: LoginStatusCheck 14 + middleware: LoginStatusCheck,LogRequest
15 jwt: SystemAuth 15 jwt: SystemAuth
16 ) 16 )
17 service Core { 17 service Core {
@@ -12,6 +12,7 @@ info( @@ -12,6 +12,7 @@ info(
12 @server( 12 @server(
13 prefix: v1/mini 13 prefix: v1/mini
14 group: comment 14 group: comment
  15 + middleware: LogRequest
15 jwt: MiniAuth 16 jwt: MiniAuth
16 ) 17 )
17 service Core { 18 service Core {
@@ -44,7 +45,7 @@ service Core { @@ -44,7 +45,7 @@ service Core {
44 @server( 45 @server(
45 prefix: v1/system 46 prefix: v1/system
46 group: comment 47 group: comment
47 - middleware: LoginStatusCheck 48 + middleware: LoginStatusCheck,LogRequest
48 jwt: SystemAuth 49 jwt: SystemAuth
49 ) 50 )
50 service Core { 51 service Core {
@@ -12,6 +12,7 @@ info( @@ -12,6 +12,7 @@ info(
12 @server( 12 @server(
13 prefix: v1 13 prefix: v1
14 group: company 14 group: company
  15 + middleware: LogRequest
15 ) 16 )
16 service Core { 17 service Core {
17 @doc "公司搜索(公开的)" 18 @doc "公司搜索(公开的)"
@@ -23,6 +24,7 @@ service Core { @@ -23,6 +24,7 @@ service Core {
23 @server( 24 @server(
24 prefix: v1 25 prefix: v1
25 group: company 26 group: company
  27 + middleware: LogRequest
26 jwt : MiniAuth 28 jwt : MiniAuth
27 ) 29 )
28 service Core { 30 service Core {
@@ -31,11 +33,11 @@ service Core { @@ -31,11 +33,11 @@ service Core {
31 post /mini/company/search-joined(CompanySearchRequest) returns (CompanySearchResponse) 33 post /mini/company/search-joined(CompanySearchRequest) returns (CompanySearchResponse)
32 } 34 }
33 35
34 -// 小程序接口 36 +// 后台接口
35 @server( 37 @server(
36 prefix: v1 38 prefix: v1
37 group: company 39 group: company
38 - middleware: LoginStatusCheck 40 + middleware: LoginStatusCheck,LogRequest
39 jwt : SystemAuth 41 jwt : SystemAuth
40 ) 42 )
41 service Core { 43 service Core {
@@ -11,7 +11,7 @@ info( @@ -11,7 +11,7 @@ info(
11 @server( 11 @server(
12 prefix: v1 12 prefix: v1
13 group: department 13 group: department
14 - middleware: LoginStatusCheck 14 + middleware: LoginStatusCheck,LogRequest
15 jwt: SystemAuth 15 jwt: SystemAuth
16 ) 16 )
17 service Core { 17 service Core {
@@ -11,6 +11,7 @@ info( @@ -11,6 +11,7 @@ info(
11 @server( 11 @server(
12 prefix: v1 12 prefix: v1
13 group: message 13 group: message
  14 + middleware: LogRequest
14 jwt: MiniAuth 15 jwt: MiniAuth
15 ) 16 )
16 service Core { 17 service Core {
@@ -11,7 +11,7 @@ info( @@ -11,7 +11,7 @@ info(
11 @server( 11 @server(
12 prefix: v1 12 prefix: v1
13 group: role 13 group: role
14 - middleware: LoginStatusCheck 14 + middleware: LoginStatusCheck,LogRequest
15 jwt: SystemAuth 15 jwt: SystemAuth
16 ) 16 )
17 service Core { 17 service Core {
@@ -24,6 +24,7 @@ service Core { @@ -24,6 +24,7 @@ service Core {
24 @server( 24 @server(
25 prefix: v1 25 prefix: v1
26 group: user 26 group: user
  27 + middleware: LogRequest
27 jwt: MiniAuth 28 jwt: MiniAuth
28 ) 29 )
29 service Core { 30 service Core {
@@ -255,7 +256,7 @@ type ( @@ -255,7 +256,7 @@ type (
255 @server( 256 @server(
256 prefix: v1 257 prefix: v1
257 group: user 258 group: user
258 - middleware: LoginStatusCheck 259 + middleware: LoginStatusCheck,LogRequest
259 jwt: SystemAuth 260 jwt: SystemAuth
260 ) 261 )
261 service Core { 262 service Core {
@@ -6,6 +6,8 @@ Migrate: false @@ -6,6 +6,8 @@ Migrate: false
6 Timeout: 30000 6 Timeout: 30000
7 # CertFile: ./key/fjmaimaimai.com_bundle.crt 7 # CertFile: ./key/fjmaimaimai.com_bundle.crt
8 # KeyFile: ./key/fjmaimaimai.com.key 8 # KeyFile: ./key/fjmaimaimai.com.key
  9 +LogRequest: true # 记录详细请求日志
  10 +
9 Log: 11 Log:
10 #Mode: file 12 #Mode: file
11 Encoding: plain 13 Encoding: plain
@@ -16,6 +16,7 @@ type Config struct { @@ -16,6 +16,7 @@ type Config struct {
16 Migrate bool `json:",optional,default=true"` 16 Migrate bool `json:",optional,default=true"`
17 ApiAuth ApiService 17 ApiAuth ApiService
18 DebugSmsCode string `json:",optional,default=999512"` 18 DebugSmsCode string `json:",optional,default=999512"`
  19 + LogRequest bool `json:",optional,default=true"`
19 } 20 }
20 21
21 type ApiService struct { 22 type ApiService struct {
@@ -36,6 +36,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -36,6 +36,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
36 ) 36 )
37 37
38 server.AddRoutes( 38 server.AddRoutes(
  39 + rest.WithMiddlewares(
  40 + []rest.Middleware{serverCtx.LogRequest},
39 []rest.Route{ 41 []rest.Route{
40 { 42 {
41 Method: http.MethodPost, 43 Method: http.MethodPost,
@@ -67,14 +69,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -67,14 +69,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
67 Path: "/article_comment/at_who/list", 69 Path: "/article_comment/at_who/list",
68 Handler: comment.MiniArticleCommentAtWhoHandler(serverCtx), 70 Handler: comment.MiniArticleCommentAtWhoHandler(serverCtx),
69 }, 71 },
70 - }, 72 + }...,
  73 + ),
71 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret), 74 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
72 rest.WithPrefix("/v1/mini"), 75 rest.WithPrefix("/v1/mini"),
73 ) 76 )
74 77
75 server.AddRoutes( 78 server.AddRoutes(
76 rest.WithMiddlewares( 79 rest.WithMiddlewares(
77 - []rest.Middleware{serverCtx.LoginStatusCheck}, 80 + []rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
78 []rest.Route{ 81 []rest.Route{
79 { 82 {
80 Method: http.MethodPost, 83 Method: http.MethodPost,
@@ -113,6 +116,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -113,6 +116,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
113 ) 116 )
114 117
115 server.AddRoutes( 118 server.AddRoutes(
  119 + rest.WithMiddlewares(
  120 + []rest.Middleware{serverCtx.LogRequest},
116 []rest.Route{ 121 []rest.Route{
117 { 122 {
118 Method: http.MethodPost, 123 Method: http.MethodPost,
@@ -129,14 +134,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -129,14 +134,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
129 Path: "/mini/message/like", 134 Path: "/mini/message/like",
130 Handler: message.MiniLikeHandler(serverCtx), 135 Handler: message.MiniLikeHandler(serverCtx),
131 }, 136 },
132 - }, 137 + }...,
  138 + ),
133 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret), 139 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
134 rest.WithPrefix("/v1"), 140 rest.WithPrefix("/v1"),
135 ) 141 )
136 142
137 server.AddRoutes( 143 server.AddRoutes(
138 rest.WithMiddlewares( 144 rest.WithMiddlewares(
139 - []rest.Middleware{serverCtx.LoginStatusCheck}, 145 + []rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
140 []rest.Route{ 146 []rest.Route{
141 { 147 {
142 Method: http.MethodPost, 148 Method: http.MethodPost,
@@ -191,6 +197,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -191,6 +197,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
191 ) 197 )
192 198
193 server.AddRoutes( 199 server.AddRoutes(
  200 + rest.WithMiddlewares(
  201 + []rest.Middleware{serverCtx.LogRequest},
194 []rest.Route{ 202 []rest.Route{
195 { 203 {
196 Method: http.MethodPost, 204 Method: http.MethodPost,
@@ -267,14 +275,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -267,14 +275,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
267 Path: "/mini/user/mylike", 275 Path: "/mini/user/mylike",
268 Handler: user.MiniMyLikeHandler(serverCtx), 276 Handler: user.MiniMyLikeHandler(serverCtx),
269 }, 277 },
270 - }, 278 + }...,
  279 + ),
271 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret), 280 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
272 rest.WithPrefix("/v1"), 281 rest.WithPrefix("/v1"),
273 ) 282 )
274 283
275 server.AddRoutes( 284 server.AddRoutes(
276 rest.WithMiddlewares( 285 rest.WithMiddlewares(
277 - []rest.Middleware{serverCtx.LoginStatusCheck}, 286 + []rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
278 []rest.Route{ 287 []rest.Route{
279 { 288 {
280 Method: http.MethodPost, 289 Method: http.MethodPost,
@@ -338,31 +347,37 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -338,31 +347,37 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
338 ) 347 )
339 348
340 server.AddRoutes( 349 server.AddRoutes(
  350 + rest.WithMiddlewares(
  351 + []rest.Middleware{serverCtx.LogRequest},
341 []rest.Route{ 352 []rest.Route{
342 { 353 {
343 Method: http.MethodPost, 354 Method: http.MethodPost,
344 Path: "/mini/company/search", 355 Path: "/mini/company/search",
345 Handler: company.MiniCompanySearchHandler(serverCtx), 356 Handler: company.MiniCompanySearchHandler(serverCtx),
346 }, 357 },
347 - }, 358 + }...,
  359 + ),
348 rest.WithPrefix("/v1"), 360 rest.WithPrefix("/v1"),
349 ) 361 )
350 362
351 server.AddRoutes( 363 server.AddRoutes(
  364 + rest.WithMiddlewares(
  365 + []rest.Middleware{serverCtx.LogRequest},
352 []rest.Route{ 366 []rest.Route{
353 { 367 {
354 Method: http.MethodPost, 368 Method: http.MethodPost,
355 Path: "/mini/company/search-joined", 369 Path: "/mini/company/search-joined",
356 Handler: company.MiniCompanySearchJoinedHandler(serverCtx), 370 Handler: company.MiniCompanySearchJoinedHandler(serverCtx),
357 }, 371 },
358 - }, 372 + }...,
  373 + ),
359 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret), 374 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
360 rest.WithPrefix("/v1"), 375 rest.WithPrefix("/v1"),
361 ) 376 )
362 377
363 server.AddRoutes( 378 server.AddRoutes(
364 rest.WithMiddlewares( 379 rest.WithMiddlewares(
365 - []rest.Middleware{serverCtx.LoginStatusCheck}, 380 + []rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
366 []rest.Route{ 381 []rest.Route{
367 { 382 {
368 Method: http.MethodPost, 383 Method: http.MethodPost,
@@ -381,6 +396,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -381,6 +396,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
381 ) 396 )
382 397
383 server.AddRoutes( 398 server.AddRoutes(
  399 + rest.WithMiddlewares(
  400 + []rest.Middleware{serverCtx.LogRequest},
384 []rest.Route{ 401 []rest.Route{
385 { 402 {
386 Method: http.MethodPost, 403 Method: http.MethodPost,
@@ -467,14 +484,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -467,14 +484,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
467 Path: "/show/search_article", 484 Path: "/show/search_article",
468 Handler: article.MiniSearchArticlePageHandler(serverCtx), 485 Handler: article.MiniSearchArticlePageHandler(serverCtx),
469 }, 486 },
470 - }, 487 + }...,
  488 + ),
471 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret), 489 rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
472 rest.WithPrefix("/v1/mini"), 490 rest.WithPrefix("/v1/mini"),
473 ) 491 )
474 492
475 server.AddRoutes( 493 server.AddRoutes(
476 rest.WithMiddlewares( 494 rest.WithMiddlewares(
477 - []rest.Middleware{serverCtx.LoginStatusCheck}, 495 + []rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
478 []rest.Route{ 496 []rest.Route{
479 { 497 {
480 Method: http.MethodGet, 498 Method: http.MethodGet,
@@ -519,7 +537,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -519,7 +537,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
519 537
520 server.AddRoutes( 538 server.AddRoutes(
521 rest.WithMiddlewares( 539 rest.WithMiddlewares(
522 - []rest.Middleware{serverCtx.LoginStatusCheck}, 540 + []rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
523 []rest.Route{ 541 []rest.Route{
524 { 542 {
525 Method: http.MethodGet, 543 Method: http.MethodGet,
@@ -554,7 +572,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -554,7 +572,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
554 572
555 server.AddRoutes( 573 server.AddRoutes(
556 rest.WithMiddlewares( 574 rest.WithMiddlewares(
557 - []rest.Middleware{serverCtx.LoginStatusCheck}, 575 + []rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
558 []rest.Route{ 576 []rest.Route{
559 { 577 {
560 Method: http.MethodPost, 578 Method: http.MethodPost,
  1 +package middleware
  2 +
  3 +import (
  4 + "github.com/zeromicro/go-zero/rest/handler"
  5 + "net/http"
  6 +)
  7 +
  8 +type LogRequestMiddleware struct {
  9 + logRequest bool
  10 +}
  11 +
  12 +// NewLogRequestMiddleware 记录请求
  13 +// logRequest true开启记录,false关闭记录
  14 +func NewLogRequestMiddleware(logRequest bool) *LogRequestMiddleware {
  15 + return &LogRequestMiddleware{
  16 + logRequest: logRequest,
  17 + }
  18 +}
  19 +
  20 +func (m *LogRequestMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
  21 + if !m.logRequest {
  22 + return func(writer http.ResponseWriter, request *http.Request) {
  23 + next(writer, request)
  24 + }
  25 + }
  26 + return handler.DetailedLogHandler(next).(http.HandlerFunc)
  27 +}
@@ -45,6 +45,7 @@ type ServiceContext struct { @@ -45,6 +45,7 @@ type ServiceContext struct {
45 SmsService smslib.SMSService 45 SmsService smslib.SMSService
46 46
47 LoginStatusCheck rest.Middleware 47 LoginStatusCheck rest.Middleware
  48 + LogRequest rest.Middleware
48 } 49 }
49 50
50 func NewServiceContext(c config.Config) *ServiceContext { 51 func NewServiceContext(c config.Config) *ServiceContext {
@@ -63,6 +64,7 @@ func NewServiceContext(c config.Config) *ServiceContext { @@ -63,6 +64,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
63 ApiAuthService: apiAuth, 64 ApiAuthService: apiAuth,
64 SmsService: smslib.SMSService{Service: gateway.NewService("短信服务", "https://sms.fjmaimaimai.com:9897", time.Second*5)}, 65 SmsService: smslib.SMSService{Service: gateway.NewService("短信服务", "https://sms.fjmaimaimai.com:9897", time.Second*5)},
65 LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle, 66 LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
  67 + LogRequest: middleware.NewLogRequestMiddleware(c.LogRequest).Handle,
66 68
67 ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)), 69 ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
68 ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)), 70 ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)),