Compare commits
1 Commits
v0.2.1
...
7f248fff62
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f248fff62 |
133
bot.go
133
bot.go
@@ -7,7 +7,12 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ParseMode string
|
type ParseMode string
|
||||||
@@ -26,8 +31,11 @@ type Bot struct {
|
|||||||
logger *Logger
|
logger *Logger
|
||||||
requestLogger *Logger
|
requestLogger *Logger
|
||||||
|
|
||||||
plugins []*Plugin
|
plugins []*Plugin
|
||||||
prefixes []string
|
middlewares []*Middleware
|
||||||
|
prefixes []string
|
||||||
|
|
||||||
|
dbContext *DatabaseContext
|
||||||
|
|
||||||
updateOffset int
|
updateOffset int
|
||||||
updateTypes []string
|
updateTypes []string
|
||||||
@@ -45,7 +53,14 @@ type BotSettings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoadSettingsFromEnv() *BotSettings {
|
func LoadSettingsFromEnv() *BotSettings {
|
||||||
|
return &BotSettings{
|
||||||
|
Token: os.Getenv("TG_TOKEN"),
|
||||||
|
Debug: os.Getenv("DEBUG") == "true",
|
||||||
|
ErrorTemplate: os.Getenv("ERROR_TEMPLATE"),
|
||||||
|
Prefixes: LoadPrefixesFromEnv(),
|
||||||
|
UpdateTypes: strings.Split(os.Getenv("UPDATE_TYPES"), ";"),
|
||||||
|
UseRequestLogger: os.Getenv("USE_REQ_LOG") == "true",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type MsgContext struct {
|
type MsgContext struct {
|
||||||
@@ -58,6 +73,12 @@ type MsgContext struct {
|
|||||||
Args []string
|
Args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DatabaseContext struct {
|
||||||
|
PostgresSQL *gorm.DB
|
||||||
|
MongoDB *mongo.Client
|
||||||
|
Redis *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
func NewBot(settings *BotSettings) *Bot {
|
func NewBot(settings *BotSettings) *Bot {
|
||||||
updateQueue := CreateQueue[*Update](256)
|
updateQueue := CreateQueue[*Update](256)
|
||||||
bot := &Bot{
|
bot := &Bot{
|
||||||
@@ -79,6 +100,7 @@ func NewBot(settings *BotSettings) *Bot {
|
|||||||
level = DEBUG
|
level = DEBUG
|
||||||
}
|
}
|
||||||
bot.logger = CreateLogger().Level(level).OpenFile(fmt.Sprintf("%s/main.log", strings.TrimRight(settings.LoggerBasePath, "/")))
|
bot.logger = CreateLogger().Level(level).OpenFile(fmt.Sprintf("%s/main.log", strings.TrimRight(settings.LoggerBasePath, "/")))
|
||||||
|
bot.logger = bot.logger.PrintTraceback(true)
|
||||||
if settings.UseRequestLogger {
|
if settings.UseRequestLogger {
|
||||||
bot.requestLogger = CreateLogger().Level(level).Prefix("REQUESTS").OpenFile(fmt.Sprintf("%s/requests.log", strings.TrimRight(settings.LoggerBasePath, "/")))
|
bot.requestLogger = CreateLogger().Level(level).Prefix("REQUESTS").OpenFile(fmt.Sprintf("%s/requests.log", strings.TrimRight(settings.LoggerBasePath, "/")))
|
||||||
}
|
}
|
||||||
@@ -95,6 +117,19 @@ func (b *Bot) Close() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) InitDatabaseContext(ctx *DatabaseContext) *Bot {
|
||||||
|
b.dbContext = ctx
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
func (b *Bot) AddDatabaseLogger(writer func(db *DatabaseContext) LoggerWriter) *Bot {
|
||||||
|
w := []LoggerWriter{writer(b.dbContext)}
|
||||||
|
b.logger.AddWriters(w)
|
||||||
|
if b.requestLogger != nil {
|
||||||
|
b.requestLogger.AddWriters(w)
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) UpdateTypes(t ...string) *Bot {
|
func (b *Bot) UpdateTypes(t ...string) *Bot {
|
||||||
b.updateTypes = make([]string, 0)
|
b.updateTypes = make([]string, 0)
|
||||||
b.updateTypes = append(b.updateTypes, t...)
|
b.updateTypes = append(b.updateTypes, t...)
|
||||||
@@ -111,11 +146,11 @@ func (b *Bot) AddPrefixes(prefixes ...string) *Bot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoadPrefixesFromEnv() []string {
|
func LoadPrefixesFromEnv() []string {
|
||||||
prefixesS, exists := os.LookupEnv("PREFIXES")
|
prefixes, exists := os.LookupEnv("PREFIXES")
|
||||||
if !exists {
|
if !exists {
|
||||||
return []string{"!"}
|
return []string{"!"}
|
||||||
}
|
}
|
||||||
return strings.Split(prefixesS, ";")
|
return strings.Split(prefixes, ";")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) ErrorTemplate(s string) *Bot {
|
func (b *Bot) ErrorTemplate(s string) *Bot {
|
||||||
@@ -136,6 +171,23 @@ func (b *Bot) AddPlugins(plugin ...*Plugin) *Bot {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) AddMiddleware(middleware ...*Middleware) *Bot {
|
||||||
|
sort.Slice(middleware, func(a, b int) bool {
|
||||||
|
first := middleware[a]
|
||||||
|
second := middleware[b]
|
||||||
|
if first.Order == second.Order {
|
||||||
|
return first.Name < second.Name
|
||||||
|
}
|
||||||
|
return middleware[a].Order < middleware[b].Order
|
||||||
|
})
|
||||||
|
|
||||||
|
b.middlewares = append(b.middlewares, middleware...)
|
||||||
|
for _, m := range middleware {
|
||||||
|
b.logger.Debug(fmt.Sprintf("middleware with name \"%s\" was registered", m.Name))
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) Run() {
|
func (b *Bot) Run() {
|
||||||
if len(b.prefixes) == 0 {
|
if len(b.prefixes) == 0 {
|
||||||
b.logger.Fatal("no prefixes defined")
|
b.logger.Fatal("no prefixes defined")
|
||||||
@@ -165,28 +217,31 @@ func (b *Bot) Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u := queue.Dequeue()
|
u := queue.Dequeue()
|
||||||
|
ctx := &MsgContext{
|
||||||
|
Bot: b,
|
||||||
|
Update: u,
|
||||||
|
}
|
||||||
|
for _, middleware := range b.middlewares {
|
||||||
|
middleware.Execute(ctx, b.dbContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, plugin := range b.plugins {
|
||||||
|
if plugin.UpdateListener != nil {
|
||||||
|
(*plugin.UpdateListener)(ctx, b.dbContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if u.CallbackQuery != nil {
|
if u.CallbackQuery != nil {
|
||||||
b.handleCallback(u)
|
b.handleCallback(u, ctx)
|
||||||
} else {
|
} else {
|
||||||
b.handleMessage(u)
|
b.handleMessage(u, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// {"callback_query":{"chat_instance":"6202057960757700762","data":"aboba","from":{"first_name":"scuroneko","id":314834933,"is_bot":false,"language_code":"ru","username":"scuroneko"},"id":"1352205741990111553","message":{"chat":{"first_name":"scuroneko","id":314834933,"type":"private","username":"scuroneko"},"date":1734338107,"from":{"first_name":"Kurumi","id":7718900880,"is_bot":true,"username":"kurumi_game_bot"},"message_id":19,"reply_markup":{"inline_keyboard":[[{"callback_data":"aboba","text":"Test"},{"callback_data":"another","text":"Another"}]]},"text":"Aboba"}},"update_id":350979488}
|
// {"callback_query":{"chat_instance":"6202057960757700762","data":"aboba","from":{"first_name":"scuroneko","id":314834933,"is_bot":false,"language_code":"ru","username":"scuroneko"},"id":"1352205741990111553","message":{"chat":{"first_name":"scuroneko","id":314834933,"type":"private","username":"scuroneko"},"date":1734338107,"from":{"first_name":"Kurumi","id":7718900880,"is_bot":true,"username":"kurumi_game_bot"},"message_id":19,"reply_markup":{"inline_keyboard":[[{"callback_data":"aboba","text":"Test"},{"callback_data":"another","text":"Another"}]]},"text":"Aboba"}},"update_id":350979488}
|
||||||
|
|
||||||
func (b *Bot) handleMessage(update *Update) {
|
func (b *Bot) handleMessage(update *Update, ctx *MsgContext) {
|
||||||
ctx := &MsgContext{
|
|
||||||
Bot: b,
|
|
||||||
Update: update,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, plugin := range b.plugins {
|
|
||||||
if plugin.UpdateListener != nil {
|
|
||||||
(*plugin.UpdateListener)(ctx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var text string
|
var text string
|
||||||
if update.Message == nil {
|
if update.Message == nil {
|
||||||
return
|
return
|
||||||
@@ -209,7 +264,6 @@ func (b *Bot) handleMessage(update *Update) {
|
|||||||
text = strings.TrimSpace(text[len(prefix):])
|
text = strings.TrimSpace(text[len(prefix):])
|
||||||
|
|
||||||
for _, plugin := range b.plugins {
|
for _, plugin := range b.plugins {
|
||||||
|
|
||||||
// Check every command
|
// Check every command
|
||||||
for cmd := range plugin.Commands {
|
for cmd := range plugin.Commands {
|
||||||
if !strings.HasPrefix(text, cmd) {
|
if !strings.HasPrefix(text, cmd) {
|
||||||
@@ -219,29 +273,18 @@ func (b *Bot) handleMessage(update *Update) {
|
|||||||
ctx.Text = strings.TrimSpace(text[len(cmd):])
|
ctx.Text = strings.TrimSpace(text[len(cmd):])
|
||||||
ctx.Args = strings.Split(ctx.Text, " ")
|
ctx.Args = strings.Split(ctx.Text, " ")
|
||||||
|
|
||||||
go plugin.Execute(cmd, ctx)
|
go plugin.Execute(cmd, ctx, b.dbContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) handleCallback(update *Update) {
|
func (b *Bot) handleCallback(update *Update, ctx *MsgContext) {
|
||||||
ctx := &MsgContext{
|
|
||||||
Bot: b,
|
|
||||||
Update: update,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, plugin := range b.plugins {
|
|
||||||
if plugin.UpdateListener != nil {
|
|
||||||
(*plugin.UpdateListener)(ctx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, plugin := range b.plugins {
|
for _, plugin := range b.plugins {
|
||||||
for payload := range plugin.Payloads {
|
for payload := range plugin.Payloads {
|
||||||
if !strings.HasPrefix(update.CallbackQuery.Data, payload) {
|
if !strings.HasPrefix(update.CallbackQuery.Data, payload) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go plugin.ExecutePayload(payload, ctx)
|
go plugin.ExecutePayload(payload, ctx, b.dbContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -259,7 +302,7 @@ func (ctx *MsgContext) Answer(text string) {
|
|||||||
_, err := ctx.Bot.SendMessage(&SendMessageP{
|
_, err := ctx.Bot.SendMessage(&SendMessageP{
|
||||||
ChatID: ctx.Msg.Chat.ID,
|
ChatID: ctx.Msg.Chat.ID,
|
||||||
Text: text,
|
Text: text,
|
||||||
ParseMode: "markdown",
|
ParseMode: ParseMD,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Bot.logger.Error(err)
|
ctx.Bot.logger.Error(err)
|
||||||
@@ -294,21 +337,21 @@ func (b *Bot) Logger() *Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ApiResponse struct {
|
type ApiResponse struct {
|
||||||
Ok bool `json:"ok"`
|
Ok bool `json:"ok"`
|
||||||
Result map[string]interface{} `json:"result,omitempty"`
|
Result map[string]any `json:"result,omitempty"`
|
||||||
Description string `json:"description,omitempty"`
|
Description string `json:"description,omitempty"`
|
||||||
ErrorCode int `json:"error_code,omitempty"`
|
ErrorCode int `json:"error_code,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApiResponseA struct {
|
type ApiResponseA struct {
|
||||||
Ok bool `json:"ok"`
|
Ok bool `json:"ok"`
|
||||||
Result []interface{} `json:"result,omitempty"`
|
Result []any `json:"result,omitempty"`
|
||||||
Description string `json:"description,omitempty"`
|
Description string `json:"description,omitempty"`
|
||||||
ErrorCode int `json:"error_code,omitempty"`
|
ErrorCode int `json:"error_code,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// request is a low-level call to api.
|
// request is a low-level call to api.
|
||||||
func (b *Bot) request(methodName string, params map[string]interface{}) (map[string]interface{}, error) {
|
func (b *Bot) request(methodName string, params map[string]any) (map[string]any, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := json.NewEncoder(&buf).Encode(params)
|
err := json.NewEncoder(&buf).Encode(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -333,7 +376,7 @@ func (b *Bot) request(methodName string, params map[string]interface{}) (map[str
|
|||||||
}
|
}
|
||||||
response := new(ApiResponse)
|
response := new(ApiResponse)
|
||||||
|
|
||||||
var result map[string]interface{}
|
var result map[string]any
|
||||||
|
|
||||||
err = json.Unmarshal(data, &response)
|
err = json.Unmarshal(data, &response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
84
logger.go
84
logger.go
@@ -5,17 +5,21 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type LoggerWriter func(level LogLevel, prefix, traceback string, message []any)
|
||||||
|
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
prefix string
|
prefix string
|
||||||
level LogLevel
|
level LogLevel
|
||||||
printTraceback bool
|
printTraceback bool
|
||||||
printTime bool
|
printTime bool
|
||||||
|
writers []LoggerWriter
|
||||||
|
|
||||||
f *os.File
|
f *os.File
|
||||||
}
|
}
|
||||||
@@ -26,6 +30,10 @@ type LogLevel struct {
|
|||||||
c color.Attribute
|
c color.Attribute
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LogLevel) GetName() string {
|
||||||
|
return l.t
|
||||||
|
}
|
||||||
|
|
||||||
type MethodTraceback struct {
|
type MethodTraceback struct {
|
||||||
Package string
|
Package string
|
||||||
Method string
|
Method string
|
||||||
@@ -36,11 +44,11 @@ type MethodTraceback struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
INFO LogLevel = LogLevel{n: 0, t: "info", c: color.FgWhite}
|
INFO = LogLevel{n: 0, t: "info", c: color.FgWhite}
|
||||||
WARN LogLevel = LogLevel{n: 1, t: "warn", c: color.FgHiYellow}
|
WARN = LogLevel{n: 1, t: "warn", c: color.FgHiYellow}
|
||||||
ERROR LogLevel = LogLevel{n: 2, t: "error", c: color.FgHiRed}
|
ERROR = LogLevel{n: 2, t: "error", c: color.FgHiRed}
|
||||||
FATAL LogLevel = LogLevel{n: 3, t: "fatal", c: color.FgRed}
|
FATAL = LogLevel{n: 3, t: "fatal", c: color.FgRed}
|
||||||
DEBUG LogLevel = LogLevel{n: 4, t: "debug", c: color.FgGreen}
|
DEBUG = LogLevel{n: 4, t: "debug", c: color.FgGreen}
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateLogger() *Logger {
|
func CreateLogger() *Logger {
|
||||||
@@ -76,6 +84,14 @@ func (l *Logger) PrintTraceback(b bool) *Logger {
|
|||||||
l.printTraceback = b
|
l.printTraceback = b
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
func (l *Logger) PrintTime(b bool) *Logger {
|
||||||
|
l.printTime = b
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
func (l *Logger) AddWriters(writers []LoggerWriter) *Logger {
|
||||||
|
l.writers = append(l.writers, writers...)
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
func (l *Logger) Info(m ...any) {
|
func (l *Logger) Info(m ...any) {
|
||||||
l.print(INFO, m)
|
l.print(INFO, m)
|
||||||
@@ -127,6 +143,47 @@ func (l *Logger) formatTraceback(mt *MethodTraceback) string {
|
|||||||
return fmt.Sprintf("%s:%s:%d", mt.filename, mt.Method, mt.line)
|
return fmt.Sprintf("%s:%s:%d", mt.filename, mt.Method, mt.line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Logger) getFullTraceback(skip int) []*MethodTraceback {
|
||||||
|
pc := make([]uintptr, 15)
|
||||||
|
runtime.Callers(skip, pc)
|
||||||
|
list := make([]*MethodTraceback, 0)
|
||||||
|
frames := runtime.CallersFrames(pc)
|
||||||
|
for {
|
||||||
|
frame, more := frames.Next()
|
||||||
|
if !more {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
details := runtime.FuncForPC(frame.PC)
|
||||||
|
signature := details.Name()
|
||||||
|
path, line := details.FileLine(frame.PC)
|
||||||
|
splitPath := strings.Split(path, "/")
|
||||||
|
|
||||||
|
splitSignature := strings.Split(signature, ".")
|
||||||
|
pkg, method := splitSignature[0], splitSignature[len(splitSignature)-1]
|
||||||
|
|
||||||
|
tb := &MethodTraceback{
|
||||||
|
filename: splitPath[len(splitPath)-1],
|
||||||
|
fullPath: path,
|
||||||
|
line: line,
|
||||||
|
signature: signature,
|
||||||
|
Package: pkg,
|
||||||
|
Method: method,
|
||||||
|
}
|
||||||
|
list = append(list, tb)
|
||||||
|
}
|
||||||
|
sort.Slice(list, func(i, j int) bool {
|
||||||
|
return j < i
|
||||||
|
})
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
func (l *Logger) formatFullTraceback(tracebacks []*MethodTraceback) string {
|
||||||
|
formatted := make([]string, 0)
|
||||||
|
for _, tb := range tracebacks {
|
||||||
|
formatted = append(formatted, l.formatTraceback(tb))
|
||||||
|
}
|
||||||
|
return strings.Join(formatted, "->")
|
||||||
|
}
|
||||||
|
|
||||||
func (l *Logger) buildString(level LogLevel, m []any) string {
|
func (l *Logger) buildString(level LogLevel, m []any) string {
|
||||||
args := []string{
|
args := []string{
|
||||||
fmt.Sprintf("[%s]", l.prefix),
|
fmt.Sprintf("[%s]", l.prefix),
|
||||||
@@ -152,11 +209,22 @@ func (l *Logger) print(level LogLevel, m []any) {
|
|||||||
if l.level.n < level.n {
|
if l.level.n < level.n {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
color.New(level.c).Println(l.buildString(level, m))
|
_, err := color.New(level.c).Println(l.buildString(level, m))
|
||||||
|
if err != nil {
|
||||||
|
l.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, writer := range l.writers {
|
||||||
|
writer(level, l.prefix, l.formatFullTraceback(l.getFullTraceback(4)), m)
|
||||||
|
}
|
||||||
|
|
||||||
if l.f != nil {
|
if l.f != nil {
|
||||||
if _, err := l.f.Write([]byte(l.buildString(level, m) + "\n")); err != nil {
|
writeToFiles := os.Getenv("WRITE_TO_FILE")
|
||||||
l.Fatal(err)
|
if writeToFiles != "false" {
|
||||||
|
if _, err := l.f.Write([]byte(l.buildString(level, m) + "\n")); err != nil {
|
||||||
|
l.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
59
plugins.go
59
plugins.go
@@ -1,6 +1,6 @@
|
|||||||
package laniakea
|
package laniakea
|
||||||
|
|
||||||
type CommandExecutor func(ctx *MsgContext)
|
type CommandExecutor func(ctx *MsgContext, dbContext *DatabaseContext)
|
||||||
|
|
||||||
type PluginBuilder struct {
|
type PluginBuilder struct {
|
||||||
name string
|
name string
|
||||||
@@ -56,10 +56,59 @@ func (p *PluginBuilder) Build() *Plugin {
|
|||||||
return plugin
|
return plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Plugin) Execute(cmd string, ctx *MsgContext) {
|
func (p *Plugin) Execute(cmd string, ctx *MsgContext, dbContext *DatabaseContext) {
|
||||||
(*p.Commands[cmd])(ctx)
|
(*p.Commands[cmd])(ctx, dbContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Plugin) ExecutePayload(payload string, ctx *MsgContext) {
|
func (p *Plugin) ExecutePayload(payload string, ctx *MsgContext, dbContext *DatabaseContext) {
|
||||||
(*p.Payloads[payload])(ctx)
|
(*p.Payloads[payload])(ctx, dbContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Middleware struct {
|
||||||
|
Name string
|
||||||
|
Executor *CommandExecutor
|
||||||
|
Order int
|
||||||
|
Async bool
|
||||||
|
}
|
||||||
|
type MiddlewareBuilder struct {
|
||||||
|
name string
|
||||||
|
executor *CommandExecutor
|
||||||
|
order int
|
||||||
|
async bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMiddleware(name string) *MiddlewareBuilder {
|
||||||
|
return &MiddlewareBuilder{name: name, async: false}
|
||||||
|
}
|
||||||
|
func (m *MiddlewareBuilder) SetName(name string) *MiddlewareBuilder {
|
||||||
|
m.name = name
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
func (m *MiddlewareBuilder) SetExecutor(executor CommandExecutor) *MiddlewareBuilder {
|
||||||
|
m.executor = &executor
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
func (m *MiddlewareBuilder) SetOrder(order int) *MiddlewareBuilder {
|
||||||
|
m.order = order
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
func (m *MiddlewareBuilder) SetAsync(async bool) *MiddlewareBuilder {
|
||||||
|
m.async = async
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
func (m *MiddlewareBuilder) Build() *Middleware {
|
||||||
|
return &Middleware{
|
||||||
|
Name: m.name,
|
||||||
|
Executor: m.executor,
|
||||||
|
Order: m.order,
|
||||||
|
Async: m.async,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (m *Middleware) Execute(ctx *MsgContext, db *DatabaseContext) {
|
||||||
|
exec := *m.Executor
|
||||||
|
if m.Async {
|
||||||
|
go exec(ctx, db)
|
||||||
|
} else {
|
||||||
|
exec(ctx, db)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
queue.go
26
queue.go
@@ -1,12 +1,18 @@
|
|||||||
package laniakea
|
package laniakea
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type Queue[T any] struct {
|
type Queue[T any] struct {
|
||||||
queue []T
|
|
||||||
size uint64
|
size uint64
|
||||||
|
mu sync.RWMutex
|
||||||
|
queue []T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var QueueFullError = errors.New("queue full")
|
||||||
|
|
||||||
func CreateQueue[T any](size uint64) *Queue[T] {
|
func CreateQueue[T any](size uint64) *Queue[T] {
|
||||||
return &Queue[T]{
|
return &Queue[T]{
|
||||||
queue: make([]T, 0),
|
queue: make([]T, 0),
|
||||||
@@ -16,18 +22,20 @@ func CreateQueue[T any](size uint64) *Queue[T] {
|
|||||||
|
|
||||||
func (q *Queue[T]) Enqueue(el T) error {
|
func (q *Queue[T]) Enqueue(el T) error {
|
||||||
if q.IsFull() {
|
if q.IsFull() {
|
||||||
return fmt.Errorf("queue full")
|
return QueueFullError
|
||||||
}
|
}
|
||||||
q.queue = append(q.queue, el)
|
q.queue = append(q.queue, el)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue[T]) Peak() T {
|
func (q *Queue[T]) Peak() T {
|
||||||
|
q.mu.RLock()
|
||||||
|
defer q.mu.RUnlock()
|
||||||
return q.queue[0]
|
return q.queue[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue[T]) IsEmpty() bool {
|
func (q *Queue[T]) IsEmpty() bool {
|
||||||
return len(q.queue) == 0
|
return q.Length() == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue[T]) IsFull() bool {
|
func (q *Queue[T]) IsFull() bool {
|
||||||
@@ -35,16 +43,26 @@ func (q *Queue[T]) IsFull() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue[T]) Length() uint64 {
|
func (q *Queue[T]) Length() uint64 {
|
||||||
|
q.mu.RLock()
|
||||||
|
defer q.mu.RUnlock()
|
||||||
return uint64(len(q.queue))
|
return uint64(len(q.queue))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue[T]) Dequeue() T {
|
func (q *Queue[T]) Dequeue() T {
|
||||||
|
q.mu.RLock()
|
||||||
el := q.queue[0]
|
el := q.queue[0]
|
||||||
|
q.mu.RUnlock()
|
||||||
|
|
||||||
if q.Length() == 1 {
|
if q.Length() == 1 {
|
||||||
|
q.mu.Lock()
|
||||||
q.queue = make([]T, 0)
|
q.queue = make([]T, 0)
|
||||||
|
q.mu.Unlock()
|
||||||
return el
|
return el
|
||||||
}
|
}
|
||||||
|
|
||||||
|
q.mu.Lock()
|
||||||
q.queue = q.queue[1:]
|
q.queue = q.queue[1:]
|
||||||
|
q.mu.Unlock()
|
||||||
return el
|
return el
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
utils.go
10
utils.go
@@ -2,26 +2,26 @@ package laniakea
|
|||||||
|
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
|
|
||||||
func MapToStruct(m map[string]interface{}, s interface{}) error {
|
func MapToStruct(m map[string]any, dst interface{}) error {
|
||||||
data, err := json.Marshal(m)
|
data, err := json.Marshal(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(data, s)
|
err = json.Unmarshal(data, dst)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func MapToJson(m map[string]interface{}) (string, error) {
|
func MapToJson(m map[string]any) (string, error) {
|
||||||
data, err := json.Marshal(m)
|
data, err := json.Marshal(m)
|
||||||
return string(data), err
|
return string(data), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func StructToMap(s interface{}) (map[string]interface{}, error) {
|
func StructToMap(s interface{}) (map[string]any, error) {
|
||||||
data, err := json.Marshal(s)
|
data, err := json.Marshal(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m := make(map[string]interface{})
|
m := make(map[string]any)
|
||||||
err = json.Unmarshal(data, &m)
|
err = json.Unmarshal(data, &m)
|
||||||
return m, err
|
return m, err
|
||||||
}
|
}
|
||||||
|
|||||||
12
version.go
12
version.go
@@ -1,8 +1,12 @@
|
|||||||
package laniakea
|
package laniakea
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION_STRING = "0.1.4"
|
VersionString = "0.1.4"
|
||||||
VERSION_MAJOR = 0
|
VersionMajor = 0
|
||||||
VERSION_MINOR = 1
|
VersionMinor = 1
|
||||||
VERSION_PATCH = 4
|
VersionPatch = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var GoVersion = os.Getenv("GoV")
|
||||||
|
|||||||
Reference in New Issue
Block a user