From 40e280a3a791afc4414a1b9cc2e046f2ffc6d97c Mon Sep 17 00:00:00 2001 From: shibaisdog Date: Mon, 20 Jan 2025 15:19:30 +0900 Subject: [PATCH] update structure --- docs/docs.go | 7 ++++ docs/swagger.json | 7 ++++ docs/swagger.yaml | 5 +++ internal/handler/korcen.go | 82 +++++++++++++++++++++++++++++++++++++ internal/handler/respond.go | 11 +++++ internal/router/routes.go | 73 +-------------------------------- 6 files changed, 114 insertions(+), 71 deletions(-) create mode 100644 internal/handler/korcen.go create mode 100644 internal/handler/respond.go diff --git a/docs/docs.go b/docs/docs.go index 67cfadf..789bf7a 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -54,6 +54,13 @@ const docTemplate = `{ "type": "object", "additionalProperties": true } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "type": "object", + "additionalProperties": true + } } } } diff --git a/docs/swagger.json b/docs/swagger.json index cef452c..f92043b 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -43,6 +43,13 @@ "type": "object", "additionalProperties": true } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "type": "object", + "additionalProperties": true + } } } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 9e4cbde..a307efd 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -48,6 +48,11 @@ paths: schema: additionalProperties: true type: object + "500": + description: Internal Server Error + schema: + additionalProperties: true + type: object summary: Process Korcen Request tags: - korcen diff --git a/internal/handler/korcen.go b/internal/handler/korcen.go new file mode 100644 index 0000000..a696f5e --- /dev/null +++ b/internal/handler/korcen.go @@ -0,0 +1,82 @@ +package handler + +import ( + "net/http" + "strings" + "time" + + "github.com/asynkron/protoactor-go/actor" + "github.com/fluffy-melli/korcen-api/pkg/check" + "github.com/gin-gonic/gin" +) + +// @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" +// @Failure 500 {object} map[string]interface{} "Internal Server Error" +// @Router /api/v1/korcen [post] +func KorcenV1(c *gin.Context, system *actor.ActorSystem, korcenPID *actor.PID) { + var header check.Header + isXML := false + + switch c.ContentType() { + case "text/xml", "application/xml": + if err := c.ShouldBindXML(&header); err != nil { + respond(c, http.StatusBadRequest, true, gin.H{"error": "Invalid XML request"}) + return + } + isXML = true + default: + if err := c.ShouldBindJSON(&header); err != nil { + respond(c, http.StatusBadRequest, false, gin.H{"error": "Invalid JSON request"}) + return + } + } + + if strings.TrimSpace(header.Input) == "" { + if isXML { + c.XML(http.StatusBadRequest, gin.H{"error": "Invalid request: empty input"}) + } else { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: empty input"}) + } + return + } + + future := system.Root.RequestFuture(korcenPID, &check.KorcenRequest{Header: &header}, 5*time.Second) + result, err := future.Result() + if err != nil { + if isXML { + c.XML(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } else { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + return + } + + korcenResp, ok := result.(*check.KorcenResponse) + if !ok { + if isXML { + c.XML(http.StatusInternalServerError, gin.H{"error": "Invalid actor response"}) + } else { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid actor response"}) + } + return + } + + if korcenResp.Err != nil { + if isXML { + c.XML(http.StatusBadRequest, gin.H{"error": korcenResp.Err.Error()}) + } else { + c.JSON(http.StatusBadRequest, gin.H{"error": korcenResp.Err.Error()}) + } + return + } + + response := korcenResp.Respond + respond(c, http.StatusOK, isXML, response) +} diff --git a/internal/handler/respond.go b/internal/handler/respond.go new file mode 100644 index 0000000..142f04a --- /dev/null +++ b/internal/handler/respond.go @@ -0,0 +1,11 @@ +package handler + +import "github.com/gin-gonic/gin" + +func respond(c *gin.Context, status int, isXML bool, payload interface{}) { + if isXML { + c.XML(status, payload) + } else { + c.JSON(status, payload) + } +} diff --git a/internal/router/routes.go b/internal/router/routes.go index 8832d6f..12c4773 100644 --- a/internal/router/routes.go +++ b/internal/router/routes.go @@ -3,24 +3,12 @@ package router import ( - "net/http" - "strings" - "time" - "github.com/asynkron/protoactor-go/actor" + "github.com/fluffy-melli/korcen-api/internal/handler" "github.com/fluffy-melli/korcen-api/internal/middleware" - "github.com/fluffy-melli/korcen-api/pkg/check" "github.com/gin-gonic/gin" ) -func respond(c *gin.Context, status int, isXML bool, payload interface{}) { - if isXML { - c.XML(status, payload) - } else { - c.JSON(status, payload) - } -} - func SetupRouter(system *actor.ActorSystem, korcenPID *actor.PID, config middleware.MiddlewareConfig) *gin.Engine { gin.SetMode(gin.ReleaseMode) r := gin.New() @@ -33,64 +21,7 @@ func SetupRouter(system *actor.ActorSystem, korcenPID *actor.PID, config middlew APIGroup := r.Group("/api/v1") { APIGroup.POST("/korcen", func(c *gin.Context) { - var header check.Header - isXML := false - - switch c.ContentType() { - case "text/xml", "application/xml": - if err := c.ShouldBindXML(&header); err != nil { - respond(c, http.StatusBadRequest, true, gin.H{"error": "Invalid XML request"}) - return - } - isXML = true - default: - if err := c.ShouldBindJSON(&header); err != nil { - respond(c, http.StatusBadRequest, false, gin.H{"error": "Invalid JSON request"}) - return - } - } - - if strings.TrimSpace(header.Input) == "" { - if isXML { - c.XML(http.StatusBadRequest, gin.H{"error": "Invalid request: empty input"}) - } else { - c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: empty input"}) - } - return - } - - future := system.Root.RequestFuture(korcenPID, &check.KorcenRequest{Header: &header}, 5*time.Second) - result, err := future.Result() - if err != nil { - if isXML { - c.XML(http.StatusInternalServerError, gin.H{"error": err.Error()}) - } else { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - } - return - } - - korcenResp, ok := result.(*check.KorcenResponse) - if !ok { - if isXML { - c.XML(http.StatusInternalServerError, gin.H{"error": "Invalid actor response"}) - } else { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid actor response"}) - } - return - } - - if korcenResp.Err != nil { - if isXML { - c.XML(http.StatusBadRequest, gin.H{"error": korcenResp.Err.Error()}) - } else { - c.JSON(http.StatusBadRequest, gin.H{"error": korcenResp.Err.Error()}) - } - return - } - - response := korcenResp.Respond - respond(c, http.StatusOK, isXML, response) + handler.KorcenV1(c, system, korcenPID) }) }