作者 tangxvhui
... ... @@ -12,6 +12,7 @@ info(
@server(
prefix: v1/mini
group: article
middleware: LogRequest
jwt: MiniAuth
)
service Core {
... ... @@ -86,7 +87,7 @@ service Core {
@server(
prefix: v1/system
group: article
middleware: LoginStatusCheck
middleware: LoginStatusCheck,LogRequest
jwt: SystemAuth
)
service Core {
... ...
... ... @@ -11,7 +11,7 @@ info(
@server(
prefix: v1/system
group: tags
middleware: LoginStatusCheck
middleware: LoginStatusCheck,LogRequest
jwt: SystemAuth
)
service Core {
... ...
... ... @@ -12,6 +12,7 @@ info(
@server(
prefix: v1/mini
group: comment
middleware: LogRequest
jwt: MiniAuth
)
service Core {
... ... @@ -44,7 +45,7 @@ service Core {
@server(
prefix: v1/system
group: comment
middleware: LoginStatusCheck
middleware: LoginStatusCheck,LogRequest
jwt: SystemAuth
)
service Core {
... ...
... ... @@ -12,6 +12,7 @@ info(
@server(
prefix: v1
group: company
middleware: LogRequest
)
service Core {
@doc "公司搜索(公开的)"
... ... @@ -23,6 +24,7 @@ service Core {
@server(
prefix: v1
group: company
middleware: LogRequest
jwt : MiniAuth
)
service Core {
... ... @@ -31,11 +33,11 @@ service Core {
post /mini/company/search-joined(CompanySearchRequest) returns (CompanySearchResponse)
}
// 小程序接口
// 后台接口
@server(
prefix: v1
group: company
middleware: LoginStatusCheck
middleware: LoginStatusCheck,LogRequest
jwt : SystemAuth
)
service Core {
... ...
... ... @@ -11,7 +11,7 @@ info(
@server(
prefix: v1
group: department
middleware: LoginStatusCheck
middleware: LoginStatusCheck,LogRequest
jwt: SystemAuth
)
service Core {
... ...
... ... @@ -11,6 +11,7 @@ info(
@server(
prefix: v1
group: message
middleware: LogRequest
jwt: MiniAuth
)
service Core {
... ...
... ... @@ -11,7 +11,7 @@ info(
@server(
prefix: v1
group: role
middleware: LoginStatusCheck
middleware: LoginStatusCheck,LogRequest
jwt: SystemAuth
)
service Core {
... ...
... ... @@ -24,6 +24,7 @@ service Core {
@server(
prefix: v1
group: user
middleware: LogRequest
jwt: MiniAuth
)
service Core {
... ... @@ -255,7 +256,7 @@ type (
@server(
prefix: v1
group: user
middleware: LoginStatusCheck
middleware: LoginStatusCheck,LogRequest
jwt: SystemAuth
)
service Core {
... ...
... ... @@ -6,6 +6,8 @@ Migrate: false
Timeout: 30000
# CertFile: ./key/fjmaimaimai.com_bundle.crt
# KeyFile: ./key/fjmaimaimai.com.key
LogRequest: true # 记录详细请求日志
Log:
#Mode: file
Encoding: plain
... ...
... ... @@ -16,6 +16,7 @@ type Config struct {
Migrate bool `json:",optional,default=true"`
ApiAuth ApiService
DebugSmsCode string `json:",optional,default=999512"`
LogRequest bool `json:",optional,default=true"`
}
type ApiService struct {
... ...
... ... @@ -36,6 +36,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ... @@ -67,14 +69,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/article_comment/at_who/list",
Handler: comment.MiniArticleCommentAtWhoHandler(serverCtx),
},
},
}...,
),
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1/mini"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ... @@ -113,6 +116,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ... @@ -129,14 +134,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/mini/message/like",
Handler: message.MiniLikeHandler(serverCtx),
},
},
}...,
),
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ... @@ -191,6 +197,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ... @@ -267,14 +275,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/mini/user/mylike",
Handler: user.MiniMyLikeHandler(serverCtx),
},
},
}...,
),
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ... @@ -338,31 +347,37 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/mini/company/search",
Handler: company.MiniCompanySearchHandler(serverCtx),
},
},
}...,
),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/mini/company/search-joined",
Handler: company.MiniCompanySearchJoinedHandler(serverCtx),
},
},
}...,
),
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ... @@ -381,6 +396,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ... @@ -467,14 +484,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/show/search_article",
Handler: article.MiniSearchArticlePageHandler(serverCtx),
},
},
}...,
),
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1/mini"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodGet,
... ... @@ -519,7 +537,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodGet,
... ... @@ -554,7 +572,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Middleware{serverCtx.LoginStatusCheck, serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodPost,
... ...
package middleware
import (
"github.com/zeromicro/go-zero/rest/handler"
"net/http"
)
type LogRequestMiddleware struct {
logRequest bool
}
// NewLogRequestMiddleware 记录请求
// logRequest true开启记录,false关闭记录
func NewLogRequestMiddleware(logRequest bool) *LogRequestMiddleware {
return &LogRequestMiddleware{
logRequest: logRequest,
}
}
func (m *LogRequestMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
if !m.logRequest {
return func(writer http.ResponseWriter, request *http.Request) {
next(writer, request)
}
}
return handler.DetailedLogHandler(next).(http.HandlerFunc)
}
... ...
... ... @@ -45,6 +45,7 @@ type ServiceContext struct {
SmsService smslib.SMSService
LoginStatusCheck rest.Middleware
LogRequest rest.Middleware
}
func NewServiceContext(c config.Config) *ServiceContext {
... ... @@ -63,6 +64,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
ApiAuthService: apiAuth,
SmsService: smslib.SMSService{Service: gateway.NewService("短信服务", "https://sms.fjmaimaimai.com:9897", time.Second*5)},
LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
LogRequest: middleware.NewLogRequestMiddleware(c.LogRequest).Handle,
ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)),
... ...