Skip to content

Commit 13b80ec

Browse files
Merge pull request #68 from samueldasilvadev/creating-auto-generated-docs
chore: creating docs generator
2 parents a08859d + 2ed7b34 commit 13b80ec

File tree

24 files changed

+1311
-43
lines changed

24 files changed

+1311
-43
lines changed

.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ tmp_dir = "tmp"
1313
exclude_unchanged = true
1414
follow_symlink = false
1515
full_bin = ""
16-
include_dir = []
16+
include_dir = ["docs"]
1717
include_ext = ["go", "env"]
1818
kill_delay = "0s"
1919
log = "build-errors.log"

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
update_docs:
2+
docker exec zord-http swag init -g ./cmd/http/main.go --parseDependency --propertyStrategy pascalcase

cmd/http/handlers/dummy.go

100644100755
Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"go-skeleton/internal/application/domain/dummy"
55
"go-skeleton/internal/application/providers/filters"
66
"go-skeleton/internal/application/providers/pagination"
7+
_ "go-skeleton/internal/application/services"
78
dummyCreate "go-skeleton/internal/application/services/dummy/CREATE"
89
dummyDelete "go-skeleton/internal/application/services/dummy/DELETE"
910
dummyEdit "go-skeleton/internal/application/services/dummy/EDIT"
@@ -36,13 +37,23 @@ func NewDummyHandlers(reg *registry.Registry) *DummyHandlers {
3637
}
3738
}
3839

