reCoreD-UI/server/route.go

99 lines
2.5 KiB
Go
Raw Normal View History

2024-04-03 09:05:12 +00:00
package server
import (
2024-04-13 02:30:02 +00:00
"net/http"
2024-04-03 09:05:12 +00:00
"path"
2024-04-09 08:28:18 +00:00
"reCoreD-UI/controllers"
2024-04-03 09:05:12 +00:00
"github.com/gin-gonic/gin"
2024-04-07 02:07:26 +00:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2024-04-03 09:05:12 +00:00
"github.com/sirupsen/logrus"
2024-04-13 02:30:02 +00:00
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
2024-04-03 09:05:12 +00:00
)
2024-04-07 13:07:20 +00:00
const (
2024-04-13 02:30:02 +00:00
apiPrefix = "/api"
metricPrefix = "/metrics"
swaggerPrefix = "/swagger"
2024-04-07 13:07:20 +00:00
)
2024-04-03 09:05:12 +00:00
func (s *Server) setupRoute() {
2024-04-09 08:28:18 +00:00
username, password, err := controllers.GetAdmin()
2024-04-03 09:05:12 +00:00
if err != nil {
logrus.Fatal(err)
}
2024-04-19 01:44:37 +00:00
logrus.Debugf("got %s:%s", username, password)
2024-04-03 09:05:12 +00:00
2024-04-19 01:44:37 +00:00
server := s.webServer.Group(s.prefix)
swaggerHandler := server
if s.debug {
swaggerHandler.GET(path.Join(swaggerPrefix, "*any"), ginSwagger.WrapHandler(swaggerfiles.Handler))
} else {
swaggerHandler.GET(path.Join(swaggerPrefix, "*any"), func(ctx *gin.Context) {
ctx.HTML(http.StatusNotFound, "", nil)
})
}
2024-04-13 02:30:02 +00:00
2024-04-19 01:44:37 +00:00
controllers.RegisterMetrics()
metricHandler := server
2024-04-07 13:07:20 +00:00
metricHandler.GET(metricPrefix, func(ctx *gin.Context) {
2024-04-09 08:28:18 +00:00
if err := controllers.RefreshMetrics(); err != nil {
2024-04-07 02:07:26 +00:00
logrus.Error(err)
}
promhttp.Handler().ServeHTTP(ctx.Writer, ctx.Request)
})
2024-04-03 09:05:12 +00:00
2024-04-19 01:44:37 +00:00
apiHandler := server
2024-04-07 02:07:26 +00:00
2024-04-07 13:07:20 +00:00
groupV1 := apiHandler.Group(apiPrefix, gin.BasicAuth(gin.Accounts{
2024-04-07 02:07:26 +00:00
username: password,
2024-04-19 01:44:37 +00:00
}), func(ctx *gin.Context) {
_, ok := ctx.Get(gin.AuthUserKey)
if !ok {
ctx.AbortWithStatusJSON(http.StatusUnauthorized, Response{
Succeed: false,
})
}
}).Group("/v1")
2024-04-03 09:05:12 +00:00
domains := groupV1.Group("/domains")
domains.
2024-04-09 13:16:19 +00:00
GET("/", getDomains).
POST("/", createDomain).
PUT("/", updateDomain).
DELETE("/:id", deleteDomain)
2024-04-03 09:05:12 +00:00
records := groupV1.Group("/records")
records.
2024-04-09 13:16:19 +00:00
GET("/:domain", getRecords).
POST("/:domain", createRecord).
POST("/:domain/bulk", createRecords).
PUT("/:domain", updateRecord).
DELETE("/:domain/:id", deleteRecord)
2024-04-03 09:05:12 +00:00
2024-04-19 01:44:37 +00:00
/*server := s.webServer.Group(s.prefix)
server.Use(apiHandler.HandleContext, metricHandler.HandleContext, func(ctx *gin.Context) {
2024-04-03 09:05:12 +00:00
uri := ctx.Request.RequestURI
logrus.Debug(uri)
2024-04-07 13:07:20 +00:00
switch {
case strings.HasPrefix(uri, path.Join(s.prefix, apiPrefix)):
2024-04-19 01:44:37 +00:00
//apiHandler.HandleContext(ctx)
2024-04-07 13:07:20 +00:00
case strings.HasPrefix(uri, path.Join(s.prefix, metricPrefix)):
2024-04-19 01:44:37 +00:00
//metricHandler.HandleContext(ctx)
2024-04-13 02:30:02 +00:00
case strings.HasPrefix(uri, path.Join(s.prefix, swaggerPrefix)):
if s.debug {
swaggerHandler.HandleContext(ctx)
} else {
ctx.HTML(http.StatusNotFound, "", nil)
}
2024-04-07 13:07:20 +00:00
default:
2024-04-07 02:07:26 +00:00
staticFileHandler()(ctx)
2024-04-03 09:05:12 +00:00
}
2024-04-19 01:44:37 +00:00
})*/
server.GET("/", staticFileHandler())
server.GET("/assets/*any", staticFileHandler())
2024-04-03 09:05:12 +00:00
}