Skip to content

Commit

Permalink
update structure
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffy-melli committed Jan 20, 2025
1 parent 8e1b99c commit 40e280a
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 71 deletions.
7 changes: 7 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ const docTemplate = `{
"type": "object",
"additionalProperties": true
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
"type": "object",
"additionalProperties": true
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions internal/handler/korcen.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 11 additions & 0 deletions internal/handler/respond.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
73 changes: 2 additions & 71 deletions internal/router/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
})
}

Expand Down

0 comments on commit 40e280a

Please sign in to comment.