40+
// HandleGetDummy Get Dummy
41+
// @Summary Get a Dummy
42+
// @Tags Dummy
43+
// @Accept json
44+
// @Produce json
45+
// @Param dummy_id path string true "Dummy ID"
46+
// @Success 200 {object} dummyGet.Response
47+
// @Failure 400 {object} services.Error
48+
// @Failure 404 {object} services.Error
49+
// @Failure 500 {object} services.Error
50+
// @Router /dummy/{dummy_id} [get]
3951
func (hs *DummyHandlers) HandleGetDummy(context echo.Context) error {
4052
s := dummyGet.NewService(hs.logger, hs.DummyRepository)
4153
data := new(dummyGet.Data)
4254

4355
if errors := context.Bind(data); errors != nil {
44-
s.CustomError(http.StatusBadRequest, errors)
45-
return context.JSON(s.Error.Status, s.Error)
56+
return context.JSON(422, errors)
4657
}
4758

4859
s.Execute(
@@ -56,13 +67,23 @@ func (hs *DummyHandlers) HandleGetDummy(context echo.Context) error {
5667
return context.JSON(http.StatusOK, response)
5768
}
5869

70+
// HandleCreateDummy Create Dummy
71+
// @Summary Create Dummy
72+
// @Tags Dummy
73+
// @Accept json
74+
// @Produce json
75+
// @Param request body dummyCreate.Data true "body model"
76+
// @Success 200 {object} dummyCreate.Response
77+
// @Failure 400 {object} services.Error
78+
// @Failure 404 {object} services.Error
79+
// @Failure 500 {object} services.Error
80+
// @Router /dummy [post]
5981
func (hs *DummyHandlers) HandleCreateDummy(context echo.Context) error {
6082
s := dummyCreate.NewService(hs.logger, hs.DummyRepository, hs.idCreator)
6183
data := new(dummyCreate.Data)
6284

6385
if errors := context.Bind(data); errors != nil {
64-
s.CustomError(http.StatusBadRequest, errors)
65-
return context.JSON(s.Error.Status, s.Error)
86+
return context.JSON(http.StatusBadRequest, errors)
6687
}
6788

6889
s.Execute(
@@ -76,17 +97,29 @@ func (hs *DummyHandlers) HandleCreateDummy(context echo.Context) error {
7697
return context.JSON(http.StatusCreated, response)
7798
}
7899

100+
// HandleEditDummy Edit Dummy
101+
// @Summary Edit Dummy
102+
// @Tags Dummy
103+
// @Accept json
104+
// @Produce json
105+
// @Param dummy_id path string true "Dummy ID"
106+
// @Param request body dummyEdit.Data true "body model"
107+
// @Success 200 {object} dummyEdit.Response
108+
// @Failure 400 {object} services.Error
109+
// @Failure 404 {object} services.Error
110+
// @Failure 500 {object} services.Error
111+
// @Router /dummy/{dummy_id} [put]
79112
func (hs *DummyHandlers) HandleEditDummy(context echo.Context) error {
80113
s := dummyEdit.NewService(hs.logger, hs.DummyRepository)
81114
data := new(dummyEdit.Data)
82115

116+
id := context.Param("id")
83117
if errors := context.Bind(data); errors != nil {
84-
s.CustomError(http.StatusBadRequest, errors)
85-
return context.JSON(s.Error.Status, s.Error)
118+
return context.JSON(http.StatusBadRequest, errors)
86119
}
87120

88121
s.Execute(
89-
dummyEdit.NewRequest(data, hs.validator),
122+
dummyEdit.NewRequest(id, data, hs.validator),
90123
)
91124

92125
response, err := s.GetResponse()
@@ -96,6 +129,19 @@ func (hs *DummyHandlers) HandleEditDummy(context echo.Context) error {
96129
return context.JSON(http.StatusOK, response)
97130
}
98131

132+
// HandleListDummy List Dummy
133+
// @Summary List Dummy
134+
// @Tags Dummy
135+
// @Accept json
136+
// @Produce json
137+
// @Param page query int true "valid int"
138+
// @Param name query string false "value example: eql|lik,value"
139+
// @Param email query string false "value example: lik,value"
140+
// @Success 200 {object} dummyList.Response
141+
// @Failure 400 {object} services.Error
142+
// @Failure 404 {object} services.Error
143+
// @Failure 500 {object} services.Error
144+
// @Router /dummy [get]
99145
func (hs *DummyHandlers) HandleListDummy(context echo.Context) error {
100146
s := dummyList.NewService(
101147
hs.logger,
@@ -111,8 +157,7 @@ func (hs *DummyHandlers) HandleListDummy(context echo.Context) error {
111157
BindErrors()
112158

113159
if bindErr != nil {
114-
s.CustomError(http.StatusBadRequest, bindErr)
115-
return context.JSON(http.StatusBadRequest, s.Error)
160+
return context.JSON(http.StatusBadRequest, bindErr)
116161
}
117162

118163
f := filters.NewFilters()
@@ -128,13 +173,23 @@ func (hs *DummyHandlers) HandleListDummy(context echo.Context) error {
128173
return context.JSON(http.StatusOK, response)
129174
}
130175

176+
// HandleDeleteDummy Delete Dummy
177+
// @Summary Delete Dummy
178+
// @Tags Dummy
179+
// @Accept json
180+
// @Produce json
181+
// @Param dummy_id path string true "Dummy ID"
182+
// @Success 200 {object} dummyDelete.Response
183+
// @Failure 400 {object} services.Error
184+
// @Failure 404 {object} services.Error
185+
// @Failure 500 {object} services.Error
186+
// @Router /dummy/{dummy_id} [delete]
131187
func (hs *DummyHandlers) HandleDeleteDummy(context echo.Context) error {
132188
s := dummyDelete.NewService(hs.logger, hs.DummyRepository)
133189
data := new(dummyDelete.Data)
134190

135191
if errors := context.Bind(data); errors != nil {
136-
s.CustomError(http.StatusBadRequest, errors)
137-
return context.JSON(s.Error.Status, s.Error)
192+
return context.JSON(http.StatusBadRequest, errors)
138193
}
139194

140195
s.Execute(

cmd/http/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ var (
1212
reg *registry.Registry
1313
)
1414

15+
// @title Swagger Zord API
16+
// @version 1.0
17+
// @description This is the Zord backend server.
1518
func main() {
1619
cmd.Setup()
1720
serverInstance := server.NewServer(cmd.Reg, cmd.ApiPrefix)

cmd/http/routes/declarable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func GetRoutes(reg *registry.Registry) map[string]Declarable {
1717
//{{codeGen1}}
1818
routes := map[string]Declarable{
1919
"health": health,
20-
"dummy": dummyListRoutes,
20+
"dummy": dummyListRoutes,
2121
//{{codeGen2}}
2222
}
2323
return routes

cmd/http/routes/dummy.go

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func NewDummyRoutes(reg *registry.Registry) *DummyRoutes {
1818
}
1919
}
2020

21-
func (hs *DummyRoutes) DeclarePublicRoutes(server *echo.Group, apiPrefix string) {}
22-
2321
func (hs *DummyRoutes) DeclarePrivateRoutes(server *echo.Group, apiPrefix string) {
2422
server.GET(apiPrefix+"/dummy", hs.hand.HandleListDummy)
2523
server.GET(apiPrefix+"/dummy/:id", hs.hand.HandleGetDummy)
2624
server.POST(apiPrefix+"/dummy", hs.hand.HandleCreateDummy)
2725
server.PUT(apiPrefix+"/dummy/:id", hs.hand.HandleEditDummy)
2826
server.DELETE(apiPrefix+"/dummy/:id", hs.hand.HandleDeleteDummy)
2927
}
28+
29+
func (hs *DummyRoutes) DeclarePublicRoutes(server *echo.Group, apiPrefix string) {}

cmd/http/server/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package server
22

33
import (
44
"fmt"
5+
echoSwagger "github.com/swaggo/echo-swagger"
56
"go-skeleton/cmd/http/routes"
7+
"go-skeleton/docs"
68
"go-skeleton/pkg/config"
79
"go-skeleton/pkg/logger"
810
"go-skeleton/pkg/registry"
@@ -33,6 +35,12 @@ func (hs *Server) Start() {
3335
server.HideBanner = true
3436
server.HidePort = true
3537

38+
if hs.config.ReadConfig("ENVIRONMENT") == "development" {
39+
docs.SwaggerInfo.Host = "localhost:" + hs.config.ReadConfig("HTTP_PORT")
40+
docs.SwaggerInfo.BasePath = "/" + hs.config.ReadConfig("API_PREFIX")
41+
server.GET("/swagger/*", echoSwagger.WrapHandler)
42+
}
43+
3644
server.Use(middleware.Recover())
3745

3846
public := server.Group("")

docker-compose.yml renamed to compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3.7'
21
services:
32
zord-http:
43
build:
@@ -8,7 +7,7 @@ services:
87
env_file:
98
- .env
109
ports:
11-
- "9005:9000"
10+
- "9000:9000"
1211
command: air http
1312
depends_on:
1413
mysql:

docker/dev/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ ENV ATLAS_VERSION=v0.20.1-7793896-canary
1111
RUN curl -sSf https://atlasgo.sh | sh
1212

1313
RUN go mod download
14-
RUN go install github.com/cosmtrek/air@latest
14+
RUN go install github.com/air-verse/air@latest
15+
RUN go install github.com/swaggo/swag/cmd/swag@latest
16+
RUN apk add build-base

0 commit comments

Comments
 (0)