reCoreD-UI/server/route.go

67 lines
1.5 KiB
Go
Raw Normal View History

2024-04-03 09:05:12 +00:00
package server
import (
"path"
2024-04-09 08:28:18 +00:00
"reCoreD-UI/controllers"
2024-04-03 09:05:12 +00:00
"strings"
"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-07 13:07:20 +00:00
const (
apiPrefix = "/api"
metricPrefix = "/metrics"
)
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-07 13:07:20 +00:00
metricHandler := gin.New()
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
apiHandler := gin.New()
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,
})).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-07 13:07:20 +00:00
server := s.webServer.Group(s.prefix)
2024-04-03 09:05:12 +00:00
server.Use(func(ctx *gin.Context) {
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-03 09:05:12 +00:00
apiHandler.HandleContext(ctx)
2024-04-07 13:07:20 +00:00
case strings.HasPrefix(uri, path.Join(s.prefix, metricPrefix)):
metricHandler.HandleContext(ctx)
default:
2024-04-07 02:07:26 +00:00
staticFileHandler()(ctx)
2024-04-03 09:05:12 +00:00
}
})
}