83 lines
2.6 KiB
Go
83 lines
2.6 KiB
Go
// Copyright 2024 Florian Beisel
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package router
|
|
|
|
import (
|
|
"git.beisel.it/florian/hostname-service/api"
|
|
"git.beisel.it/florian/hostname-service/auth"
|
|
"git.beisel.it/florian/hostname-service/docs"
|
|
"git.beisel.it/florian/hostname-service/middleware"
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
swaggerFiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
// ... other necessary imports ...
|
|
)
|
|
|
|
func New() *gin.Engine {
|
|
gin.SetMode(gin.DebugMode)
|
|
|
|
router := gin.Default()
|
|
// // Configure CORS
|
|
config := cors.DefaultConfig()
|
|
config.AllowOrigins = []string{"http://localhost:3000", "http://localhost:8080", "*"} // Set to your frontend's URL
|
|
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
|
|
config.AllowHeaders = []string{"Origin", "Content-Type", "Authorization"}
|
|
router.Use(cors.New(config))
|
|
|
|
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)
|
|
|
|
// List available Rules
|
|
authenticated.GET("/rules", api.ListAvailableRules)
|
|
authenticated.GET("/rules/:rule", api.GetRuleStruct)
|
|
}
|
|
}
|
|
|
|
// Swagger endpoint
|
|
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
|
|
|
return router
|
|
}
|