From 2cd46585a823199550900f19f6203d99de95a82c Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Mon, 13 May 2024 16:57:55 +0300 Subject: [PATCH] add hot-reload & debugger --- .air.toml | 10 ++++++++++ .drone.yml | 2 ++ .gitignore | 2 ++ Makefile | 21 +++++++++++++++++++-- compose.prod.yml | 12 +++--------- compose.yml | 2 ++ internal/handler/new_message_handler.go | 4 ---- internal/handler/util/command.go | 7 +++++++ internal/handler/wizard/doc.go | 2 ++ internal/locale/load.go | 2 +- internal/locale/localizer.go | 11 ++++++++++- 11 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 .air.toml create mode 100644 internal/handler/wizard/doc.go diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..c7f9cd3 --- /dev/null +++ b/.air.toml @@ -0,0 +1,10 @@ +root = "." + +[build] +exclude_regex = ["_test\\.go"] +bin = "/usr/bin/make debug" +cmd = "/usr/bin/make build" +kill_delay = 0 +log = "build-errors.log" +send_interrupt = true +stop_on_error = true diff --git a/.drone.yml b/.drone.yml index f687bbb..8c724a0 100644 --- a/.drone.yml +++ b/.drone.yml @@ -19,3 +19,5 @@ steps: trigger: event: - push + branch: + - master diff --git a/.gitignore b/.gitignore index 65036e6..7e28f15 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,8 @@ vendor/ # Go workspace file go.work .idea/ +.config/ +tmp/ # Env files .env diff --git a/Makefile b/Makefile index 47acc47..427e66c 100644 --- a/Makefile +++ b/Makefile @@ -2,20 +2,37 @@ SHELL = /bin/bash -o pipefail export PATH := $(shell go env GOPATH)/bin:$(PATH) ROOT_DIR=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) -BIN=$(ROOT_DIR)/build/vegapokerbot +BIN_NAME=vegapokerbot +BIN=$(ROOT_DIR)/build/$(BIN_NAME) GO_VERSION=$(shell go version | sed -e 's/go version //') BIN_DIR=$(ROOT_DIR)/build build: deps fmt - @echo "> building with ${GO_VERSION}" +ifeq ($(DEBUG), true) + @echo "> building debug with ${GO_VERSION}" + @CGO_ENABLED=0 go build -buildvcs=false -gcflags "all=-N -l" -tags=release -o $(BIN) . +else + @echo "> building release with ${GO_VERSION}" @CGO_ENABLED=0 go build -buildvcs=false -tags=release -o $(BIN) . +endif @echo $(BIN) docker: @docker buildx build --platform linux/amd64 --tag $CI_APP_IMAGE --file Dockerfile . run: +ifeq ($(DEBUG), true) + @killall -s 9 dlv > /dev/null 2>&1 || true + @killall -s 9 ${BIN_NAME} > /dev/null 2>&1 || true + @air +else @${BIN} run +endif + +debug: + @echo "> starting under delve" + @dlv --listen=:40000 --headless=true --api-version=2 --accept-multiclient --log exec ${BIN} run + fmt: @echo "> fmt" @gofmt -l -s -w `go list -buildvcs=false -f '{{.Dir}}' ${ROOT_DIR}/... | grep -v /vendor/` diff --git a/compose.prod.yml b/compose.prod.yml index c5ac712..bc01968 100644 --- a/compose.prod.yml +++ b/compose.prod.yml @@ -1,29 +1,23 @@ services: db: image: postgres:latest + restart: always environment: POSTGRES_USER: app POSTGRES_PASSWORD: app POSTGRES_DATABASE: app - networks: - - default app: image: "gitea.neur0tx.site/neur0toxine/vegapokerbot:latest" - networks: - - default links: - db env_file: - .env + restart: always labels: traefik.enable: "true" - traefik.http.routers.vegapokerbot.rule: Host(`vegapokerbot.neur0tx.site`) && PathPrefix(`/webhook8c31c4b2d65a87b4f3a6f10f8eb166fba9a2e5dc7696bc5291a7e69641dc5c21`) + traefik.http.routers.vegapokerbot.rule: Host(`vegapokerbot.example.com`) && PathPrefix(`/webhook`) traefik.http.routers.vegapokerbot.entrypoints: websecure traefik.http.services.vegapokerbot.loadbalancer.server.port: "3333" traefik.http.routers.vegapokerbot.tls: true traefik.http.routers.vegapokerbot.tls.certresolver: letsencrypt - -networks: - default: - driver: bridge diff --git a/compose.yml b/compose.yml index 81fb1a6..3dae2f2 100644 --- a/compose.yml +++ b/compose.yml @@ -22,8 +22,10 @@ services: - db environment: GOCACHE: /go + DEBUG: true # Set to false to disable hot-reload & debugger. ports: - 3333:3333 + - 40000:40000 command: make build run networks: diff --git a/internal/handler/new_message_handler.go b/internal/handler/new_message_handler.go index 5fdd167..3d3a670 100644 --- a/internal/handler/new_message_handler.go +++ b/internal/handler/new_message_handler.go @@ -30,10 +30,6 @@ func (h *MessageHandler) Handle(wh telego.Update) error { return group.NewPoll(h.App, wh.Message.From.ID, wh.Message.Chat.ID).Handle(wh) } - if util.MatchCommand("start", wh.Message) { - return wizard.NewRegister(h.App, wh.Message.From.ID, wh.Message.Chat.ID).Handle(wh) - } - setup, found := store.RedmineSetups.Get(wh.Message.Chat.ID) if found { return wizard.NewRedmineSetup(h.App, wh.Message.From.ID, wh.Message.Chat.ID, setup).Handle(wh) diff --git a/internal/handler/util/command.go b/internal/handler/util/command.go index a98c293..47e8055 100644 --- a/internal/handler/util/command.go +++ b/internal/handler/util/command.go @@ -8,3 +8,10 @@ import ( func MatchCommand(command string, msg *telego.Message) bool { return th.CommandEqual(command)(telego.Update{Message: msg}) } + +func HasCommand(msg *telego.Message) bool { + if msg == nil { + return false + } + return th.CommandRegexp.MatchString(msg.Text) +} diff --git a/internal/handler/wizard/doc.go b/internal/handler/wizard/doc.go new file mode 100644 index 0000000..629daea --- /dev/null +++ b/internal/handler/wizard/doc.go @@ -0,0 +1,2 @@ +// Package wizard contains setup wizard implementation. +package wizard diff --git a/internal/locale/load.go b/internal/locale/load.go index fb08868..e8fe0a1 100644 --- a/internal/locale/load.go +++ b/internal/locale/load.go @@ -32,7 +32,7 @@ func init() { } for _, tag := range tags { - localizers[tag.String()] = &localizer{loc: i18n.NewLocalizer(bundle, tag.String())} + localizers[tag.String()] = &localizer{loc: i18n.NewLocalizer(bundle, tag.String()), tag: tag} } } diff --git a/internal/locale/localizer.go b/internal/locale/localizer.go index c6853ca..ebe3ace 100644 --- a/internal/locale/localizer.go +++ b/internal/locale/localizer.go @@ -1,14 +1,19 @@ package locale -import "github.com/nicksnyder/go-i18n/v2/i18n" +import ( + "github.com/nicksnyder/go-i18n/v2/i18n" + "golang.org/x/text/language" +) type Localizer interface { Message(string) string Template(string, interface{}) string + Tag() language.Tag } type localizer struct { loc *i18n.Localizer + tag language.Tag } func (l *localizer) Message(str string) string { @@ -21,3 +26,7 @@ func (l *localizer) Template(str string, tpl interface{}) string { TemplateData: tpl, }) } + +func (l *localizer) Tag() language.Tag { + return l.tag +}