From 046bd0eb71c59a29ab77f5dc79781914a683a38c Mon Sep 17 00:00:00 2001 From: Luca Bernstein Date: Mon, 22 May 2023 18:30:27 +0200 Subject: [PATCH] Merge health with api server Protect health endpoint with Basic Auth credentials --- .env.sample | 3 +++ Dockerfile | 1 - {web => api}/health/monitoring.go | 8 ++++---- {web => api}/health/monitoring_test.go | 0 api/server.go | 6 ++++++ main.go | 3 --- scenarioTests/steps/bot.py | 2 +- web/server.go | 16 ---------------- 8 files changed, 14 insertions(+), 25 deletions(-) rename {web => api}/health/monitoring.go (96%) rename {web => api}/health/monitoring_test.go (100%) delete mode 100644 web/server.go diff --git a/.env.sample b/.env.sample index 00af971..5ef1886 100644 --- a/.env.sample +++ b/.env.sample @@ -7,3 +7,6 @@ POSTGRES_PASSWORD= SQLITE_FILE=beancount-bot-tg.sqlite BOT_API_KEY= + +MONITORING_USER= +MONITORING_PASS= diff --git a/Dockerfile b/Dockerfile index 6d867c8..a81138e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,6 +36,5 @@ COPY --from=flutter_builder_web /api/ui/build/web /dist/api/ui/build/web COPY --from=golang_builder /src/app /dist/app EXPOSE 8080 -EXPOSE 8081 ENTRYPOINT [ "/dist/app" ] diff --git a/web/health/monitoring.go b/api/health/monitoring.go similarity index 96% rename from web/health/monitoring.go rename to api/health/monitoring.go index cef4abe..a9179b1 100644 --- a/web/health/monitoring.go +++ b/api/health/monitoring.go @@ -2,11 +2,11 @@ package health import ( "fmt" - "net/http" "os" "github.com/LucaBernstein/beancount-bot-tg/bot" "github.com/LucaBernstein/beancount-bot-tg/helpers" + "github.com/gin-gonic/gin" ) type MonitoringResult struct { @@ -35,10 +35,10 @@ type MonitoringResult struct { // TODO: Use package? // https://pkg.go.dev/github.com/prometheus/client_golang/prometheus?utm_source=godoc#pkg-overview -func MonitoringEndpoint(bc *bot.BotController) func(w http.ResponseWriter, r *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { +func MonitoringEndpoint(bc *bot.BotController) func(c *gin.Context) { + return func(c *gin.Context) { m := gatherMetrics(bc) - fmt.Fprintf(w, ` + fmt.Fprintf(c.Writer, ` # HELP bc_bot_logs_daily Count of logs of specified type in the previous 24h # TYPE bc_bot_logs_daily gauge bc_bot_logs_daily{level="error"} %d diff --git a/web/health/monitoring_test.go b/api/health/monitoring_test.go similarity index 100% rename from web/health/monitoring_test.go rename to api/health/monitoring_test.go diff --git a/api/server.go b/api/server.go index 4562319..eacf645 100644 --- a/api/server.go +++ b/api/server.go @@ -6,9 +6,11 @@ import ( "github.com/LucaBernstein/beancount-bot-tg/api/admin" "github.com/LucaBernstein/beancount-bot-tg/api/config" "github.com/LucaBernstein/beancount-bot-tg/api/suggestions" + "github.com/LucaBernstein/beancount-bot-tg/api/health" "github.com/LucaBernstein/beancount-bot-tg/api/token" "github.com/LucaBernstein/beancount-bot-tg/api/transactions" "github.com/LucaBernstein/beancount-bot-tg/bot" + "github.com/LucaBernstein/beancount-bot-tg/helpers" "github.com/gin-gonic/gin" "github.com/mandrigin/gin-spa/spa" ) @@ -18,6 +20,10 @@ func StartWebServer(bc *bot.BotController) { r.Use(gin.Recovery()) configureCors(r) + r.GET("/health", gin.BasicAuth(gin.Accounts{ + helpers.EnvOrFb("MONITORING_USER", "beancount-bot-tg-health"): helpers.EnvOrFb("MONITORING_PASS", "this_service_should_be_healthy"), + }), health.MonitoringEndpoint(bc)) + apiGroup := r.Group("/api") tokenGroup := apiGroup.Group("/token") diff --git a/main.go b/main.go index 56ea0ba..6730647 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "github.com/LucaBernstein/beancount-bot-tg/api" "github.com/LucaBernstein/beancount-bot-tg/bot" "github.com/LucaBernstein/beancount-bot-tg/db" - "github.com/LucaBernstein/beancount-bot-tg/web" ) func main() { @@ -16,8 +15,6 @@ func main() { go api.StartWebServer(bc) - go web.StartWebServer(bc) - bot := bot.CreateBot(bc) bc.AddBotAndStart(bot) } diff --git a/scenarioTests/steps/bot.py b/scenarioTests/steps/bot.py index 29c3f14..3742e15 100644 --- a/scenarioTests/steps/bot.py +++ b/scenarioTests/steps/bot.py @@ -105,7 +105,7 @@ async def step_impl(context, position, keyboardEntry): @when('I get the server endpoint "{endpoint}"') @async_run_until_complete async def step_impl(context, endpoint): - res = requests.get(url="http://localhost:8081"+endpoint, timeout=3) + res = requests.get(url="http://localhost:8080"+endpoint, timeout=3) context.body = res.text @then('the response body {shouldShouldNot} include "{include}"') diff --git a/web/server.go b/web/server.go deleted file mode 100644 index c563690..0000000 --- a/web/server.go +++ /dev/null @@ -1,16 +0,0 @@ -package web - -import ( - "log" - "net/http" - - "github.com/LucaBernstein/beancount-bot-tg/bot" - "github.com/LucaBernstein/beancount-bot-tg/web/health" -) - -func StartWebServer(bc *bot.BotController) { - http.HandleFunc("/health", health.MonitoringEndpoint(bc)) - port := ":8081" - log.Printf("Web server started on %s", port) - log.Fatal(http.ListenAndServe(port, nil)) -}