🐛 fix session bug
This commit is contained in:
@@ -3,7 +3,6 @@ package generate
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -21,7 +20,7 @@ func GenerateRotateCaptcha(captcha rotate.Captcha, redis *redis.Client, ctx cont
|
||||
}
|
||||
blockData := captchaData.GetData()
|
||||
if blockData == nil {
|
||||
return nil, errors.New("captcha data is nil")
|
||||
return nil, nil
|
||||
}
|
||||
masterImageBase64 := captchaData.GetMasterImage().ToBase64()
|
||||
thumbImageBase64 := captchaData.GetThumbImage().ToBase64()
|
||||
|
@@ -3,7 +3,6 @@ package generate
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -21,7 +20,7 @@ func GenerateSlideBasicCaptcha(slide slide.Captcha, redis *redis.Client, ctx con
|
||||
}
|
||||
blockData := captData.GetData()
|
||||
if blockData == nil {
|
||||
return nil, errors.New("block data is nil")
|
||||
return nil, nil
|
||||
}
|
||||
var masterImageBase64, tileImageBase64 string
|
||||
masterImageBase64 = captData.GetMasterImage().ToBase64()
|
||||
|
@@ -10,7 +10,10 @@ func NewBundle(tag language.Tag, configs ...string) *i18n2.Bundle {
|
||||
bundle := i18n2.NewBundle(tag)
|
||||
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
|
||||
for _, file := range configs {
|
||||
bundle.LoadMessageFile(file)
|
||||
_, err := bundle.LoadMessageFile(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return bundle
|
||||
}
|
||||
|
@@ -9,13 +9,6 @@ import (
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
/*
|
||||
type Cache interface {
|
||||
GetLocalizer() (*i18n2.Localizer, bool)
|
||||
SetLocalizer(l *i18n2.Localizer)
|
||||
}
|
||||
*/
|
||||
|
||||
func getLocalizer(ctx context.Context) (*i18n2.Localizer, bool) {
|
||||
v := ctx.Value(I18nKey)
|
||||
if l, b := v.(*i18n2.Localizer); b {
|
||||
@@ -49,14 +42,3 @@ func IsHasI18n(ctx context.Context) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// func isHasI18n(ctx context.Context) bool {
|
||||
// if use, exist := ctx.Value(isUseI18n).(bool); exist {
|
||||
// return use
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
//
|
||||
// func setHasI18n(ctx context.Context, use bool) context.Context {
|
||||
// return context.WithValue(ctx, isUseI18n, use)
|
||||
// }
|
||||
|
@@ -7,37 +7,24 @@ import (
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func FormatText(ctx context.Context, msgId string, defaultText string) string {
|
||||
return FormatTextWithData(ctx, msgId, defaultText, nil)
|
||||
func FormatText(ctx context.Context, msgId string) string {
|
||||
return FormatTextWithData(ctx, msgId)
|
||||
}
|
||||
|
||||
func FormatTextWithData(ctx context.Context, msgId string, defaultText string, args map[string]interface{}) string {
|
||||
return FormatMessage(ctx, &i18n2.Message{
|
||||
ID: msgId,
|
||||
Other: defaultText,
|
||||
}, args)
|
||||
}
|
||||
|
||||
func FormatMessage(ctx context.Context, message *i18n2.Message, args map[string]interface{}) string {
|
||||
if localizer, ok := getLocalizer(ctx); ok {
|
||||
return localizer.MustLocalize(&i18n2.LocalizeConfig{
|
||||
DefaultMessage: message,
|
||||
TemplateData: args,
|
||||
})
|
||||
func FormatTextWithData(ctx context.Context, msgId string) string {
|
||||
hasI18n := IsHasI18n(ctx)
|
||||
if !hasI18n {
|
||||
return ""
|
||||
}
|
||||
return formatInternalMessage(message, args)
|
||||
}
|
||||
|
||||
func formatInternalMessage(message *i18n2.Message, args map[string]interface{}) string {
|
||||
if args == nil {
|
||||
return message.Other
|
||||
localizer, ok := getLocalizer(ctx)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
tpl := i18n2.NewMessageTemplate(message)
|
||||
msg, err := tpl.Execute("other", args, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
localizeConfig := &i18n2.LocalizeConfig{
|
||||
MessageID: msgId,
|
||||
}
|
||||
return msg
|
||||
localize := localizer.MustLocalize(localizeConfig)
|
||||
return localize
|
||||
}
|
||||
|
||||
func FetchCurrentLanguageFromCtx(ctx context.Context) (*language.Tag, bool) {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package i18n
|
||||
|
||||
const I18nKey = "i18n"
|
||||
const I18nCurrentLangKey = "lang"
|
||||
const I18nCurrentLangKey = "language"
|
||||
|
||||
var (
|
||||
defaultLangHeaderKey = "Accept-Language"
|
||||
|
@@ -5,10 +5,13 @@ import "golang.org/x/text/language"
|
||||
func MatchCurrentLanguageTag(accept string, supportTags []language.Tag) language.Tag {
|
||||
langTags, _, err := language.ParseAcceptLanguage(accept)
|
||||
if err != nil {
|
||||
langTags = []language.Tag{language.English}
|
||||
langTags = []language.Tag{language.Chinese}
|
||||
}
|
||||
var matcher = language.NewMatcher(supportTags)
|
||||
_, i, _ := matcher.Match(langTags...)
|
||||
tag := supportTags[i]
|
||||
if tag == language.Und {
|
||||
tag = language.Chinese
|
||||
}
|
||||
return tag
|
||||
}
|
||||
|
@@ -5,8 +5,8 @@ import "net/http"
|
||||
func CORSMiddleware() func(http.Header) {
|
||||
return 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")
|
||||
header.Set("Access-Control-Expose-Headers", "Content-Length, Content-Type,Authorization,Accept-Language,Origin")
|
||||
header.Set("Access-Control-Allow-Credentials", "true")
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@ package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
func SecurityHeadersMiddleware(w http.ResponseWriter, r *http.Request) {
|
||||
func SecurityHeadersMiddleware(r *http.Request) {
|
||||
r.Header.Set("X-Frame-Options", "DENY")
|
||||
r.Header.Set("Content-Security-Policy", "default-src 'self'; connect-src *; font-src *; script-src-elem * 'unsafe-inline'; img-src * data:; style-src * 'unsafe-inline';")
|
||||
r.Header.Set("X-XSS-Protection", "1; mode=block")
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
)
|
||||
|
||||
func UnauthorizedCallbackMiddleware() func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
return func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
// httpx.WriteJson(w, http.StatusUnauthorized, response.ErrorWithCodeMessage(http.StatusUnauthorized, "Unauthorized"))
|
||||
httpx.OkJsonCtx(r.Context(), w, response.ErrorWithCodeMessage(http.StatusUnauthorized, "Unauthorized"))
|
||||
}
|
||||
}
|
@@ -71,8 +71,8 @@ func ErrorWithMessage(message string) *types.Response {
|
||||
}
|
||||
|
||||
// ErrorWithI18n returns an error response with the given message.
|
||||
func ErrorWithI18n(ctx context.Context, msgId string, defaultMsg string) *types.Response {
|
||||
message := i18n.FormatText(ctx, msgId, defaultMsg)
|
||||
func ErrorWithI18n(ctx context.Context, msgId string) *types.Response {
|
||||
message := i18n.FormatText(ctx, msgId)
|
||||
return &types.Response{
|
||||
Code: 500,
|
||||
Message: message,
|
||||
|
Reference in New Issue
Block a user