go-gallery/routers/routes/routes.go

63 lines
1.7 KiB
Go
Raw Normal View History

2018-04-10 10:32:34 +00:00
package routes
import (
2018-04-12 09:41:21 +00:00
"git.trj.tw/golang/go-gallery/models"
2018-04-17 09:16:03 +00:00
"git.trj.tw/golang/go-gallery/modules/context"
2018-04-24 09:49:58 +00:00
"git.trj.tw/golang/go-gallery/modules/middleware"
2018-04-17 09:16:03 +00:00
"git.trj.tw/golang/go-gallery/routers/account"
2018-04-24 09:49:58 +00:00
"git.trj.tw/golang/go-gallery/routers/album"
2018-04-24 10:23:21 +00:00
"github.com/gin-contrib/cors"
2018-04-10 10:32:34 +00:00
"github.com/gin-gonic/gin"
)
// NewServ - get new service
func NewServ() *gin.Engine {
2018-04-17 09:16:03 +00:00
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
2018-04-24 10:23:21 +00:00
r.Use(cors.Default())
2018-04-10 10:32:34 +00:00
return r
}
2018-04-11 09:27:48 +00:00
// SetDefaultRoutes -
2018-04-10 10:32:34 +00:00
func SetDefaultRoutes(r *gin.Engine) {
r.GET("/", func(c *gin.Context) {
2018-04-12 09:41:21 +00:00
accs, err := models.GetAllAccount()
if err != nil {
c.JSON(500, gin.H{
"message": "db error",
})
return
}
c.JSON(200, accs)
2018-04-10 10:32:34 +00:00
})
2018-04-13 09:16:21 +00:00
api := r.Group("/api")
{
api.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": 200,
"message": "Success",
})
})
2018-04-17 09:16:03 +00:00
}
accountAPI := api.Group("/account")
{
accountAPI.POST("/login", context.PatchContext(account.UserLogin))
2018-04-18 07:05:37 +00:00
accountAPI.POST("/logout", context.PatchContext(account.UserLogout))
2018-04-18 10:21:29 +00:00
accountAPI.POST("/signup", context.PatchContext(account.UserSignup))
2018-04-13 09:16:21 +00:00
}
2018-04-24 09:49:58 +00:00
api.GET("/albums", context.PatchContext(middleware.VerifyToken), context.PatchContext(album.GetAllAlbums))
albumAPI := api.Group("/album", context.PatchContext(middleware.VerifyToken))
{
2018-04-25 07:50:45 +00:00
albumAPI.POST("/", context.PatchContext(album.CreateAlbum))
2018-05-08 08:19:51 +00:00
albumAPI.GET("/:album", context.PatchContext(album.GetAlbum))
albumAPI.PUT("/:album", context.PatchContext(album.UpdateAlbum))
albumAPI.DELETE("/:album", context.PatchContext(album.DeleteAlbum))
2018-04-24 09:49:58 +00:00
}
2018-04-30 02:13:01 +00:00
photoAPI := albumAPI.Group("/:album/photo", context.PatchContext(middleware.VerifyToken), context.PatchContext(middleware.GetAlbumToNext))
{
photoAPI.GET("/", context.PatchContext(album.GetPhoto))
}
2018-04-10 10:32:34 +00:00
}