批量查询

This commit is contained in:
xmdhs 2023-09-10 21:01:11 +08:00
parent c6ef8a8706
commit bfaf686ff0
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
4 changed files with 13 additions and 7 deletions

View File

@ -159,10 +159,15 @@ func (y *Yggdrasil) GetProfile() httprouter.Handle {
func (y *Yggdrasil) BatchProfile() httprouter.Handle { func (y *Yggdrasil) BatchProfile() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ctx := r.Context() ctx := r.Context()
a, has := getAnyModel[[]string](ctx, w, r.Body, y.validate, y.logger) a, has := getAnyModel[[]string](ctx, w, r.Body, nil, y.logger)
if !has { if !has {
return return
} }
if len(a) > 5 {
y.logger.DebugContext(ctx, "最多同时查询五个")
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: "最多同时查询五个"}, 400)
return
}
ul, err := y.yggdrasilService.BatchProfile(ctx, a) ul, err := y.yggdrasilService.BatchProfile(ctx, a)
if err != nil { if err != nil {
y.logger.WarnContext(ctx, err.Error()) y.logger.WarnContext(ctx, err.Error())

View File

@ -49,7 +49,7 @@ type RefreshToken struct {
type UserInfo struct { type UserInfo struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Properties []UserProperties `json:"properties"` Properties []UserProperties `json:"properties,omitempty"`
} }
type UserProperties struct { type UserProperties struct {

View File

@ -33,7 +33,7 @@ func newYggdrasil(r *httprouter.Router, handelY yggdrasil.Yggdrasil) error {
r.DELETE("/api/yggdrasil/api/user/profile/:uuid/:textureType", warpHJSON(handelY.DelTexture())) r.DELETE("/api/yggdrasil/api/user/profile/:uuid/:textureType", warpHJSON(handelY.DelTexture()))
r.GET("/api/yggdrasil/sessionserver/session/minecraft/profile/:uuid", warpHJSON(handelY.GetProfile())) r.GET("/api/yggdrasil/sessionserver/session/minecraft/profile/:uuid", warpHJSON(handelY.GetProfile()))
r.POST("/api/profiles/minecraft", warpHJSON(handelY.BatchProfile())) r.POST("/api/yggdrasil/api/profiles/minecraft", warpHJSON(handelY.BatchProfile()))
r.GET("/api/yggdrasil", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { r.GET("/api/yggdrasil", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Write([]byte(`{ w.Write([]byte(`{

View File

@ -15,10 +15,11 @@ func DeCodeBody[T any](r io.Reader, v *validator.Validate) (T, error) {
if err != nil { if err != nil {
return a, fmt.Errorf("DeCodeBody: %w", err) return a, fmt.Errorf("DeCodeBody: %w", err)
} }
if v != nil {
err = v.Struct(a) err = v.Struct(a)
if err != nil { if err != nil {
return a, fmt.Errorf("DeCodeBody: %w", err) return a, fmt.Errorf("DeCodeBody: %w", err)
} }
}
return a, nil return a, nil
} }