feat: added API gateway
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"datarush/internal/gw/domain"
|
||||
"datarush/internal/gw/middleware"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func getUserIDFromContext(ctx context.Context) (string, error) {
|
||||
return middleware.GetUserIDFromContext(ctx)
|
||||
}
|
||||
|
||||
func getPathParam(r *http.Request, key string) string {
|
||||
vars := mux.Vars(r)
|
||||
return vars[key]
|
||||
}
|
||||
|
||||
func getQueryParam(r *http.Request, key string) string {
|
||||
return r.URL.Query().Get(key)
|
||||
}
|
||||
|
||||
func getQueryParamInt(r *http.Request, key string, defaultValue int) int {
|
||||
val := r.URL.Query().Get(key)
|
||||
if val == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
intVal, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return intVal
|
||||
}
|
||||
|
||||
func getQueryParamInt32(r *http.Request, key string, defaultValue int32) int32 {
|
||||
return int32(getQueryParamInt(r, key, int(defaultValue)))
|
||||
}
|
||||
|
||||
func getQueryParamBool(r *http.Request, key string) *bool {
|
||||
val := r.URL.Query().Get(key)
|
||||
if val == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
boolVal, err := strconv.ParseBool(val)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &boolVal
|
||||
}
|
||||
|
||||
type PingHandler struct{}
|
||||
|
||||
func NewPingHandler() *PingHandler {
|
||||
return &PingHandler{}
|
||||
}
|
||||
|
||||
func (h *PingHandler) Ping(w http.ResponseWriter, r *http.Request) {
|
||||
response := &domain.PingResponse{
|
||||
Message: "pong",
|
||||
Status: "ok",
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
Reference in New Issue
Block a user