diff --git a/hades.yaml b/hades.yaml new file mode 100644 index 0000000..961b522 --- /dev/null +++ b/hades.yaml @@ -0,0 +1,34 @@ +--- +app: + name space: + name: + +api: + title: title + description: description + email: aespin@gcelsa.com + version: 1.0 + base path: /api/v1 + +types: + - name: CreateUserReq + - name: CreateUSerRes + +services: + - name: CreateUser + request: CreateUserReq + response: CreateUserRes + +server: + - endpoints: + - name: CreateUser + url: /user/create + request: CreateUserReq + response: CreateUserRes + summary: create user + description: create user + handler: CreateUserHandler + - handlers: + - name: CreateUser + request: CreateUserReq + response: CreateUserRes \ No newline at end of file diff --git a/templates/server.templ b/templates/server.templ new file mode 100644 index 0000000..f8da7cb --- /dev/null +++ b/templates/server.templ @@ -0,0 +1,84 @@ +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, + } +} diff --git a/templates/service.templ b/templates/service.templ new file mode 100644 index 0000000..0357277 --- /dev/null +++ b/templates/service.templ @@ -0,0 +1,39 @@ +package service + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "os" + "regexp" + "time" + + + "{{ App.NameSpace }}/internal/types" + "golang.org/x/crypto/bcrypt" +) + +{{ range .Services}} +type IService interface { + {{ .Name}}(ctx context.Context, req types.{{ .Request}}) (types.{{ .Response}}, error) +} +{{ end }} + +type service struct {} + +{{ range .Services}} +func (s *service) {{ .Name}}(ctx context.Context, req types.{{ .Request}}) (types.{{ .Response}}, error) { + // create business logic + + return types.{{ .Response }}{ + + }, nil +} +{{ end }} + +func NewService() IService { + return &service{ + } +} \ No newline at end of file diff --git a/types/hades.go b/types/hades.go new file mode 100644 index 0000000..1dbeb27 --- /dev/null +++ b/types/hades.go @@ -0,0 +1,28 @@ +package types + +type Hades struct { + App struct { + NameSpace interface{} `yaml:"name space"` + Name interface{} `yaml:"name"` + } `yaml:"app"` + API interface{} `yaml:"api"` + Types []struct { + Name string `yaml:"name"` + } `yaml:"types"` + Services []struct { + Name string `yaml:"name"` + Request string `yaml:"request"` + Response string `yaml:"response"` + } `yaml:"services"` + Server []struct { + Endpoints []struct { + Name string `yaml:"name"` + URL string `yaml:"url"` + Request string `yaml:"request"` + Response string `yaml:"response"` + Summary string `yaml:"summary"` + Description string `yaml:"description"` + Handler string `yaml:"handler"` + } `yaml:"endpoints"` + } `yaml:"server"` +}