✨ add i18n support
This commit is contained in:
@@ -49,7 +49,7 @@ type (
|
||||
timeout: 10s // 超时时间
|
||||
maxBytes: 1048576 // 最大请求大小
|
||||
signature: true // 是否开启签名验证
|
||||
middleware: I18nMiddleware,SecurityHeadersMiddleware // 注册中间件
|
||||
middleware: SecurityHeadersMiddleware // 注册中间件
|
||||
MaxConns: true // 是否开启最大连接数限制
|
||||
Recover: true // 是否开启自动恢复
|
||||
)
|
||||
|
@@ -3,11 +3,11 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/config"
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/handler"
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/svc"
|
||||
"schisandra-album-cloud-microservices/common/middleware"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
@@ -21,14 +21,10 @@ func main() {
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(func(header http.Header) {
|
||||
header.Set("Access-Control-Allow-Origin", "*")
|
||||
header.Add("Access-Control-Allow-Headers", "UserHeader1, UserHeader2")
|
||||
header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
|
||||
header.Set("Access-Control-Expose-Headers", "Content-Length, Content-Type")
|
||||
}, nil, "*"))
|
||||
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(middleware.CORSMiddleware(), nil, "*"))
|
||||
defer server.Stop()
|
||||
|
||||
// i18n middleware
|
||||
server.Use(middleware.I18nMiddleware)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
|
@@ -2,4 +2,7 @@ Name: auth
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
Mysql:
|
||||
Dsn: root:1611@tcp(localhost:3306)/schisandra-cloud-album?charset=utf8mb4&parseTime=True&loc=Local
|
||||
DataSource: root:1611@tcp(localhost:3306)/schisandra-cloud-album?charset=utf8mb4&parseTime=True&loc=Local
|
||||
Auth:
|
||||
AccessSecret: uOvKLmVfztaXGpNYd4Z0I1SiT7MweJhl
|
||||
AccessExpire: 86400
|
@@ -4,7 +4,11 @@ import "github.com/zeromicro/go-zero/rest"
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
Auth struct {
|
||||
AccessSecret string
|
||||
AccessExpire int64
|
||||
}
|
||||
Mysql struct {
|
||||
Dsn string
|
||||
DataSource string
|
||||
}
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@ import (
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.I18nMiddleware, serverCtx.SecurityHeadersMiddleware},
|
||||
[]rest.Middleware{serverCtx.SecurityHeadersMiddleware},
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
|
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/types"
|
||||
"schisandra-album-cloud-microservices/common/i18n"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -25,6 +26,10 @@ func NewAccountLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Acco
|
||||
|
||||
func (l *AccountLoginLogic) AccountLogin(req *types.AccountLoginRequest) (resp *types.LoginResponse, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
i18n.IsHasI18n(l.ctx)
|
||||
text := i18n.FormatText(l.ctx, "user.name", "landaiqing")
|
||||
|
||||
return
|
||||
return &types.LoginResponse{
|
||||
AccessToken: text,
|
||||
}, nil
|
||||
}
|
||||
|
@@ -1,19 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
type I18nMiddleware struct {
|
||||
}
|
||||
|
||||
func NewI18nMiddleware() *I18nMiddleware {
|
||||
return &I18nMiddleware{}
|
||||
}
|
||||
|
||||
func (m *I18nMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO generate middleware implement function, delete after code implementation
|
||||
|
||||
// Passthrough to next handler if need
|
||||
next(w, r)
|
||||
}
|
||||
}
|
@@ -6,14 +6,14 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/config"
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/middleware"
|
||||
"schisandra-album-cloud-microservices/common/core"
|
||||
"schisandra-album-cloud-microservices/common/ent/gen/entschema"
|
||||
"schisandra-album-cloud-microservices/common/ent"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
I18nMiddleware rest.Middleware
|
||||
SecurityHeadersMiddleware rest.Middleware
|
||||
DB *entschema.Client
|
||||
DB *ent.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
@@ -21,6 +21,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
Config: c,
|
||||
I18nMiddleware: middleware.NewI18nMiddleware().Handle,
|
||||
SecurityHeadersMiddleware: middleware.NewSecurityHeadersMiddleware().Handle,
|
||||
DB: core.InitMySQL(c.Mysql.Dsn),
|
||||
DB: core.InitMySQL(c.Mysql.DataSource),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user