From 184578df0764ba90aa9677374c5231b9b505dde0 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Thu, 10 Feb 2022 09:21:44 +0200 Subject: [PATCH] feat: add ping controller for check by gateway --- .env.example | 4 +++- controllers/gateway_controller.go | 18 ++++++++++++++++++ main.go | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 controllers/gateway_controller.go diff --git a/.env.example b/.env.example index 86c77e9..a466c3f 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,6 @@ IS_RATE_LIMITER_ENABLED=true THREADS_MAX_COUNT=50 POSTGRES_URL=postgres://localhost/micrach?pool_max_conns=5 THREAD_BUMP_LIMIT=500 -IS_CAPTCHA_ACTIVE=true \ No newline at end of file +IS_CAPTCHA_ACTIVE=true +GATEWAY_URL=http://localhost:3001 +GATEWAY_API_KEY=example \ No newline at end of file diff --git a/controllers/gateway_controller.go b/controllers/gateway_controller.go new file mode 100644 index 0000000..5d23e1a --- /dev/null +++ b/controllers/gateway_controller.go @@ -0,0 +1,18 @@ +package controllers + +import ( + Config "micrach/config" + + "github.com/gin-gonic/gin" +) + +func Ping(c *gin.Context) { + headerKey := c.Request.Header.Get("Authorization") + if Config.App.Gateway.ApiKey != headerKey { + c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"}) + return + } + c.JSON(200, gin.H{ + "message": "pong", + }) +} diff --git a/main.go b/main.go index c2ee44f..2871fe0 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,9 @@ func main() { } router.Static("/uploads", "./uploads") router.Static("/static", "./static") + if Config.App.Gateway.Url != "" { + router.GET("/ping", Controllers.Ping) + } router.GET("/", Controllers.GetThreads) router.POST("/", Controllers.CreateThread) router.GET("/:threadID", Controllers.GetThread)