[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/**
|
.idea/**
|
||||||
.vscode/**
|
.vscode/**
|
||||||
.vstags
|
.vstags
|
||||||
data/config.json
|
/data/config.json
|
||||||
data/*_db
|
/data/*_db
|
||||||
aleesa-telegram-go
|
/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
|
all: clean build
|
||||||
|
|
||||||
build:
|
build:
|
||||||
${GOOPTS} go build ${BUILDOPTS} -o aleesa-telegram-go
|
${GOOPTS} go build ${BUILDOPTS} -o aleesa-telegram-go ./cmd/aleesa-telegram-go
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
go clean
|
rm -rf aleesa-telegram-go
|
||||||
|
|
||||||
upgrade:
|
upgrade:
|
||||||
rm -rf vendor
|
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.
|
# Data_dir used not only for caches but for some settings and stats too.
|
||||||
"cachedir" : "data/cache",
|
"data_dir" : "data",
|
||||||
|
|
||||||
# Required for debugging reasons, if commented out or not exist, no debug logging appears at all.
|
# Required for debugging reasons, if commented out or not exist, no debug logging appears at all.
|
||||||
"log" : "/var/log/aleesa-telegram/debug.log",
|
"log" : "/var/log/aleesa-telegram/debug.log",
|
||||||
|
|
||||||
# Verbosity can be trace, debug, info (inform), notice, warning (warn), error (err), critical (crit, fatal), alert, emergency.
|
# Verbosity can be trace, debug, info, warn, error.
|
||||||
# Currently implemented debug, info, notice, warn, error, fatal. Notice is default logging level.
|
# Currently implemented debug, info, warn, error. Info is default logging level.
|
||||||
"loglevel": "notice",
|
"loglevel": "info",
|
||||||
|
|
||||||
# Stuff for telegram itself.
|
# 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 "!".
|
# 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.
|
# 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" : "!",
|
"csign" : "!",
|
||||||
|
|
||||||
"token" : "Long_string_given_by_@BotFather"
|
|
||||||
},
|
|
||||||
|
|
||||||
# Unprivileged user to run this bot from.
|
# Unprivileged user to run this bot from.
|
||||||
"user" : "nobody",
|
"user" : "nobody",
|
||||||
|
|
||||||
"redis_server": "redis",
|
"redis": {
|
||||||
"redis_port" : "6379",
|
"server": "redis",
|
||||||
"redis_channels" : [
|
"port" : 6379,
|
||||||
"telegram"
|
"channel" : "misc",
|
||||||
],
|
"my_channel": "telegram"
|
||||||
|
|
||||||
"redis_router_channel" : "misc"
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// vim: set ft=json noet ai ts=4 sw=4 sts=4:
|
||||||
|
|||||||
Reference in New Issue
Block a user