diff --git a/internal/router/routes.go b/internal/router/routes.go index a9604ee..59e8359 100644 --- a/internal/router/routes.go +++ b/internal/router/routes.go @@ -1,6 +1,7 @@ package router import ( + "net/http" "strings" _ "github.com/fluffy-melli/korcen-api/docs" @@ -9,37 +10,50 @@ import ( "github.com/gin-gonic/gin" ) -// @Summary Process Korcen Request -// @Description Processes a Korcen request and returns the result -// @Tags korcen -// @Accept json -// @Produce json -// @Param input body check.Header true "Korcen Input" -// @Success 200 {object} check.Respond "Korcen Result" -// @Failure 400 {object} map[string]interface{} "Invalid Request" -// @Router /api/v1/korcen [post] +// @Summary Process Korcen Request +// @Description Processes a Korcen request and returns the result +// @Tags korcen +// @Accept json,xml +// @Produce json,xml +// @Param input body check.Header true "Korcen Input" +// @Success 200 {object} check.Respond "Korcen Result" +// @Failure 400 {object} map[string]interface{} "Invalid Request" +// @Router /api/v1/korcen [post] func Korcen(c *gin.Context) { var header check.Header - if err := c.ShouldBindJSON(&header); err != nil { - c.JSON(400, gin.H{"error": "Invalid request"}) - return + switch c.ContentType() { + case "application/xml": + if err := c.ShouldBindXML(&header); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid XML request"}) + return + } + default: + if err := c.ShouldBindJSON(&header); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON request"}) + return + } } if strings.TrimSpace(header.Input) == "" { - c.JSON(400, gin.H{"error": "Invalid request"}) + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: empty input"}) return } response := check.Korcen(&header) - c.JSON(200, response) + if c.GetHeader("Accept") == "application/xml" { + c.XML(http.StatusOK, response) + } else { + c.JSON(http.StatusOK, response) + } } func SetupRouter() *gin.Engine { gin.SetMode(gin.ReleaseMode) r := gin.Default() r.Use(middleware.TokenBucketMiddleware()) + APIGroup := r.Group("/api/v1") { APIGroup.POST("/korcen", Korcen) diff --git a/main.go b/main.go index c2142f9..c00ede5 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,12 @@ -// hi/korcen-api/main.go +// korcen-api/main.go package main import ( + "fmt" + "log" + "os" + _ "github.com/fluffy-melli/korcen-api/docs" "github.com/fluffy-melli/korcen-api/internal/router" swaggerFiles "github.com/swaggo/files" @@ -10,7 +14,12 @@ import ( ) func main() { + fmt.Println("Korcen API Server Start") + setup := router.SetupRouter() setup.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) - setup.Run(":7777") + if err := setup.Run(":7777"); err != nil { + log.Println("Failed to start server:", err) + os.Exit(1) + } } diff --git a/pkg/check/korcen.go b/pkg/check/korcen.go index 3ad8f25..e922581 100644 --- a/pkg/check/korcen.go +++ b/pkg/check/korcen.go @@ -3,20 +3,24 @@ package check import ( + "encoding/xml" + "github.com/fluffy-melli/korcen-go" ) type Header struct { - Input string `json:"input"` - Start string `json:"replace-front"` - End string `json:"replace-end"` + XMLName xml.Name `json:"-" xml:"header"` + Input string `json:"input" xml:"input"` + Start string `json:"replace-front" xml:"replace-front"` + End string `json:"replace-end" xml:"replace-end"` } type Respond struct { - Detect bool `json:"detect"` - Swear string `json:"swear"` - String string `json:"input"` - NewString string `json:"output"` + XMLName xml.Name `json:"-" xml:"respond"` + Detect bool `json:"detect" xml:"detect"` + Swear string `json:"swear" xml:"swear"` + String string `json:"input" xml:"input"` + NewString string `json:"output" xml:"output"` } func Korcen(header *Header) *Respond {