Add rules routes

This commit is contained in:
Florian Beisel 2024-01-24 11:03:08 +01:00
parent 0247413a10
commit 57722116d6
Signed by: florian
GPG Key ID: 79ECA2E54996FF4D
5 changed files with 180 additions and 70 deletions

View File

@ -17,11 +17,19 @@ package api
import ( import (
"errors" "errors"
"net/http" "net/http"
"reflect"
"git.beisel.it/florian/hostname-service/rules" "git.beisel.it/florian/hostname-service/rules"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// @Summary Returns a list of available rules
// @Description Return a list of names of Rules which are known in the RulesRegistry
// @ID get-rules
// @Produce json
// @Security Bearer
// @Tags Querying Rules
// @Router /rules [get]
func ListAvailableRules(c *gin.Context) { func ListAvailableRules(c *gin.Context) {
descriptions := make(map[string]string) descriptions := make(map[string]string)
for category, descriptor := range rules.RulesRegistry { for category, descriptor := range rules.RulesRegistry {
@ -36,3 +44,44 @@ func getHostnameRuleByCategory(category string) (rules.HostnameRule, error) {
} }
return nil, errors.New("unknown category") return nil, errors.New("unknown category")
} }
// @Summary Returns details about a rule
// @Description Returns two obea an Input Object and an Output Object
// @Description describing the Rules stored values and required parameters
// @ID get-rule-details
// @Produce json
// @Success 200 {object} models.SimpleHostnameResponse "Hostname"
// @Security Bearer
// @Tags Querying Rules
// @Router /rules/:rule [get]
func GetRuleStruct(c *gin.Context) {
ruleName := c.Param("rule")
descriptor, exists := rules.RulesRegistry[ruleName]
if !exists {
c.JSON(http.StatusNotFound, gin.H{"error": "Rule not found"})
return
}
// Create instances of the rule and its input struct
ruleInstance := descriptor.Factory()
inputInstance := reflect.New(reflect.TypeOf(ruleInstance).Elem()).Interface()
// Serialize instances to JSON
ruleJSON, err := rules.StructToJSON(ruleInstance)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing rule struct"})
return
}
inputJSON, err := rules.StructToJSON(inputInstance)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing input struct"})
return
}
c.JSON(http.StatusOK, gin.H{
"input": inputJSON,
"output": ruleJSON,
})
}

View File

@ -101,32 +101,6 @@ const docTemplate = `{
} }
} }
}, },
"/api/rules": {
"get": {
"description": "Get a list of all available hostname generation rules.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Rules"
],
"summary": "List Available Rules",
"responses": {
"200": {
"description": "List of available rules with descriptions",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/api/server": { "/api/server": {
"put": { "put": {
"security": [ "security": [
@ -286,6 +260,51 @@ const docTemplate = `{
} }
} }
}, },
"/rules": {
"get": {
"security": [
{
"Bearer": []
}
],
"description": "Return a list of names of Rules which are known in the RulesRegistry",
"produces": [
"application/json"
],
"tags": [
"Querying Rules"
],
"summary": "Returns a list of available rules",
"operationId": "get-rules",
"responses": {}
}
},
"/rules/:rule": {
"get": {
"security": [
{
"Bearer": []
}
],
"description": "Returns two obea an Input Object and an Output Object\ndescribing the Rules stored values and required parameters",
"produces": [
"application/json"
],
"tags": [
"Querying Rules"
],
"summary": "Returns details about a rule",
"operationId": "get-rule-details",
"responses": {
"200": {
"description": "Hostname",
"schema": {
"$ref": "#/definitions/models.SimpleHostnameResponse"
}
}
}
}
},
"/{category}": { "/{category}": {
"get": { "get": {
"security": [ "security": [

View File

@ -95,32 +95,6 @@
} }
} }
}, },
"/api/rules": {
"get": {
"description": "Get a list of all available hostname generation rules.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Rules"
],
"summary": "List Available Rules",
"responses": {
"200": {
"description": "List of available rules with descriptions",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/api/server": { "/api/server": {
"put": { "put": {
"security": [ "security": [
@ -280,6 +254,51 @@
} }
} }
}, },
"/rules": {
"get": {
"security": [
{
"Bearer": []
}
],
"description": "Return a list of names of Rules which are known in the RulesRegistry",
"produces": [
"application/json"
],
"tags": [
"Querying Rules"
],
"summary": "Returns a list of available rules",
"operationId": "get-rules",
"responses": {}
}
},
"/rules/:rule": {
"get": {
"security": [
{
"Bearer": []
}
],
"description": "Returns two obea an Input Object and an Output Object\ndescribing the Rules stored values and required parameters",
"produces": [
"application/json"
],
"tags": [
"Querying Rules"
],
"summary": "Returns details about a rule",
"operationId": "get-rule-details",
"responses": {
"200": {
"description": "Hostname",
"schema": {
"$ref": "#/definitions/models.SimpleHostnameResponse"
}
}
}
}
},
"/{category}": { "/{category}": {
"get": { "get": {
"security": [ "security": [

View File

@ -212,23 +212,6 @@ paths:
summary: Update hostname for category "notebook" summary: Update hostname for category "notebook"
tags: tags:
- Manipulate existing Hostnames - Manipulate existing Hostnames
/api/rules:
get:
consumes:
- application/json
description: Get a list of all available hostname generation rules.
produces:
- application/json
responses:
"200":
description: List of available rules with descriptions
schema:
additionalProperties:
type: string
type: object
summary: List Available Rules
tags:
- Rules
/api/server: /api/server:
post: post:
consumes: consumes:
@ -330,6 +313,36 @@ paths:
summary: User login summary: User login
tags: tags:
- Authentication - Authentication
/rules:
get:
description: Return a list of names of Rules which are known in the RulesRegistry
operationId: get-rules
produces:
- application/json
responses: {}
security:
- Bearer: []
summary: Returns a list of available rules
tags:
- Querying Rules
/rules/:rule:
get:
description: |-
Returns two obea an Input Object and an Output Object
describing the Rules stored values and required parameters
operationId: get-rule-details
produces:
- application/json
responses:
"200":
description: Hostname
schema:
$ref: '#/definitions/models.SimpleHostnameResponse'
security:
- Bearer: []
summary: Returns details about a rule
tags:
- Querying Rules
securityDefinitions: securityDefinitions:
Bearer: Bearer:
description: Type "Bearer" followed by a space and JWT token. description: Type "Bearer" followed by a space and JWT token.

View File

@ -19,6 +19,7 @@ import (
"git.beisel.it/florian/hostname-service/auth" "git.beisel.it/florian/hostname-service/auth"
"git.beisel.it/florian/hostname-service/docs" "git.beisel.it/florian/hostname-service/docs"
"git.beisel.it/florian/hostname-service/middleware" "git.beisel.it/florian/hostname-service/middleware"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files" swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger" ginSwagger "github.com/swaggo/gin-swagger"
@ -27,7 +28,14 @@ import (
func New() *gin.Engine { func New() *gin.Engine {
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)
router := gin.Default() 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.Host = "localhost:8080"
docs.SwaggerInfo.BasePath = "/api/v1" docs.SwaggerInfo.BasePath = "/api/v1"
@ -36,6 +44,7 @@ func New() *gin.Engine {
{ {
// public routes // public routes
v1.POST("/login", auth.LoginHandler) v1.POST("/login", auth.LoginHandler)
v1.GET("/login", api.Helloworld)
// Protected Routes // Protected Routes
authenticated := v1.Group("/").Use(middleware.Authenticate()) authenticated := v1.Group("/").Use(middleware.Authenticate())
@ -62,7 +71,8 @@ func New() *gin.Engine {
authenticated.GET("/:category", api.ListHostnamesByCategory) authenticated.GET("/:category", api.ListHostnamesByCategory)
// List available Rules // List available Rules
authenticated.GET("/api/rules", api.ListAvailableRules) authenticated.GET("/rules", api.ListAvailableRules)
authenticated.GET("/rules/:rule", api.GetRuleStruct)
} }
} }