log middleware

This commit is contained in:
2025-09-29 09:14:39 +03:00
parent 49ffe9ae03
commit e15d56196d
27 changed files with 641 additions and 164 deletions

View File

@@ -1,39 +0,0 @@
package database
import (
"fmt"
"log"
"os"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type config struct {
Host string `json:"host"`
User string `json:"user"`
Password string `json:"password"`
Database string `json:"database"`
}
var Database *gorm.DB
func getDSN() string {
user := os.Getenv("PSQL_USER")
password := os.Getenv("PSQL_PASS")
database := os.Getenv("PSQL_NAME")
log.Println("user:", user, "database:", database)
return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", user, password, "postgres", database)
}
func Connect() {
var err error
Database, err = gorm.Open(postgres.Open(getDSN()), new(gorm.Config))
if err != nil {
log.Fatalln(err)
}
err = Database.AutoMigrate(&User{}, &Fraction{}, &Group{}, &ShopAuto{})
if err != nil {
log.Fatalln(err)
}
}

23
database/mdb/logs.go Normal file
View File

@@ -0,0 +1,23 @@
package mdb
import (
"context"
"kurumibot/laniakea"
"time"
)
type MessageLogEntry struct {
MessageID int `bson:"messageId"`
SenderID int `bson:"senderId"`
ChatID int `bson:"chatId"`
Text string `bson:"text"`
TimeStamp int64 `bson:"timestamp"`
}
func WriteMessageLog(db *laniakea.DatabaseContext, e *MessageLogEntry) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := db.MongoDB.Database("kurumi").Collection("msg_logs")
_, err := collection.InsertOne(ctx, e)
return err
}

27
database/mongo.go Normal file
View File

@@ -0,0 +1,27 @@
package database
import (
"fmt"
"log"
"os"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var MongoClient *mongo.Client
func ConnectMongo() {
var err error
opts := options.Client()
host, exists := os.LookupEnv("MONGO_HOST")
if !exists {
host = "localhost"
}
opts = opts.ApplyURI(fmt.Sprintf("mongodb://%s:%s@%s:27017", os.Getenv("MONGO_USER"), os.Getenv("MONGO_PASS"), host))
opts = opts.SetCompressors([]string{"snappy", "zlib", "zstd"})
MongoClient, err = mongo.Connect(opts)
if err != nil {
log.Fatalln(err)
}
}

32
database/postgresql.go Normal file
View File

@@ -0,0 +1,32 @@
package database
import (
"fmt"
"log"
"os"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var PostgresDatabase *gorm.DB
func getDSN() string {
user := os.Getenv("PSQL_USER")
password := os.Getenv("PSQL_PASS")
database := os.Getenv("PSQL_NAME")
host, exists := os.LookupEnv("PSQL_HOST")
if !exists {
host = "localhost"
}
log.Println("user:", user, "database:", database)
return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", user, password, host, database)
}
func ConnectPostgres() {
var err error
PostgresDatabase, err = gorm.Open(postgres.Open(getDSN()), new(gorm.Config))
if err != nil {
log.Fatalln(err)
}
}

View File

@@ -1,4 +1,4 @@
package database
package psql
import "github.com/shopspring/decimal"

View File

@@ -1,4 +1,4 @@
package database
package psql
import "github.com/shopspring/decimal"

10
database/psql/migrate.go Normal file
View File

@@ -0,0 +1,10 @@
package psql
import "kurumibot/database"
func Migrate() error {
return database.PostgresDatabase.AutoMigrate(
&User{}, &Fraction{}, &Group{}, &Waifu{}, &Work{},
&ShopAuto{}, &ShopBusiness{}, &ShopMaid{}, &ShopMiner{},
)
}

View File

@@ -1,4 +1,4 @@
package database
package psql
import "github.com/shopspring/decimal"
@@ -8,6 +8,10 @@ type ShopAuto struct {
Price decimal.Decimal
}
func (ShopAuto) TableName() string {
return "shop_auto"
}
type ShopBusiness struct {
ID int
Name string
@@ -15,6 +19,10 @@ type ShopBusiness struct {
Income decimal.Decimal
}
func (ShopBusiness) TableName() string {
return "shop_business"
}
type ShopMaid struct {
ID int
Name string
@@ -22,9 +30,17 @@ type ShopMaid struct {
Income decimal.Decimal
}
func (ShopMaid) TableName() string {
return "shop_maid"
}
type ShopMiner struct {
ID int
Name string
Price decimal.Decimal
Income decimal.Decimal
}
func (ShopMiner) TableName() string {
return "shop_miner"
}

View File

@@ -1,8 +1,10 @@
package database
package psql
import (
"database/sql"
"errors"
"kurumibot/database"
"log"
"math"
"time"
@@ -15,28 +17,28 @@ type User struct {
TelegramID int `gorm:"column:tg_id"`
Balance decimal.Decimal
Name string
GroupID int
GroupID int `gorm:"default:1"`
Group *Group
Level int
WorkID int
Level int `gorm:"default:1"`
Exp int `gorm:"default:0"`
WorkID int `gorm:"default:1"`
Work *Work
WorkTime time.Time
Exp int
AutoID int
AutoID sql.NullInt64
Auto *ShopAuto
BusinessID int
BusinessID sql.NullInt64
Business *ShopBusiness
MaidID int
MaidID sql.NullInt64
Maid *ShopMaid
MinerID int
MinerID sql.NullInt64
Miner *ShopMiner
IncomeTime time.Time
BTC decimal.Decimal
Invested decimal.Decimal
PairID int
PairID sql.NullInt64
Pair *User
Greeting string
Donat int
Greeting string `gorm:"size:255,default:Привет"`
Donat int `gorm:"default:0"`
FractionID sql.NullInt64
Fraction *Fraction
@@ -49,8 +51,13 @@ type User struct {
func GetOrCreateUser(tgId int, name string) (*User, error) {
user, err := GetUser(tgId)
log.Println(errors.Is(err, gorm.ErrRecordNotFound))
if errors.Is(err, gorm.ErrRecordNotFound) {
user, err = CreateUser(tgId, name)
_, err = CreateUser(tgId, name)
if err != nil {
return nil, err
}
return GetUser(tgId)
}
return user, err
}
@@ -60,19 +67,19 @@ func CreateUser(tgId int, name string) (*User, error) {
TelegramID: tgId,
Name: name,
}
tx := Database.Create(user)
tx := database.PostgresDatabase.Create(user)
return user, tx.Error
}
func GetUser(telegramId int) (*User, error) {
user := new(User)
tx := Database.Joins("Group").Joins("Work").Joins("Auto").Joins("Business").Joins("Maid").Joins("Miner").Joins("Fraction").Preload("Pair").Take(user, "tg_id=?", telegramId)
tx := database.PostgresDatabase.Joins("Group").Joins("Work").Joins("Auto").Joins("Business").Joins("Maid").Joins("Miner").Joins("Fraction").Preload("Pair").Take(user, "tg_id=?", telegramId)
return user, tx.Error
}
func GetAllUsers() ([]*User, error) {
users := make([]*User, 0)
tx := Database.Joins("Group").Joins("Work").Joins("Auto").Joins("Business").Joins("Maid").Joins("Miner").Joins("Fraction").Preload("Pair").Find(&users)
tx := database.PostgresDatabase.Joins("Group").Joins("Work").Joins("Auto").Joins("Business").Joins("Maid").Joins("Miner").Joins("Fraction").Preload("Pair").Find(&users)
return users, tx.Error
}

View File

@@ -1,6 +1,10 @@
package database
package psql
import "github.com/shopspring/decimal"
import (
"kurumibot/database"
"github.com/shopspring/decimal"
)
type Waifu struct {
ID int
@@ -18,36 +22,36 @@ type Waifu struct {
func GetAllWaifus() ([]*Waifu, error) {
waifus := make([]*Waifu, 0)
tx := Database.Joins("Owner").Find(&waifus).Order("id")
tx := database.PostgresDatabase.Joins("Owner").Find(&waifus).Order("id")
return waifus, tx.Error
}
func GetUserWaifus(userId int) ([]*Waifu, error) {
waifus := make([]*Waifu, 0)
tx := Database.Find(&waifus, "owner_id = ?", userId).Order("id")
tx := database.PostgresDatabase.Find(&waifus, "owner_id = ?", userId).Order("id")
return waifus, tx.Error
}
func GetFreeWaifus() ([]*Waifu, error) {
waifus := make([]*Waifu, 0)
tx := Database.Find(&waifus, "owner_id is null").Order("id")
tx := database.PostgresDatabase.Find(&waifus, "owner_id is null").Order("id")
return waifus, tx.Error
}
func GetFreeWaifusCount() (int64, error) {
var count int64 = 0
tx := Database.Model(&Waifu{}).Where("owner_id is null").Count(&count)
tx := database.PostgresDatabase.Model(&Waifu{}).Where("owner_id is null").Count(&count)
return count, tx.Error
}
func GetFreeWaifusWithRarity(rarity int) ([]*Waifu, error) {
waifus := make([]*Waifu, 0)
tx := Database.Find(&waifus, "owner_id is null and rarity = ?", rarity)
tx := database.PostgresDatabase.Find(&waifus, "owner_id is null and rarity = ?", rarity)
return waifus, tx.Error
}
func GetWaifuById(id int) (*Waifu, error) {
waifu := new(Waifu)
tx := Database.Joins("Owner").Find(waifu, id)
tx := database.PostgresDatabase.Joins("Owner").Find(waifu, id)
return waifu, tx.Error
}

View File

@@ -1,6 +1,10 @@
package database
package psql
import "github.com/shopspring/decimal"
import (
"kurumibot/database"
"github.com/shopspring/decimal"
)
type Work struct {
ID int
@@ -13,12 +17,12 @@ type Work struct {
func GetWorkById(id int) (*Work, error) {
work := new(Work)
tx := Database.First(work, id)
tx := database.PostgresDatabase.First(work, id)
return work, tx.Error
}
func GetAllWorks() ([]*Work, error) {
works := make([]*Work, 0)
tx := Database.Order("id").Find(&works)
tx := database.PostgresDatabase.Order("id").Find(&works)
return works, tx.Error
}

13
database/redis.go Normal file
View File

@@ -0,0 +1,13 @@
package database
import "github.com/redis/go-redis/v9"
var RedisClient *redis.Client
func ConnectRedis() {
RedisClient = redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "",
DB: 0,
})
}