NewHandler creates a new Gin engine for the API. @title GoDoxy API @version 1.0 @description GoDoxy API @termsOfService https://github.com/yusing/godoxy/blob/main/LICENSE @contact.name Yusing @contact.url https://github.com/yusing/godoxy/issues @license.name MIT @lice
(requireAuth bool)
| 43 | // @externalDocs.description GoDoxy Docs |
| 44 | // @externalDocs.url https://docs.godoxy.dev |
| 45 | func NewHandler(requireAuth bool) *gin.Engine { |
| 46 | if !common.IsDebug { |
| 47 | gin.SetMode("release") |
| 48 | } |
| 49 | r := gin.New() |
| 50 | r.Use(ErrorHandler()) |
| 51 | r.Use(ErrorLoggingMiddleware()) |
| 52 | r.Use(NoCache()) |
| 53 | |
| 54 | log.Debug().Msg("gin codec json.API: " + reflect.TypeOf(json.API).Name()) |
| 55 | |
| 56 | r.GET("/api/v1/version", apiV1.Version) |
| 57 | |
| 58 | if auth.IsEnabled() && requireAuth { |
| 59 | v1Auth := r.Group("/api/v1/auth") |
| 60 | { |
| 61 | v1Auth.HEAD("/check", CSRFMiddleware(), authApi.Check) |
| 62 | v1Auth.POST("/login", CSRFMiddleware(), authApi.Login) |
| 63 | v1Auth.GET("/callback", authApi.Callback) |
| 64 | v1Auth.POST("/callback", CSRFMiddleware(), authApi.Callback) |
| 65 | v1Auth.POST("/logout", CSRFMiddleware(), authApi.Logout) |
| 66 | v1Auth.GET("/logout", authApi.Logout) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | v1 := r.Group("/api/v1") |
| 71 | if auth.IsEnabled() && requireAuth { |
| 72 | v1.Use(AuthMiddleware()) |
| 73 | v1.Use(CSRFMiddleware()) |
| 74 | } |
| 75 | if common.APISkipOriginCheck { |
| 76 | v1.Use(SkipOriginCheckMiddleware()) |
| 77 | } |
| 78 | { |
| 79 | // enable cache for favicon |
| 80 | v1.GET("/favicon", apiV1.FavIcon) |
| 81 | v1.GET("/health", apiV1.Health) |
| 82 | v1.GET("/icons", apiV1.Icons) |
| 83 | v1.GET("/stats", apiV1.Stats) |
| 84 | v1.GET("/events", apiV1.Events) |
| 85 | |
| 86 | route := v1.Group("/route") |
| 87 | { |
| 88 | route.GET("/list", routeApi.Routes) |
| 89 | route.GET("/:which", routeApi.Route) |
| 90 | route.GET("/providers", routeApi.Providers) |
| 91 | route.GET("/by_provider", routeApi.ByProvider) |
| 92 | route.POST("/playground", routeApi.Playground) |
| 93 | route.GET("/validate", routeApi.Validate) // websocket |
| 94 | route.POST("/validate", routeApi.Validate) |
| 95 | } |
| 96 | |
| 97 | file := v1.Group("/file") |
| 98 | { |
| 99 | file.GET("/list", fileApi.List) |
| 100 | file.GET("/content", fileApi.Get) |
| 101 | file.PUT("/content", fileApi.Set) |
| 102 | file.POST("/content", fileApi.Set) |