🎨 Refactor basic server structure
These changes refactor the router handling into its own package to keep main.go clean. Also API handlers are here refactored to their corresponding files.
This commit is contained in:
51
main.go
51
main.go
@ -19,15 +19,9 @@ package main
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.beisel.it/florian/hostname-service/api"
|
||||
"git.beisel.it/florian/hostname-service/auth"
|
||||
"git.beisel.it/florian/hostname-service/config"
|
||||
"git.beisel.it/florian/hostname-service/db"
|
||||
"git.beisel.it/florian/hostname-service/docs"
|
||||
"git.beisel.it/florian/hostname-service/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
"git.beisel.it/florian/hostname-service/router"
|
||||
)
|
||||
|
||||
// @title Hostname Service API
|
||||
@ -54,48 +48,9 @@ func main() {
|
||||
log.Fatalf("Failed to load configuration: %v", err)
|
||||
}
|
||||
|
||||
gin.SetMode(gin.DebugMode)
|
||||
|
||||
db.Init(config.GlobalConfig.DatabaseFile)
|
||||
|
||||
router := gin.Default()
|
||||
|
||||
docs.SwaggerInfo.Host = "localhost:8080"
|
||||
docs.SwaggerInfo.BasePath = "/api/v1"
|
||||
|
||||
v1 := router.Group("/api/v1")
|
||||
{
|
||||
// public routes
|
||||
v1.POST("/login", auth.LoginHandler)
|
||||
|
||||
// Protected Routes
|
||||
authenticated := v1.Group("/").Use(middleware.Authenticate())
|
||||
{
|
||||
authenticated.GET("/hello", api.Helloworld)
|
||||
|
||||
// Create Host
|
||||
authenticated.POST("/:category", func(c *gin.Context) {
|
||||
api.CreateOrUpdateHostname(c, false)
|
||||
})
|
||||
|
||||
// Get Host Details
|
||||
authenticated.GET("/:category/:hostname", api.GetHostnameByCategoryAndName)
|
||||
|
||||
// Update Host
|
||||
authenticated.PUT("/:category/:oldhostname", func(c *gin.Context) {
|
||||
api.CreateOrUpdateHostname(c, true)
|
||||
})
|
||||
|
||||
// Delete Host
|
||||
authenticated.DELETE("/:category/:hostname", api.DeleteHostname)
|
||||
|
||||
// List Hostnames
|
||||
authenticated.GET("/:category", api.ListHostnamesByCategory)
|
||||
}
|
||||
}
|
||||
|
||||
// Swagger endpoint
|
||||
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
||||
router := router.New()
|
||||
router.Run(":8080")
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user