Commit 1f374aed authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update

parent 1d224a83
debug = true
[mysql]
host = "aon-db"
port = 3306
user = "root"
password = "XN2UARuys3zy4Oux"
database = "tg"
max_conn = 10
max_idle_conn = 2
[pgsql]
host = "aws-0-ap-northeast-1.pooler.supabase.com"
port = 5432
user = "postgres.vbxtvjffhsirnyxjcuku"
password = ""
database = "postgres"
max_conn = 3
max_idle_conn = 3
enable_log = true
cert_file = "db.crt"
[server]
listen = "0.0.0.0:8080"
......
......@@ -8,12 +8,12 @@ import (
type Config struct {
Debug bool `toml:"debug"`
MySQL MysqlConfig `toml:"mysql"`
PGSQL PGSQLConfig `toml:"pgsql"`
Server ServerConfig `toml:"server"`
TGBot TGBotConfig `toml:"tg_bot"`
}
type MysqlConfig struct {
type PGSQLConfig struct {
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
......@@ -22,6 +22,7 @@ type MysqlConfig struct {
MaxConn int `toml:"max_conn"`
MaxIdleConn int `toml:"max_idle_conn"`
EnableLog bool `toml:"enable_log"`
CertFile string `toml:"cert_file"`
}
type ServerConfig struct {
......
......@@ -6,7 +6,7 @@ import (
dbModel "sdk_api/model/db"
"time"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
......@@ -22,14 +22,24 @@ func New(_c *config.Config) (dao *Dao, err error) {
c: _c,
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True",
_c.MySQL.User, _c.MySQL.Password, _c.MySQL.Host, _c.MySQL.Port, _c.MySQL.Database)
// dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True",
// _c.MySQL.User, _c.MySQL.Password, _c.MySQL.Host, _c.MySQL.Port, _c.MySQL.Database)
// dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=verify-ca sslrootcert=ca.crt",
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d",
_c.PGSQL.Host, _c.PGSQL.User, _c.PGSQL.Password, _c.PGSQL.Database, _c.PGSQL.Port,
)
if _c.PGSQL.CertFile != "" {
dsn = fmt.Sprintf("%s sslmode=require sslrootcert=%s", dsn, _c.PGSQL.CertFile)
}
lgr := logger.Default
if _c.MySQL.EnableLog {
if _c.PGSQL.EnableLog {
lgr = logger.Default.LogMode(logger.Info)
}
dao.db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
dao.db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
......@@ -42,8 +52,8 @@ func New(_c *config.Config) (dao *Dao, err error) {
if err != nil {
return
}
sqlDB.SetMaxOpenConns(_c.MySQL.MaxConn)
sqlDB.SetMaxIdleConns(_c.MySQL.MaxIdleConn)
sqlDB.SetMaxOpenConns(_c.PGSQL.MaxConn)
sqlDB.SetMaxIdleConns(_c.PGSQL.MaxIdleConn)
sqlDB.SetConnMaxIdleTime(time.Hour)
err = dao.db.AutoMigrate(&dbModel.User{}, &dbModel.Active{}, &dbModel.ChatGroup{})
if err != nil {
......
package dao
import (
"fmt"
dbModel "sdk_api/model/db"
"gorm.io/gorm"
......@@ -9,6 +10,7 @@ import (
func (d *Dao) CreateUser(user *dbModel.User) (err error) {
return d.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "user_id"}, {Name: "chat_id"}},
DoUpdates: clause.Assignments(map[string]interface{}{"left_at": gorm.Expr("NULL")}),
}).Create(user).Error
}
......@@ -16,14 +18,15 @@ func (d *Dao) CreateUser(user *dbModel.User) (err error) {
func (d *Dao) IncrMessageCount(a *dbModel.Active) (err error) {
a.MsgCount = 1
return d.db.Clauses(clause.OnConflict{
DoUpdates: clause.Assignments(map[string]interface{}{"msg_count": gorm.Expr("msg_count + ?", 1)}),
Columns: []clause.Column{{Name: "user_id"}, {Name: "chat_id"}, {Name: "unix_day"}},
DoUpdates: clause.Assignments(map[string]interface{}{"msg_count": gorm.Expr(fmt.Sprintf("%s.msg_count + ?", a.TableName()), 1)}),
}).Create(a).Error
}
func (d *Dao) GetUserActiveMsgCount(userId, chatId, unixDay int) (count int, err error) {
a := dbModel.Active{}
err = d.db.Model(&dbModel.Active{}).
Where("`user_id` = ? AND `chat_id` = ? AND `unix_day` = ?", userId, chatId, unixDay).
Where("user_id = ? AND chat_id = ? AND unix_day = ?", userId, chatId, unixDay).
First(&a).Error
if err == gorm.ErrRecordNotFound {
......@@ -36,7 +39,7 @@ func (d *Dao) GetUserActiveMsgCount(userId, chatId, unixDay int) (count int, err
func (d *Dao) UserExist(userId, chatId int) (exist bool, err error) {
a := dbModel.User{}
err = d.db.Model(&dbModel.User{}).
Where("`user_id` = ? AND `chat_id` = ? AND `left_at` IS NULL", userId, chatId).
Where("user_id = ? AND chat_id = ? AND left_at IS NULL", userId, chatId).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return false, nil
......@@ -46,14 +49,14 @@ func (d *Dao) UserExist(userId, chatId int) (exist bool, err error) {
func (d *Dao) UserLeft(userId, chatId int) (err error) {
return d.db.Model(&dbModel.User{}).
Where("`user_id` = ? AND `chat_id` = ? AND `left_at` IS NULL", userId, chatId).
Where("user_id = ? AND chat_id = ? AND left_at IS NULL", userId, chatId).
Update("left_at", gorm.Expr("NOW()")).Error
}
func (d *Dao) IsSupportedChat(chatId int) (exist bool, err error) {
a := dbModel.ChatGroup{}
err = d.db.Model(&dbModel.ChatGroup{}).
Where("`chat_id` = ?", chatId).
Where("chat_id = ?", chatId).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return false, nil
......
......@@ -15,6 +15,7 @@ services:
volumes:
- ./conf/tg-messenger/config.toml:/config.toml
- ./data/tg-messenger/db.crt:/app/db.crt
- ./data/tg-messenger/api-log:/app
command:
- "/bin/sh"
......@@ -33,6 +34,7 @@ services:
volumes:
- ./conf/tg-messenger/config.toml:/config.toml
- ./data/tg-messenger/db.crt:/app/db.crt
- ./data/tg-messenger/messenger-log:/app
command:
- "/bin/sh"
......
......@@ -10,7 +10,7 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/sirupsen/logrus v1.9.0
github.com/tidwall/gjson v1.17.1
gorm.io/driver/mysql v1.5.6
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.10
)
......@@ -24,8 +24,11 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
......@@ -43,6 +46,7 @@ require (
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
......
......@@ -28,8 +28,6 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
......@@ -39,6 +37,14 @@ github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVI
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
......@@ -100,6 +106,8 @@ golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
......@@ -117,9 +125,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
......
......@@ -7,16 +7,20 @@ import (
)
type User struct {
Id int `gorm:"primaryKey"`
Id int `gorm:"primaryKey;autoIncrement"`
UserId int `gorm:"type:int;uniqueIndex:uidx_uid_cid;not null;comment:telegram用户id"`
Username string `gorm:"type:varchar(255);not null;comment:telegram用户名"`
Username string `gorm:"type:text;not null;comment:telegram用户名"`
ChatId int `gorm:"type:int;uniqueIndex:uidx_uid_cid;not null;comment:telegram群id"`
LeftAt sql.NullTime `gorm:"index;comment:退出时间"`
gorm.Model
}
func (*User) TableName() string {
return "telegram_task.user"
}
type Active struct {
Id int `gorm:"primaryKey"`
Id int `gorm:"primaryKey;autoIncrement"`
UserId int `gorm:"type:int;uniqueIndex:uidx_uid_cid_day;not null;comment:telegram用户id"`
ChatId int `gorm:"type:int;uniqueIndex:uidx_uid_cid_day;not null;comment:telegram群id"`
UnixDay int `gorm:"type:int;not null;uniqueIndex:uidx_uid_cid_day;comment:unix day"`
......@@ -24,10 +28,18 @@ type Active struct {
gorm.Model
}
func (*Active) TableName() string {
return "telegram_task.active"
}
type ChatGroup struct {
Id int `gorm:"primaryKey"`
Id int `gorm:"primaryKey;autoIncrement"`
ChatId int `gorm:"type:int;uniqueIndex;not null;comment:telegram群id"`
ChatTitle string `gorm:"type:varchar(255);not null;comment:telegram群名"`
Description string `gorm:"type:varchar(255);comment:群描述"`
ChatTitle string `gorm:"type:text;not null;comment:telegram群名"`
Description string `gorm:"type:text;comment:群描述"`
gorm.Model
}
func (*ChatGroup) TableName() string {
return "telegram_task.chat_group"
}
......@@ -24,7 +24,7 @@ func isJoined(c *gin.Context) {
c.JSON(200, withError(constant.InternalError))
return
}
c.JSON(200, gin.H{"joined": joined})
c.JSON(200, withSuccess(gin.H{"joined": joined}))
return
}
......@@ -45,5 +45,5 @@ func active(c *gin.Context) {
c.JSON(200, withError(constant.InternalError))
return
}
c.JSON(200, gin.H{"msgCount": msgCount})
c.JSON(200, withSuccess(gin.H{"msgCount": msgCount}))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment