From c31547ce4aeb3aca01225b47c73be281e29bd639 Mon Sep 17 00:00:00 2001 From: eleksir Date: Fri, 31 Oct 2025 20:15:00 +0300 Subject: [PATCH] [WIP] Add missing cmd dir, fix makefile, fix sample_config.json, update .gitignore --- .gitignore | 8 ++- Makefile | 4 +- cmd/aleesa-telegram-go/main.go | 102 +++++++++++++++++++++++++++++++++ data/sample_config.json | 47 +++++++-------- 4 files changed, 133 insertions(+), 28 deletions(-) create mode 100644 cmd/aleesa-telegram-go/main.go diff --git a/.gitignore b/.gitignore index c2e214f..787eda4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .idea/** .vscode/** .vstags -data/config.json -data/*_db -aleesa-telegram-go +/data/config.json +/data/*_db +/data/cache +/aleesa-telegram-go +/*.log diff --git a/Makefile b/Makefile index 64d9663..607d69f 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,10 @@ BUILDOPTS=-ldflags="-s -w" -a -gcflags=all=-l -trimpath all: clean build build: - ${GOOPTS} go build ${BUILDOPTS} -o aleesa-telegram-go + ${GOOPTS} go build ${BUILDOPTS} -o aleesa-telegram-go ./cmd/aleesa-telegram-go clean: - go clean + rm -rf aleesa-telegram-go upgrade: rm -rf vendor diff --git a/cmd/aleesa-telegram-go/main.go b/cmd/aleesa-telegram-go/main.go new file mode 100644 index 0000000..a02740f --- /dev/null +++ b/cmd/aleesa-telegram-go/main.go @@ -0,0 +1,102 @@ +package main + +import ( + "context" + "fmt" + "os" + "os/signal" + "path/filepath" + "syscall" + + "aleesa-telegram-go/internal/log" + "aleesa-telegram-go/internal/tg" + + "github.com/go-redis/redis/v8" +) + +// init производит некоторую инициализацию перед запуском main(). +func init() { + var ( + err error + ) + + executablePath, err := os.Executable() + + if err != nil { + log.Fatalf("Unable to get current executable path: %s", err) + } + + configJSONPath := fmt.Sprintf("%s/data/config.json", filepath.Dir(executablePath)) + + locations := []string{ + "~/.aleesa-telegram-go.json", + "~/aleesa-telegram-go.json", + "/etc/aleesa-telegram-go.json", + configJSONPath, + } + + for _, location := range locations { + tg.Config, err = tg.ParseConfig(location) + + if err == nil { + break + } + + log.Errorf("Unable to parse config at %s: %s", location, err) + } + + if err != nil { + os.Exit(1) + } +} + +func main() { + var ( + logfile *os.File + err error + ) + + // Откроем лог и скормим его логгеру. + if tg.Config.Log != "" { + logfile, err = os.OpenFile(tg.Config.Log, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + + if err != nil { + log.Errorf("Unable to open log file %s: %s", tg.Config.Log, err) + os.Exit(1) + } + } + + log.Init(tg.Config.Loglevel, logfile) + + // Main context + var ctx = context.Background() + + // Инициализируем redis-клиента. + tg.RedisClient = redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%d", tg.Config.Redis.Server, tg.Config.Redis.Port), + }) + + log.Debugf("Lazy connect() to redis at %s:%d", tg.Config.Redis.Server, tg.Config.Redis.Port) + tg.Subscriber = tg.RedisClient.Subscribe(ctx, tg.Config.Redis.MyChannel) + redisMsgChan := tg.Subscriber.Channel() + + // Самое время поставить траппер сигналов. + signal.Notify(tg.SigChan, + syscall.SIGINT, + syscall.SIGTERM, + syscall.SIGQUIT) + + go tg.SigHandler() + go tg.Telega(tg.Config) + + // Обработчик событий от редиски. + for msg := range redisMsgChan { + if tg.Shutdown { + continue + } + + tg.RedisMsgParser(msg.Payload) + } +} + +/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */ diff --git a/data/sample_config.json b/data/sample_config.json index e6f622f..b88eb82 100644 --- a/data/sample_config.json +++ b/data/sample_config.json @@ -1,31 +1,32 @@ { - # Cachedir used not only for caches but for some settings and stats too. - "cachedir" : "data/cache", + # Data_dir used not only for caches but for some settings and stats too. + "data_dir" : "data", - # Required for debugging reasons, if commented out or not exist, no debug logging appears at all. - "log" : "/var/log/aleesa-telegram/debug.log", + # Required for debugging reasons, if commented out or not exist, no debug logging appears at all. + "log" : "/var/log/aleesa-telegram/debug.log", - # Verbosity can be trace, debug, info (inform), notice, warning (warn), error (err), critical (crit, fatal), alert, emergency. - # Currently implemented debug, info, notice, warn, error, fatal. Notice is default logging level. - "loglevel": "notice", + # Verbosity can be trace, debug, info, warn, error. + # Currently implemented debug, info, warn, error. Info is default logging level. + "loglevel": "info", - # Stuff for telegram itself. - "telegrambot" : { - # Prefix-sign for all commands. In many irc-bots (eggdrop/windrop) it is "." (dot), in sulci it was "!". - # In telegram commands begin with "/" on the start of first (and only) line. In case of this bot you can define what to use. - "csign" : "!", + # Stuff for telegram itself. + "telegram" : { + "token" : "Long_string_given_by_@BotFather" + }, - "token" : "Long_string_given_by_@BotFather" - }, + # Prefix-sign for all commands. In many irc-bots (eggdrop/windrop) it is "." (dot), in sulci it was "!". + # In telegram commands begin with "/" on the start of first (and only) line. In case of this bot you can define what to use. + "csign" : "!", - # Unprivileged user to run this bot from. - "user" : "nobody", + # Unprivileged user to run this bot from. + "user" : "nobody", - "redis_server": "redis", - "redis_port" : "6379", - "redis_channels" : [ - "telegram" - ], - - "redis_router_channel" : "misc" + "redis": { + "server": "redis", + "port" : 6379, + "channel" : "misc", + "my_channel": "telegram" + } } + +// vim: set ft=json noet ai ts=4 sw=4 sts=4: