[WIP] Add missing cmd dir, fix makefile, fix sample_config.json, update .gitignore
This commit is contained in:
8
.gitignore
vendored
8
.gitignore
vendored
@@ -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
|
||||
|
||||
4
Makefile
4
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
|
||||
|
||||
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: */
|
||||
@@ -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",
|
||||
|
||||
# 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" : {
|
||||
"telegram" : {
|
||||
"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" : "!",
|
||||
|
||||
"token" : "Long_string_given_by_@BotFather"
|
||||
},
|
||||
|
||||
# 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:
|
||||
|
||||
Reference in New Issue
Block a user