game7 api
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"nova_task/internal/consts"
|
||||
"nova_task/internal/model"
|
||||
"nova_task/internal/pkg/errs"
|
||||
"nova_task/internal/types"
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
|
||||
type ApiKeyCheckMiddleware struct {
|
||||
conf model.NhSystemConfigModel
|
||||
user model.NhUserModel
|
||||
}
|
||||
|
||||
func NewApiKeyCheckMiddleware(conf model.NhSystemConfigModel) *ApiKeyCheckMiddleware {
|
||||
@@ -20,24 +19,22 @@ func NewApiKeyCheckMiddleware(conf model.NhSystemConfigModel) *ApiKeyCheckMiddle
|
||||
func (m *ApiKeyCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
key, err := m.conf.GetCarvApiKey(ctx)
|
||||
key, err := m.conf.GetApiKey(ctx, consts.CarvApiKey)
|
||||
if err != nil {
|
||||
if !errors.Is(err, model.ErrNotFound) {
|
||||
result := types.CarvResult{
|
||||
Error: &types.Error{
|
||||
Code: int(errs.ErrDatabaseOperate),
|
||||
Message: "api key config not exist",
|
||||
},
|
||||
}
|
||||
errs.WriteHttpResponse(ctx, w, result)
|
||||
return
|
||||
result := types.CarvResult{
|
||||
Error: &types.Error{
|
||||
Code: int(errs.ErrDatabaseOperate),
|
||||
Message: "api key config not exist",
|
||||
},
|
||||
}
|
||||
errs.WriteHttpResponse(ctx, w, result)
|
||||
return
|
||||
}
|
||||
apiKey := r.Header.Get("x-api-key")
|
||||
if apiKey == "" || apiKey != key {
|
||||
result := types.CarvResult{
|
||||
Error: &types.Error{
|
||||
Code: int(errs.ErrInvalidApiKey),
|
||||
Code: int(errs.ErrUnauthorized),
|
||||
Message: "invalid api key",
|
||||
},
|
||||
}
|
||||
|
||||
36
internal/middleware/game7apikeycheck_middleware.go
Normal file
36
internal/middleware/game7apikeycheck_middleware.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"nova_task/internal/consts"
|
||||
"nova_task/internal/model"
|
||||
"nova_task/internal/pkg/errs"
|
||||
)
|
||||
|
||||
type Game7ApiKeyCheckMiddleware struct {
|
||||
conf model.NhSystemConfigModel
|
||||
}
|
||||
|
||||
func NewGame7ApiKeyCheckMiddleware(conf model.NhSystemConfigModel) *Game7ApiKeyCheckMiddleware {
|
||||
return &Game7ApiKeyCheckMiddleware{conf: conf}
|
||||
}
|
||||
|
||||
func (m *Game7ApiKeyCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
key, err := m.conf.GetApiKey(ctx, consts.Game7ApiKey)
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user