lot of new functionalities
This commit is contained in:
31
servers/web/protected_api/handler.go
Normal file
31
servers/web/protected_api/handler.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package protected_api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/core/application"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
cfg *viper.Viper
|
||||
applicationHandler *application.ApplicationHandler
|
||||
}
|
||||
|
||||
func NewHandler(cfg *viper.Viper, applicationHandler *application.ApplicationHandler) *Handler {
|
||||
return &Handler{
|
||||
cfg: cfg,
|
||||
applicationHandler: applicationHandler,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ApiKeyMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
apiKey := r.Header.Get("X-API-Key")
|
||||
if apiKey != h.cfg.GetString("services.api.api_key") {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
39
servers/web/protected_api/users.go
Normal file
39
servers/web/protected_api/users.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package protected_api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (h *Handler) UsersHTTPHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var user storage.Account
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&user)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("could not read account input")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.applicationHandler.RegisterUser(r.Context(), user)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error registering user")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"user_id": result.UserID,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user