484 lines
15 KiB
Go
484 lines
15 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"path"
|
|
"time"
|
|
|
|
"git.espin.casa/albert/cml04-mediciones-service/internal/service"
|
|
"git.espin.casa/albert/cml04-mediciones-service/internal/types"
|
|
"github.com/zc2638/swag"
|
|
"github.com/zc2638/swag/endpoint"
|
|
"github.com/zc2638/swag/option"
|
|
)
|
|
|
|
type Server struct {
|
|
api *swag.API
|
|
Url string
|
|
}
|
|
|
|
func GetToleranceEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/tolerance",
|
|
endpoint.BodyR(types.GetToleranciaReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.GetToleranciaRes{})),
|
|
endpoint.Summary("Info about product tolerances"),
|
|
endpoint.Description("Info about product tolerances"),
|
|
endpoint.Handler(GetToleranceHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func PostToleranceEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/tolerance/create",
|
|
endpoint.BodyR(types.CreateToleranceReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.CreateToleranceRes{})),
|
|
endpoint.Summary("create a new product tolerances"),
|
|
endpoint.Description("create a new product tolerances"),
|
|
endpoint.Handler(PostToleranceHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func GetProductionOrderEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/po",
|
|
endpoint.BodyR(types.GetProductionOrderReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.GetProductionOrderRes{})),
|
|
endpoint.Summary("get production order data"),
|
|
endpoint.Description("get production order data"),
|
|
endpoint.Handler(GetProductionOrderHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func GetProductionOrdersEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/pos",
|
|
endpoint.BodyR(types.GetProductionOrdersReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.GetProductionOrdersRes{})),
|
|
endpoint.Summary("get production order data"),
|
|
endpoint.Description("get production order data"),
|
|
endpoint.Handler(GetProductionOrdersHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func GetMedicionEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/medicion",
|
|
endpoint.BodyR(types.GetMedicionReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.GetMedicionRes{})),
|
|
endpoint.Summary("get medicion data"),
|
|
endpoint.Description("get medicion data"),
|
|
endpoint.Handler(GetMedicionHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func GetMedicionesEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/mediciones",
|
|
endpoint.BodyR(types.GetMedicionesReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.GetMedicionesRes{})),
|
|
endpoint.Summary("get mediciones data"),
|
|
endpoint.Description("get mediciones data"),
|
|
endpoint.Handler(GetMedicionesHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func PostMedicionesEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/medicion/create",
|
|
endpoint.BodyR(types.PostMedicionesReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.PostMedicionesRes{})),
|
|
endpoint.Summary("get mediciones data"),
|
|
endpoint.Description("get mediciones data"),
|
|
endpoint.Handler(PostMedicionesHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func GetUserEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/user",
|
|
endpoint.BodyR(types.GetUsuarioReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.GetUsuarioRes{})),
|
|
endpoint.Summary("Get user based on email primary key"),
|
|
endpoint.Description("Get user based on email primary key"),
|
|
endpoint.Handler(GetUserHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func CreateUserEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/user/create",
|
|
endpoint.BodyR(types.CreateUsuarioReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.CreateUsuarioRes{})),
|
|
endpoint.Summary("Create a new user"),
|
|
endpoint.Description("Create a new user"),
|
|
endpoint.Handler(CreateUserHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func ValidateUserEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/user/validate",
|
|
endpoint.BodyR(types.ValidarUsuarioReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.ValidarUsuarioRes{})),
|
|
endpoint.Summary("User validation"),
|
|
endpoint.Description("User validation"),
|
|
endpoint.Handler(ValidateUserHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func GetExcelEndPoint(svc service.IService) *swag.Endpoint {
|
|
return endpoint.New(http.MethodPost, "/excel/get",
|
|
endpoint.BodyR(types.GetExcelReq{}),
|
|
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.GetExcelRes{})),
|
|
endpoint.Summary("get excel mediciones"),
|
|
endpoint.Description("get excel mediciones"),
|
|
endpoint.Handler(GetExcelHandler(svc)),
|
|
)
|
|
}
|
|
|
|
func GetExcelHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.GetExcelReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
res, err := svc.GetExcel(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "get excel handler failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func ValidateUserHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.ValidarUsuarioReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
//
|
|
res, err := svc.ValidarUsuario(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "validate user failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func GetUserHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.GetUsuarioReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// get user from database
|
|
res, err := svc.GetUsuario(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "get user failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func CreateUserHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.CreateUsuarioReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// create user service call
|
|
res, err := svc.CreateUsuario(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "create user failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func PostMedicionesHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.PostMedicionesReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// post medicion
|
|
res, err := svc.PostMediciones(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "post medicion failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func GetMedicionesHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.GetMedicionesReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// get mediciones
|
|
res, err := svc.GetMediciones(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "get mediciones failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func GetMedicionHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.GetMedicionReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// get medicion
|
|
res, err := svc.GetMedicion(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "get medicion failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func GetProductionOrdersHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.GetProductionOrdersReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// get production orders
|
|
res, err := svc.GetProductionOrders(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "get production orders failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func GetToleranceHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.GetToleranciaReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// get tolerancia from database
|
|
res, err := svc.GetTolerance(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "get tolerancia failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func PostToleranceHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.CreateToleranceReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// post tolerance into database
|
|
res, err := svc.CreateTolerance(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "create tolerance for product failed", http.StatusInternalServerError)
|
|
return
|
|
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
// write response
|
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func GetProductionOrderHandler(svc service.IService) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// request body holder
|
|
req := types.GetProductionOrderReq{}
|
|
// decode json body request
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
|
return
|
|
}
|
|
//
|
|
res, err := svc.GetProductionOrder(r.Context(), req)
|
|
if err != nil {
|
|
http.Error(w, "get production order failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// set content type header
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// response
|
|
w.WriteHeader(http.StatusOK)
|
|
//
|
|
if err := json.NewEncoder(w).Encode(res); err != nil {
|
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func (s *Server) Start() error {
|
|
// set api handlers
|
|
for p, endpoints := range s.api.Paths {
|
|
http.DefaultServeMux.Handle(path.Join(s.api.BasePath, p), endpoints)
|
|
}
|
|
// set swagger handlers
|
|
http.DefaultServeMux.Handle("/swagger/json", s.api.Handler())
|
|
patterns := swag.UIPatterns("/swagger/ui")
|
|
for _, pattern := range patterns {
|
|
http.DefaultServeMux.Handle(pattern, swag.UIHandler("/swagger/ui", "/swagger/json", true))
|
|
}
|
|
// Crear un servidor HTTP
|
|
server := http.Server{
|
|
Addr: s.Url,
|
|
ReadTimeout: 5 * time.Second, // time limit for reading
|
|
WriteTimeout: 5 * time.Second, // time limit for writting
|
|
IdleTimeout: 5 * time.Second,
|
|
}
|
|
// start api
|
|
go func() {
|
|
if err := server.ListenAndServe(); err != nil {
|
|
panic(err)
|
|
}
|
|
}()
|
|
return nil
|
|
}
|
|
|
|
func NewServer(url string, svc service.IService) *Server {
|
|
// create new swag
|
|
api := swag.New(
|
|
option.Title("CELSA 4 Mediciones REST API Server 22"),
|
|
option.Description("This is an implementation of a REST API server for mediciones"),
|
|
option.ContactEmail("aespin@gcelsa.com"),
|
|
option.Version("1.0"),
|
|
option.BasePath("/api/v1"),
|
|
option.License("MIT", "https://opensource.org/license/mit"),
|
|
)
|
|
// add end points
|
|
api.AddEndpoint(
|
|
GetToleranceEndPoint(svc),
|
|
PostToleranceEndPoint(svc),
|
|
GetProductionOrderEndPoint(svc),
|
|
GetProductionOrdersEndPoint(svc),
|
|
PostMedicionesEndPoint(svc),
|
|
GetMedicionesEndPoint(svc),
|
|
GetMedicionEndPoint(svc),
|
|
GetUserEndPoint(svc),
|
|
CreateUserEndPoint(svc),
|
|
ValidateUserEndPoint(svc),
|
|
GetExcelEndPoint(svc),
|
|
)
|
|
return &Server{
|
|
api: api,
|
|
Url: url,
|
|
}
|
|
}
|