✨ Add rules routes
This commit is contained in:
@ -17,11 +17,19 @@ package api
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"git.beisel.it/florian/hostname-service/rules"
|
||||
"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) {
|
||||
descriptions := make(map[string]string)
|
||||
for category, descriptor := range rules.RulesRegistry {
|
||||
@ -36,3 +44,44 @@ func getHostnameRuleByCategory(category string) (rules.HostnameRule, error) {
|
||||
}
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user