[feature] Merged with api-prototype
This commit is contained in:
35
internal/app/handler/auth.go
Normal file
35
internal/app/handler/auth.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
def "git.pbiernat.dev/egommerce/application/services/identity/internal/app/definition"
|
||||
"git.pbiernat.dev/egommerce/application/services/identity/internal/app/service"
|
||||
)
|
||||
|
||||
var AuthLoginHandler *Handler
|
||||
|
||||
func init() {
|
||||
AuthLoginHandler = &Handler{
|
||||
Handle: AuthLoginHandlerFunc,
|
||||
Request: &def.AuthLoginRequest{},
|
||||
Response: &def.AuthLoginResponse{},
|
||||
}
|
||||
}
|
||||
|
||||
func AuthLoginHandlerFunc(h *Handler, w http.ResponseWriter) (interface{}, int, error) {
|
||||
var req = h.Request.(*def.AuthLoginRequest)
|
||||
// u := entity.TestUser
|
||||
|
||||
token, err := service.AuthService.Login(req)
|
||||
if err != nil {
|
||||
return nil, http.StatusUnauthorized, err
|
||||
}
|
||||
|
||||
service.AuthService.SetCookie(w, service.AuthService.TokenCookieName, token)
|
||||
// service.AuthService.SetCookie(w, service.AuthService.RefreshTokenCookieName, refreshTtoken)
|
||||
|
||||
// log.Println("user:", u, "req:", token, "err:", err)
|
||||
|
||||
return nil, http.StatusOK, nil
|
||||
}
|
||||
15
internal/app/handler/error.go
Normal file
15
internal/app/handler/error.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package handler
|
||||
|
||||
import "net/http"
|
||||
|
||||
type NotFoundHandler struct{}
|
||||
|
||||
func (NotFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
encodeResponse(w, &response{http.StatusNotFound, "Path " + r.RequestURI + " not found"}, nil)
|
||||
}
|
||||
|
||||
type MethodNotAllowedHandler struct{}
|
||||
|
||||
func (MethodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
encodeResponse(w, &response{http.StatusMethodNotAllowed, "Method Not Allowed: " + r.Method}, nil)
|
||||
}
|
||||
83
internal/app/handler/handler.go
Normal file
83
internal/app/handler/handler.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
def "git.pbiernat.dev/egommerce/application/services/identity/internal/app/definition"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
type Env struct {
|
||||
Addr string
|
||||
DB *pgxpool.Pool
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
*Env
|
||||
Handle HandlerFunc
|
||||
Request interface{}
|
||||
Response interface{}
|
||||
Params Set
|
||||
}
|
||||
|
||||
type HandlerFunc func(h *Handler, w http.ResponseWriter) (interface{}, int, error)
|
||||
|
||||
type Set map[string]string
|
||||
|
||||
type response struct {
|
||||
Status int
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
func Init(e *Env, h *Handler) *Handler {
|
||||
// return &Handler{e, h.Handle, h.Request, h.Response, Set{}}
|
||||
h.Env = e
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if err := decodeRequestData(r, h.Request); err != nil {
|
||||
log.Println("Decode request data error:", err.Error())
|
||||
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
h.Params = mux.Vars(r)
|
||||
res, code, err := h.Handle(h, w)
|
||||
|
||||
encodeResponse(w, &response{code, res}, err)
|
||||
}
|
||||
|
||||
func decodeRequestData(r *http.Request, v interface{}) error {
|
||||
buf, _ := ioutil.ReadAll(r.Body)
|
||||
rdr := ioutil.NopCloser(bytes.NewReader(buf))
|
||||
r.Body = ioutil.NopCloser(bytes.NewReader(buf))
|
||||
|
||||
json.NewDecoder(rdr).Decode(&v)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func encodeResponse(w http.ResponseWriter, res *response, err error) {
|
||||
if err != nil {
|
||||
encodeError(w, res.Status, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(res.Status)
|
||||
if res.Data != nil {
|
||||
json.NewEncoder(w).Encode(res.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func encodeError(w http.ResponseWriter, status int, e error) {
|
||||
w.WriteHeader(status)
|
||||
|
||||
json.NewEncoder(w).Encode(def.Error(e.Error()))
|
||||
}
|
||||
40
internal/app/handler/health_check.go
Normal file
40
internal/app/handler/health_check.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var HealthCheckHandler *Handler
|
||||
|
||||
func init() {
|
||||
HealthCheckHandler = &Handler{
|
||||
Handle: HealthCheckHandlerFunc,
|
||||
Request: &HealthCheckRequest{},
|
||||
Response: &HealthCheckResponse{},
|
||||
}
|
||||
}
|
||||
|
||||
type HealthCheckRequest struct {
|
||||
}
|
||||
|
||||
type HealthCheckResponse struct {
|
||||
Status string `json:"status"`
|
||||
Data *HealthCheckResponseBody `json:"data"`
|
||||
}
|
||||
|
||||
type HealthCheckResponseBody struct {
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func HealthCheckHandlerFunc(_ *Handler, w http.ResponseWriter) (interface{}, int, error) {
|
||||
return &HealthCheckResponseBody{
|
||||
Message: "This is welcome health message. Everything seems to be alright ;)",
|
||||
}, http.StatusOK, nil
|
||||
|
||||
// return &HealthCheckResponse{
|
||||
// Status: http.StatusText(http.StatusOK),
|
||||
// Data: &HealthCheckResponseBody{
|
||||
// Message: "This is welcome health message. Everything seems to be alright ;)",
|
||||
// },
|
||||
// }, http.StatusOK, nil
|
||||
}
|
||||
Reference in New Issue
Block a user