1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"net/http" |
5
|
|
|
|
6
|
|
|
"github.com/gin-gonic/contrib/static" |
7
|
|
|
"github.com/gin-gonic/gin" |
8
|
|
|
"github.com/rfinochi/golang-workshop-todo/docs" |
9
|
|
|
|
10
|
|
|
swaggerFiles "github.com/swaggo/files" |
11
|
|
|
ginSwagger "github.com/swaggo/gin-swagger" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
func (app *application) initRouter() { |
15
|
|
|
app.router = gin.Default() |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
func (app *application) addAPIRoutes() { |
19
|
|
|
if app.router != nil { |
20
|
|
|
app.router.Use(logRequestMiddleware(app.infoLog)) |
21
|
|
|
app.router.Use(revisionMiddleware(app.errorLog)) |
22
|
|
|
app.router.Use(requestIDMiddleware()) |
23
|
|
|
app.router.Use(tokenAuthMiddleware(app.errorLog)) |
24
|
|
|
app.router.Use(static.Serve("/", static.LocalFile("./ui/html", true))) |
25
|
|
|
|
26
|
|
|
api := app.router.Group("/api") |
27
|
|
|
api.GET("/", app.getItemsEndpoint) |
28
|
|
|
api.GET("/:id", app.getItemEndpoint) |
29
|
|
|
api.POST("/", app.postItemEndpoint) |
30
|
|
|
api.PUT("/", app.putItemEndpoint) |
31
|
|
|
api.PATCH("/:id", app.updateItemEndpoint) |
32
|
|
|
api.DELETE("/:id", app.deleteItemEndpoint) |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
func (app *application) addSwaggerRoutes() { |
37
|
|
|
if app.router != nil { |
38
|
|
|
docs.SwaggerInfo.Schemes = []string{"https"} |
39
|
|
|
app.router.GET("/api-docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) |
40
|
|
|
app.router.GET("/api-docs", func(c *gin.Context) { |
41
|
|
|
c.Redirect(http.StatusMovedPermanently, "./api-docs/index.html") |
42
|
|
|
}) |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|