-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
47 lines (37 loc) · 1.21 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package routes
/*
Handles all the rounting and web requests coming in
*/
import (
"github.com/dolfelt/avatar-go/data"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
// Register creates and registers all the routes to the router
func Register(app *data.Application) *gin.Engine {
router := gin.New()
return configRouter(router, app)
}
func configRouter(router *gin.Engine, app *data.Application) *gin.Engine {
// GetRouter returns the router with all the routes defined
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(cors.New(getCORSConfig()))
// Get endpoints for displaying the avatar
router.GET("/:hash", read(app))
router.GET("/:hash/:size_or_backup", read(app))
router.GET("/:hash/:size_or_backup/:size", read(app))
// router.GET("/:hash/:backup/:size", read(app))
// router.GET("/:hash:[0-9a-f]{40}/:backup:[0-9a-f]{40}/:size", read(app))
router.GET("/", index())
// Head endpoint for determining if the avatar exists
router.HEAD("/:hash", exists(app))
authRouter := router.Group("/")
if !app.Debug {
authRouter.Use(AuthRequired())
}
// Options endpoint for available methods
authRouter.POST("/:hash", write(app))
authRouter.DELETE("/:hash", delete(app))
return router
}