feat: more reorganisation and keys endpoint
This commit is contained in:
@@ -19,14 +19,18 @@ func KickoffApi(logger *slog.Logger, env bootstrap.Environment, db *gorm.DB) {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
// For a nice looking logger:
|
||||
// r := gin.Default()
|
||||
// JSON logger: https://gin-gonic.com/en/docs/logging/structured-logging/
|
||||
// r := gin.Default() // default, this makes a nice looking log-trace but I want JSON
|
||||
r := gin.New()
|
||||
r.Use(middleware.SlogMiddleware(logger))
|
||||
r.Use(gin.Recovery())
|
||||
|
||||
if env.AuthenticationEnabled {
|
||||
slog.Debug("activating authentication middleware") // only log when actually doign the thing it logs to do
|
||||
r.Use(middleware.AuthMiddleware())
|
||||
}
|
||||
|
||||
api := r.Group("/api")
|
||||
routes.RegisterApiRoutes(api /*env,*/, db)
|
||||
routes.RegisterApiRoutes(api, db)
|
||||
|
||||
file := r.Group("/file")
|
||||
routes.RegisterFileRoutes(file, env, db)
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
)
|
||||
|
||||
func SlogMiddleware(logger *slog.Logger) gin.HandlerFunc {
|
||||
// Make a slog-looking logger, inspired by the gin docs themself
|
||||
// JSON logger: https://gin-gonic.com/en/docs/logging/structured-logging/
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
@@ -47,6 +49,8 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
}
|
||||
|
||||
headerParts := strings.Split(authorizationHeader, " ")
|
||||
// The header must be a specific format, 0 being the bearer text and 1 being the token itself, making it 2 pieces total
|
||||
// In the following if statement we verify both parts if the part after Bearer is empty its only 1 part for example
|
||||
if len(headerParts) != 2 || headerParts[0] != "Bearer" {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, response.BasicResponse{
|
||||
Msg: "Authorization header is invalid",
|
||||
@@ -54,7 +58,6 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
givenKey := headerParts[1]
|
||||
slog.Info(givenKey)
|
||||
//givenKey := headerParts[1]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,19 @@ import (
|
||||
|
||||
func RegisterApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm.DB) {
|
||||
// prefix: api
|
||||
|
||||
api.GET("/keys", func(c *gin.Context) {
|
||||
|
||||
})
|
||||
|
||||
api.POST("/keys", func(c *gin.Context) {
|
||||
|
||||
})
|
||||
|
||||
api.DELETE("/keys", func(c *gin.Context) {
|
||||
|
||||
})
|
||||
|
||||
// Display the information on what is going on at the moment
|
||||
api.GET("/command", func(c *gin.Context) {
|
||||
state, err := database.GetState(db)
|
||||
|
||||
Reference in New Issue
Block a user