121 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			121 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"github.com/sirupsen/logrus"
 | 
						|
	"os"
 | 
						|
 | 
						|
	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
 | 
						|
)
 | 
						|
 | 
						|
type contextKey uint8
 | 
						|
 | 
						|
const (
 | 
						|
	_ contextKey = iota
 | 
						|
	ctxKeyLogger
 | 
						|
	ctxKeyBotApi
 | 
						|
	ctxKeyConfig
 | 
						|
	ctxKeyCommandHandler
 | 
						|
	ctxKeyMessageCleaner
 | 
						|
	ctxKeyDataBase
 | 
						|
	ctxKeyPlayersBoard
 | 
						|
)
 | 
						|
 | 
						|
func createLogger(debug bool) *logrus.Logger {
 | 
						|
	logLevel := logrus.InfoLevel
 | 
						|
	if debug {
 | 
						|
		logLevel = logrus.DebugLevel
 | 
						|
	}
 | 
						|
 | 
						|
	return &logrus.Logger{
 | 
						|
		Out: os.Stderr,
 | 
						|
		Formatter: &logrus.TextFormatter{
 | 
						|
			FullTimestamp:   true,
 | 
						|
			TimestampFormat: "02 Jan 06 15:04",
 | 
						|
			//PadLevelText:     true,
 | 
						|
			QuoteEmptyFields: true,
 | 
						|
		},
 | 
						|
		Hooks:        make(logrus.LevelHooks),
 | 
						|
		Level:        logLevel,
 | 
						|
		ExitFunc:     os.Exit,
 | 
						|
		ReportCaller: false,
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func createContext(config *Config, bot *tgbotapi.BotAPI, mc *messageCleaner, ch *commandHandler, db *dbLayer, pb *playersBoard) (context.Context, func()) {
 | 
						|
	ctx, cancelFunc := context.WithCancel(context.Background())
 | 
						|
 | 
						|
	ctx = context.WithValue(ctx, ctxKeyLogger, createLogger(true))
 | 
						|
	ctx = context.WithValue(ctx, ctxKeyBotApi, bot)
 | 
						|
	ctx = context.WithValue(ctx, ctxKeyConfig, config)
 | 
						|
	ctx = context.WithValue(ctx, ctxKeyMessageCleaner, mc)
 | 
						|
	ctx = context.WithValue(ctx, ctxKeyCommandHandler, ch)
 | 
						|
	ctx = context.WithValue(ctx, ctxKeyDataBase, db)
 | 
						|
	ctx = context.WithValue(ctx, ctxKeyPlayersBoard, pb)
 | 
						|
 | 
						|
	return ctx, cancelFunc
 | 
						|
}
 | 
						|
 | 
						|
func getLogger(ctx context.Context) *logrus.Logger {
 | 
						|
	v := ctx.Value(ctxKeyLogger)
 | 
						|
	if logger, ok := v.(*logrus.Logger); ok {
 | 
						|
		return logger
 | 
						|
	}
 | 
						|
 | 
						|
	panic("Failed to get logger from ctx")
 | 
						|
}
 | 
						|
 | 
						|
func getBotApi(ctx context.Context) *tgbotapi.BotAPI {
 | 
						|
	v := ctx.Value(ctxKeyBotApi)
 | 
						|
	if botApi, ok := v.(*tgbotapi.BotAPI); ok {
 | 
						|
		return botApi
 | 
						|
	}
 | 
						|
 | 
						|
	panic("Failed to get bot api from ctx")
 | 
						|
}
 | 
						|
 | 
						|
func getConfig(ctx context.Context) *Config {
 | 
						|
	v := ctx.Value(ctxKeyConfig)
 | 
						|
	if config, ok := v.(*Config); ok {
 | 
						|
		return config
 | 
						|
	}
 | 
						|
 | 
						|
	panic("Failed to get config from ctx")
 | 
						|
}
 | 
						|
 | 
						|
func getMessageCleaner(ctx context.Context) *messageCleaner {
 | 
						|
	v := ctx.Value(ctxKeyMessageCleaner)
 | 
						|
	if mc, ok := v.(*messageCleaner); ok {
 | 
						|
		return mc
 | 
						|
	}
 | 
						|
 | 
						|
	panic("Failed to get message cleaner from ctx")
 | 
						|
}
 | 
						|
 | 
						|
func getCommandHandler(ctx context.Context) *commandHandler {
 | 
						|
	v := ctx.Value(ctxKeyCommandHandler)
 | 
						|
	if ch, ok := v.(*commandHandler); ok {
 | 
						|
		return ch
 | 
						|
	}
 | 
						|
 | 
						|
	panic("Failed to get command handler from ctx")
 | 
						|
}
 | 
						|
 | 
						|
func getDB(ctx context.Context) *dbLayer {
 | 
						|
	v := ctx.Value(ctxKeyDataBase)
 | 
						|
	if db, ok := v.(*dbLayer); ok {
 | 
						|
		return db
 | 
						|
	}
 | 
						|
 | 
						|
	panic("Failed to get db from ctx")
 | 
						|
}
 | 
						|
 | 
						|
func getDPlayersBoard(ctx context.Context) *playersBoard {
 | 
						|
	v := ctx.Value(ctxKeyPlayersBoard)
 | 
						|
	if pb, ok := v.(*playersBoard); ok {
 | 
						|
		return pb
 | 
						|
	}
 | 
						|
 | 
						|
	panic("Failed to get players board from ctx")
 | 
						|
}
 |