carv api logic
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"nova_task/internal/model"
|
||||
"nova_task/internal/pkg/errs"
|
||||
"nova_task/internal/types"
|
||||
)
|
||||
|
||||
type ApiKeyCheckMiddleware struct {
|
||||
@@ -17,16 +19,29 @@ func NewApiKeyCheckMiddleware(conf model.NhSystemConfigModel) *ApiKeyCheckMiddle
|
||||
|
||||
func (m *ApiKeyCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
key, err := m.conf.GetCarvApiKey(r.Context())
|
||||
ctx := r.Context()
|
||||
key, err := m.conf.GetCarvApiKey(ctx)
|
||||
if err != nil {
|
||||
if !errors.Is(err, model.ErrNotFound) {
|
||||
http.Error(w, "system error", http.StatusInternalServerError)
|
||||
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 {
|
||||
http.Error(w, "Invalid API key", http.StatusUnauthorized)
|
||||
result := types.CarvResult{
|
||||
Error: &types.Error{
|
||||
Code: int(errs.ErrInvalidApiKey),
|
||||
Message: "invalid api key",
|
||||
},
|
||||
}
|
||||
errs.WriteHttpResponse(ctx, w, result)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user