|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/CodeMeAPixel/FixFX-Core/internal/services" |
| 5 | + "github.com/gofiber/fiber/v2" |
| 6 | +) |
| 7 | + |
| 8 | +var validatorService *services.ValidatorService |
| 9 | + |
| 10 | +// InitValidatorHandler initializes the validator handler |
| 11 | +func InitValidatorHandler() { |
| 12 | + validatorService = services.NewValidatorService() |
| 13 | +} |
| 14 | + |
| 15 | +// ValidateJSON handles POST /api/validator/validate |
| 16 | +// @Summary Validate JSON |
| 17 | +// @Description Validate JSON with optional txAdmin embed/config schema validation |
| 18 | +// @Tags Validator |
| 19 | +// @Accept json |
| 20 | +// @Produce json |
| 21 | +// @Param body body ValidateRequest true "JSON to validate" |
| 22 | +// @Success 200 {object} map[string]interface{} |
| 23 | +// @Failure 400 {object} map[string]interface{} |
| 24 | +// @Router /validator/validate [post] |
| 25 | +func ValidateJSON(c *fiber.Ctx) error { |
| 26 | + var req struct { |
| 27 | + JSON string `json:"json"` |
| 28 | + Type string `json:"type"` |
| 29 | + } |
| 30 | + |
| 31 | + if err := c.BodyParser(&req); err != nil { |
| 32 | + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 33 | + "error": "Invalid request body", |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + if req.JSON == "" { |
| 38 | + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 39 | + "error": "JSON input is required", |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + // Determine validation type |
| 44 | + validationType := services.ValidationGeneric |
| 45 | + switch req.Type { |
| 46 | + case "txadmin-embed": |
| 47 | + validationType = services.ValidationTxEmbed |
| 48 | + case "txadmin-embed-config": |
| 49 | + validationType = services.ValidationTxEmbedConf |
| 50 | + case "generic", "": |
| 51 | + validationType = services.ValidationGeneric |
| 52 | + default: |
| 53 | + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ |
| 54 | + "error": "Invalid validation type. Valid types: generic, txadmin-embed, txadmin-embed-config", |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + result := validatorService.Validate(req.JSON, validationType) |
| 59 | + |
| 60 | + return c.JSON(fiber.Map{ |
| 61 | + "result": result, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +// GetValidatorInfo handles GET /api/validator/info |
| 66 | +// @Summary Get validator information |
| 67 | +// @Description Get available validation types, txAdmin placeholders, and schema info |
| 68 | +// @Tags Validator |
| 69 | +// @Produce json |
| 70 | +// @Success 200 {object} map[string]interface{} |
| 71 | +// @Router /validator/info [get] |
| 72 | +func GetValidatorInfo(c *fiber.Ctx) error { |
| 73 | + placeholders := []map[string]string{ |
| 74 | + {"name": "serverCfxId", "description": "The Cfx.re id of your server, tied to sv_licenseKey"}, |
| 75 | + {"name": "serverJoinUrl", "description": "The direct join URL (e.g., https://cfx.re/join/xxxxxx)"}, |
| 76 | + {"name": "serverBrowserUrl", "description": "The FiveM Server browser URL"}, |
| 77 | + {"name": "serverClients", "description": "Number of players online"}, |
| 78 | + {"name": "serverMaxClients", "description": "The sv_maxclients value"}, |
| 79 | + {"name": "serverName", "description": "The txAdmin-given server name"}, |
| 80 | + {"name": "statusColor", "description": "Hex-encoded color from Config JSON"}, |
| 81 | + {"name": "statusString", "description": "Status text from Config JSON"}, |
| 82 | + {"name": "uptime", "description": "How long the server has been online"}, |
| 83 | + {"name": "nextScheduledRestart", "description": "When the next scheduled restart is"}, |
| 84 | + } |
| 85 | + |
| 86 | + types := []map[string]string{ |
| 87 | + {"value": "generic", "label": "Generic JSON", "description": "Validates JSON syntax only"}, |
| 88 | + {"value": "txadmin-embed", "label": "txAdmin Embed JSON", "description": "Validates Discord embed JSON for txAdmin status embed"}, |
| 89 | + {"value": "txadmin-embed-config", "label": "txAdmin Embed Config", "description": "Validates txAdmin embed configuration (colors, buttons)"}, |
| 90 | + } |
| 91 | + |
| 92 | + return c.JSON(fiber.Map{ |
| 93 | + "types": types, |
| 94 | + "placeholders": placeholders, |
| 95 | + "limits": fiber.Map{ |
| 96 | + "embed": fiber.Map{ |
| 97 | + "title": 256, |
| 98 | + "description": 4096, |
| 99 | + "fields": 25, |
| 100 | + "fieldName": 256, |
| 101 | + "fieldValue": 1024, |
| 102 | + }, |
| 103 | + "config": fiber.Map{ |
| 104 | + "buttons": 5, |
| 105 | + "buttonLabel": 80, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }) |
| 109 | +} |
0 commit comments