wip
This commit is contained in:
parent
d884e25b70
commit
2aa3add007
34
hades.yaml
Normal file
34
hades.yaml
Normal file
@ -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
|
84
templates/server.templ
Normal file
84
templates/server.templ
Normal file
@ -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,
|
||||
}
|
||||
}
|
39
templates/service.templ
Normal file
39
templates/service.templ
Normal file
@ -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{
|
||||
}
|
||||
}
|
28
types/hades.go
Normal file
28
types/hades.go
Normal file
@ -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"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user