SetRoutes sets up all gin HTTP API endpoints that can be called by front end
(r *gin.Engine)
| 41 | |
| 42 | // SetRoutes sets up all gin HTTP API endpoints that can be called by front end |
| 43 | func SetRoutes(r *gin.Engine) { |
| 44 | r.Use(cors.New(cors.Config{ |
| 45 | AllowOrigins: []string{"*"}, |
| 46 | AllowMethods: []string{"GET", "POST", "DELETE", "PUT", "HEAD", "OPTIONS"}, |
| 47 | AllowHeaders: []string{"Origin", "authorization", "content-type"}, |
| 48 | ExposeHeaders: []string{"Content-Length"}, |
| 49 | AllowCredentials: true, |
| 50 | MaxAge: 12 * time.Hour, |
| 51 | })) |
| 52 | |
| 53 | r.GET("/", meta.GUI) |
| 54 | r.GET("/version", meta.GetVersion) |
| 55 | r.GET("/healthz", meta.GetHealthz) |
| 56 | |
| 57 | // use ginSwagger middleware to serve the API docs |
| 58 | if config.Global.ZincSwaggerEnable { |
| 59 | r.GET("/swagger", func(c *gin.Context) { |
| 60 | c.Redirect(http.StatusMovedPermanently, "/swagger/index.html") |
| 61 | }) |
| 62 | r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) |
| 63 | } |
| 64 | front, err := zincsearch.GetFrontendAssets() |
| 65 | if err != nil { |
| 66 | log.Err(err) |
| 67 | } |
| 68 | |
| 69 | // UI |
| 70 | HTTPCacheForUI(r) |
| 71 | r.StaticFS("/ui/", http.FS(front)) |
| 72 | r.NoRoute(func(c *gin.Context) { |
| 73 | log.Error(). |
| 74 | Str("method", c.Request.Method). |
| 75 | Int("code", 404). |
| 76 | Int("took", 0). |
| 77 | Msg(c.Request.RequestURI) |
| 78 | |
| 79 | if strings.HasPrefix(c.Request.RequestURI, "/ui/") { |
| 80 | path := strings.TrimPrefix(c.Request.RequestURI, "/ui/") |
| 81 | locationPath := strings.Repeat("../", strings.Count(path, "/")) |
| 82 | c.Status(http.StatusFound) |
| 83 | c.Writer.Header().Set("Location", "./"+locationPath) |
| 84 | } |
| 85 | }) |
| 86 | |
| 87 | // auth |
| 88 | r.POST("/api/login", auth.Login) |
| 89 | r.POST("/api/user", AuthMiddleware("auth.CreateUpdateUser"), auth.CreateUpdateUser) |
| 90 | r.PUT("/api/user", AuthMiddleware("auth.CreateUpdateUser"), auth.CreateUpdateUser) |
| 91 | r.DELETE("/api/user/:id", AuthMiddleware("auth.DeleteUser"), auth.DeleteUser) |
| 92 | r.GET("/api/user", AuthMiddleware("auth.ListUser"), auth.ListUser) |
| 93 | r.GET("/api/permissions", AuthMiddleware("auth.ListPermissions"), auth.ListPermissions) |
| 94 | r.GET("/api/role", AuthMiddleware("auth.ListRole"), auth.ListRole) |
| 95 | r.POST("/api/role", AuthMiddleware("auth.CreateUpdateRole"), auth.CreateUpdateRole) |
| 96 | r.PUT("/api/role", AuthMiddleware("auth.CreateUpdateRole"), auth.CreateUpdateRole) |
| 97 | r.DELETE("/api/role/:id", AuthMiddleware("auth.DeleteRole"), auth.DeleteRole) |
| 98 | |
| 99 | // index |
| 100 | r.GET("/api/index", AuthMiddleware("index.List"), index.List) |
no test coverage detected