identity-service/internal/app/handler/handler.go

84 lines
1.6 KiB
Go
Raw Normal View History

2022-06-19 15:19:05 +02:00
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()))
}