37 lines
890 B
Go
37 lines
890 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"booklib/internal/config"
|
|
"booklib/internal/redispkg"
|
|
"booklib/internal/store"
|
|
)
|
|
|
|
func NewRouter(cfg *config.Config, st *store.Store, rdb *redispkg.R) *gin.Engine {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
a := &api{cfg: cfg, st: st, rdb: rdb}
|
|
r := gin.New()
|
|
r.Use(gin.Recovery())
|
|
g := r.Group("/api")
|
|
g.GET("/healthz", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
|
|
g.POST("/auth/login", a.login)
|
|
|
|
p := g.Group("", a.authMw())
|
|
p.GET("/auth/me", a.me)
|
|
|
|
users := p.Group("/users", a.adminOnly())
|
|
users.GET("", a.listUsers)
|
|
users.POST("", a.createUser)
|
|
users.DELETE("/:id", a.deleteUser)
|
|
|
|
libs := p.Group("/libraries")
|
|
libs.GET("", a.listLibraries)
|
|
libs.POST("", a.adminOnly(), a.createLibrary)
|
|
libs.POST("/:id/scan", a.adminOnly(), a.scanLibrary)
|
|
libs.POST("/:id/upload", a.adminOnly(), a.upload)
|
|
return r
|
|
}
|