From 79e6cc8e5b8faa0f6bbf09365112bf38b46fc3b2 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Mon, 28 Feb 2022 09:18:44 +0200 Subject: [PATCH] Feature - Gateway (#12) * feat: add geateway config * feat: add ping controller for check by gateway * feat: add new env vars for gateway * feat: update config * gateway request wip * feat: add auth header for gateway request * feat: separate gateway folder * update endpoint for gateway * add makefile * lint * swap png to svg * add url of board to request to gateway * change deployed path prefix * add env in compose * add body log on gateway connect --- .env.example | 7 +++++- Makefile | 2 ++ config/config.go | 27 +++++++++++++++++++++ docker-compose.yml | 11 +++++++-- gateway/connect.go | 39 +++++++++++++++++++++++++++++++ gateway/controllers.go | 19 +++++++++++++++ main.go | 5 ++++ static/icons/github.svg | 1 + static/images/github-icon.png | Bin 1714 -> 0 bytes static/styles/index.css | 4 ++++ templates/components/footer.html | 2 +- templates/head/static.html | 4 ++-- 12 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 Makefile create mode 100644 gateway/connect.go create mode 100644 gateway/controllers.go create mode 100644 static/icons/github.svg delete mode 100644 static/images/github-icon.png diff --git a/.env.example b/.env.example index 86c77e9..ba539bd 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,9 @@ 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 +GATEWAY_BOARD_ID=b +GATEWAY_BOARD_URL=http://localhost:3000 +GATEWAY_BOARD_DESCRIPTION=Random \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..913d0b1 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +dev: + nodemon --exec go run main.go --signal SIGTERM diff --git a/config/config.go b/config/config.go index 67dac10..45fcb40 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,14 @@ import ( "strconv" ) +type GatewayConfig struct { + Url string + ApiKey string + BoardId string + BoardUrl string + BoardDescription string +} + type AppConfig struct { Env string Port int @@ -15,6 +23,7 @@ type AppConfig struct { ThreadsMaxCount int ThreadBumpLimit int IsCaptchaActive bool + Gateway GatewayConfig } type DbConfig struct { @@ -46,6 +55,22 @@ func getValueOrDefaultString(value string, defaultValue string) string { return value } +func getGatewayConfig() GatewayConfig { + url := os.Getenv("GATEWAY_URL") + apiKey := os.Getenv("GATEWAY_API_KEY") + boardId := os.Getenv("GATEWAY_BOARD_ID") + description := os.Getenv("GATEWAY_BOARD_DESCRIPTION") + boardUrl := os.Getenv("GATEWAY_BOARD_URL") + + return GatewayConfig{ + Url: url, + ApiKey: apiKey, + BoardId: boardId, + BoardUrl: boardUrl, + BoardDescription: description, + } +} + func getAppConfig() AppConfig { env := getValueOrDefaultString(os.Getenv("ENV"), "release") port := getValueOrDefaultInt(os.Getenv("PORT"), 3000) @@ -54,6 +79,7 @@ func getAppConfig() AppConfig { threadsMaxCount := getValueOrDefaultInt(os.Getenv("THREADS_MAX_COUNT"), 50) threadBumpLimit := getValueOrDefaultInt(os.Getenv("THREAD_BUMP_LIMIT"), 500) isCaptchaActive := getValueOrDefaultBoolean(os.Getenv("IS_CAPTCHA_ACTIVE"), true) + gateway := getGatewayConfig() return AppConfig{ Env: env, @@ -63,6 +89,7 @@ func getAppConfig() AppConfig { ThreadsMaxCount: threadsMaxCount, ThreadBumpLimit: threadBumpLimit, IsCaptchaActive: isCaptchaActive, + Gateway: gateway, } } diff --git a/docker-compose.yml b/docker-compose.yml index 256cc7b..0707256 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,13 @@ services: IS_RATE_LIMITER_ENABLED: ${IS_RATE_LIMITER_ENABLED} THREADS_MAX_COUNT: ${THREADS_MAX_COUNT} POSTGRES_URL: ${POSTGRES_URL} + THREAD_BUMP_LIMIT: ${THREAD_BUMP_LIMIT} + IS_CAPTCHA_ACTIVE: ${IS_CAPTCHA_ACTIVE} + GATEWAY_URL: ${GATEWAY_URL} + GATEWAY_API_KEY: ${GATEWAY_API_KEY} + GATEWAY_BOARD_ID: ${GATEWAY_BOARD_ID} + GATEWAY_BOARD_URL: ${GATEWAY_BOARD_URL} + GATEWAY_BOARD_DESCRIPTION: ${GATEWAY_BOARD_DESCRIPTION} volumes: - /root/micrach-go/uploads:/app/uploads deploy: @@ -33,11 +40,11 @@ services: traefik.http.middlewares.micrach-https-redirect.redirectscheme.scheme: "https" traefik.http.routers.micrach.entrypoints: "http" - traefik.http.routers.micrach.rule: "Host(`micrach.igonin.dev`)" + traefik.http.routers.micrach.rule: "Host(`micrach.igonin.dev`) && PathPrefix(`/${GATEWAY_BOARD_ID}`)" traefik.http.routers.micrach.middlewares: "micrach-https-redirect" traefik.http.routers.micrach-secure.entrypoints: "https" - traefik.http.routers.micrach-secure.rule: "Host(`micrach.igonin.dev`)" + traefik.http.routers.micrach-secure.rule: "Host(`micrach.igonin.dev`) && PathPrefix(`/${GATEWAY_BOARD_ID}`)" traefik.http.routers.micrach-secure.tls: "true" traefik.http.routers.micrach-secure.service: "micrach" diff --git a/gateway/connect.go b/gateway/connect.go new file mode 100644 index 0000000..2b859b2 --- /dev/null +++ b/gateway/connect.go @@ -0,0 +1,39 @@ +package gateway + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "log" + "net/http" + + Config "micrach/config" +) + +// Make http request to gateway to tell the board id and description +func Connect() { + requestBody, _ := json.Marshal(map[string]string{ + "id": Config.App.Gateway.BoardId, + "name": Config.App.Gateway.BoardDescription, + "url": Config.App.Gateway.Url, + }) + url := Config.App.Gateway.Url + "/api/boards" + req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody)) + if err != nil { + log.Panicln(err) + } + req.Header.Set("Authorization", Config.App.Gateway.ApiKey) + req.Header.Set("Content-Type", "application/json") + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Panicln(err) + } + //We Read the response body on the line below. + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Panicln(err) + } + log.Println(string(body)) + log.Println("gateway - online") +} diff --git a/gateway/controllers.go b/gateway/controllers.go new file mode 100644 index 0000000..b0b7a16 --- /dev/null +++ b/gateway/controllers.go @@ -0,0 +1,19 @@ +package gateway + +import ( + Config "micrach/config" + "net/http" + + "github.com/gin-gonic/gin" +) + +func Ping(c *gin.Context) { + headerKey := c.Request.Header.Get("Authorization") + if Config.App.Gateway.ApiKey != headerKey { + c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) + return + } + c.JSON(200, gin.H{ + "message": "pong", + }) +} diff --git a/main.go b/main.go index 5f8c232..20240c3 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( Config "micrach/config" Controllers "micrach/controllers" Db "micrach/db" + Gateway "micrach/gateway" Repositories "micrach/repositories" Templates "micrach/templates" Utils "micrach/utils" @@ -62,6 +63,10 @@ func main() { } router.Static("/uploads", "./uploads") router.Static("/static", "./static") + if Config.App.Gateway.Url != "" { + router.GET("/api/ping", Gateway.Ping) + Gateway.Connect() + } router.GET("/", Controllers.GetThreads) router.POST("/", Controllers.CreateThread) router.GET("/:threadID", Controllers.GetThread) diff --git a/static/icons/github.svg b/static/icons/github.svg new file mode 100644 index 0000000..aa05db9 --- /dev/null +++ b/static/icons/github.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/static/images/github-icon.png b/static/images/github-icon.png deleted file mode 100644 index 8b25551a97921681334176ee143b41510a117d86..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1714 zcmaJ?X;2eq7*4oFu!ne{XxAht2qc?8LXr|_LPCfTpaBK7K$c{I0Ld=NLIOeuC;@2) zZ$K%a)k+m-s0>xHmKxL%0V&0TRzzznhgyqrIC$F)0{WwLXLrBvd*^wc_uSc%h%m9E z{W5z3f#4_!7RvAyFh6!S_*<8qJ%KOIm?#E|L=rJQq=gB5C6WLG5;c?r%V0>EmEH#X z5eSwPRa6WXBMs#$5H%GtW2go-in9p>zW@UYDNNWc^XOXZQ? z1QjEV00I#$3^1wQUJ8&-2UsjB-G|9y(LDhMNN3PM{APL4eYi{(m*ERcUnJa{R+-3^ z34^A6;U^v`8N*O6ji%S@sd{fJqD`XFIUJ5zgTe5^5nj414F(y!G&=H(f)Lgzv?>%+ zAsWD}2qhpH7>|TU`X&W6IxDNuO_vET7|j5oG&&VDr!)hUO8+0KR?nh!m<)a!?|%yG zqOwq!CWCcIhE{<$E|F|@g>nP6FoYr6C<8>D?ID9%&5J(4oSbR1I^byW*g@__U z4QsF&uJSEcFeleM3~ChjEQGbHOjsGDMbyAl(p=Ttv9RaVo8~I#js@@Y9C^_2U})yn zzSHU%6FxuY?d;&65MyR({^lU*3$z$ZllDb(o&<7d;A_`h2U+3~BJ2Hv`{W}KEU801#cv_B|9Cm!ynR{S`AMsSn z;7E=B;mb!wx$L;S>yGXG^6=&WlQn9$s?&L%Y1D8TI^MlKB1DqsEng$>f4=xYWBoPI z_S1p!sJ#d2?YI4kPA{k}Eby?F=f-J9zIc`YDl^pzjVm~9ebE?Hn?t0Nx+la|D0MB; z9)2xv1G>a1|A9kQ>~DV<=X3-4yC&n!m8-3K#P z{X@0zRuQsy$+N ziSCoLJU{Z$nQy4A4Y5UJ07$5FA~qL2%Q+cLaqDU?Lz3?=BC5;Nk6BbTmmceEaM>-Z zi>O&-dSE=%ex;vcvCOk{*JQ5^_4M z4lW7%l9IqY(z7pV(?I@@8=KPFO82)O{VDI18-*d-k$YmI^XiuPs_LuFw<^ZcD}yP5 c*NrbeloN*74g`U%%F6r~k%+>C^#XapzmV0H-2eap diff --git a/static/styles/index.css b/static/styles/index.css index 8fb0bdb..4d9fde7 100644 --- a/static/styles/index.css +++ b/static/styles/index.css @@ -1,3 +1,7 @@ #pagesList { flex-wrap: wrap } + +.github-icon { + width: 2.5rem; +} \ No newline at end of file diff --git a/templates/components/footer.html b/templates/components/footer.html index b8613a3..7234109 100644 --- a/templates/components/footer.html +++ b/templates/components/footer.html @@ -2,7 +2,7 @@ diff --git a/templates/head/static.html b/templates/head/static.html index a7cf3d6..a8b980d 100644 --- a/templates/head/static.html +++ b/templates/head/static.html @@ -11,6 +11,6 @@ crossorigin="anonymous"> - - + + {{ end }}