package server import ( "encoding/json" "net/http" "path" "time" "{{ App.NameSpace }}/internal/service" "{{ App.NameSpace }}/internal/types" "github.com/zc2638/swag" "github.com/zc2638/swag/endpoint" "github.com/zc2638/swag/option" ) type Server struct { api *swag.API Url string } {{ range .Server.EndPoints }} func {{ .Name }}EndPoint(svc service.IService) *swag.Endpoint { return endpoint.New(http.MethodPost, "{{ .URL }}", endpoint.BodyR(types.{{ .Request{} }}), endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.{{ .Response{} }})), endpoint.Summary("{{ .Summary }}"), endpoint.Description(" {{ .Description }}"), endpoint.Handler({{ .Handler }}(svc)), ) } {{ end }} {{ range .Server.Handlers }} func {{ .Name }}Handler(svc service.IService) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // request body holder req := types.{{.Request{}}} // decode json body request if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, "body json decoding failed", http.StatusBadRequest) return } // TODO 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 } }) } {{ end }} 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( {{ range .Server.EndPoints }} {{ .Name}}(svc), {{ end }} ) return &Server{ api: api, Url: url, } }