kgen 平台接口

This commit is contained in:
lianghuanjie
2025-01-13 19:48:14 +08:00
parent 2b9a1acce2
commit d592615908
9 changed files with 322 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package middleware
import (
"net/http"
"nova_task/internal/consts"
"nova_task/internal/model"
"nova_task/internal/pkg/errs"
)
type KGeNApiKeyCheckMiddleware struct {
conf model.NhSystemConfigModel
}
func NewKGeNApiKeyCheckMiddleware(conf model.NhSystemConfigModel) *KGeNApiKeyCheckMiddleware {
return &KGeNApiKeyCheckMiddleware{conf: conf}
}
func (m *KGeNApiKeyCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
key, err := m.conf.GetApiKey(ctx, consts.KgenApiKey)
if err != nil {
_, result := errs.ErrorHandle(errs.New(errs.ErrDatabaseOperate, "api key config not exist"))
errs.WriteHttpResponse(ctx, w, result)
return
}
apiKey := r.Header.Get("x-api-key")
if apiKey == "" || apiKey != key {
_, result := errs.ErrorHandle(errs.New(errs.ErrUnauthorized, "invalid api key"))
errs.WriteHttpResponse(ctx, w, result)
return
}
next(w, r)
}
}