[WIP] Add missing cmd dir, fix makefile, fix sample_config.json, update .gitignore
This commit is contained in:
102
cmd/aleesa-telegram-go/main.go
Normal file
102
cmd/aleesa-telegram-go/main.go
Normal file
@@ -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: */
|
||||
Reference in New Issue
Block a user