Skip to content

Commit

Permalink
Merge health with api server
Browse files Browse the repository at this point in the history
Protect health endpoint with Basic Auth credentials
  • Loading branch information
LucaBernstein committed May 25, 2023
1 parent 10abea7 commit 046bd0e
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ POSTGRES_PASSWORD=
SQLITE_FILE=beancount-bot-tg.sqlite

BOT_API_KEY=

MONITORING_USER=
MONITORING_PASS=
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
8 changes: 4 additions & 4 deletions web/health/monitoring.go → api/health/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
Expand Down
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -16,8 +15,6 @@ func main() {

go api.StartWebServer(bc)

go web.StartWebServer(bc)

bot := bot.CreateBot(bc)
bc.AddBotAndStart(bot)
}
2 changes: 1 addition & 1 deletion scenarioTests/steps/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"')
Expand Down
16 changes: 0 additions & 16 deletions web/server.go

This file was deleted.

0 comments on commit 046bd0e

Please sign in to comment.