ci/cd test
This commit is contained in:
83
src/internal/app/handler/handler.go
Normal file
83
src/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/identity-service/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, e error) {
|
||||
if e != nil {
|
||||
encodeError(w, res.Status, e)
|
||||
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()))
|
||||
}
|
||||
Reference in New Issue
Block a user