1、add sqlite

This commit is contained in:
redhat 2025-05-22 09:13:28 +08:00
parent dc78e4cb65
commit de5f51e9dc
11 changed files with 119 additions and 1 deletions

View File

@ -5,7 +5,7 @@ BINARY="dash"
all: gotool build all: gotool build
build: build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${BINARY} CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gcc go build -ldflags="-s -w" -v -a -o ${BINARY}
run: run:
@go run ./ @go run ./

View File

@ -11,6 +11,17 @@ log:
max_age: 180 max_age: 180
max_backups: 20 max_backups: 20
database:
# sqlite只需要指定db_name即可
type: "sqlite"
db_name: "./data/sqlite/dash.db" # 如果是sqlite需要指定完整路径和文件名称
# 其他远程数据库需要指定下面的内容
user: ""
password: ""
host: ""
port: 0
max_conns: 0
# 使用令牌桶限流 # 使用令牌桶限流
rate: rate:
fill_interval: 10 # 填充速率 每(ms)填充一个 fill_interval: 10 # 填充速率 每(ms)填充一个

24
dao/sqldb/base.go Normal file
View File

@ -0,0 +1,24 @@
package sqldb
import (
"dashboard/dao/sqldb/sqlite"
"dashboard/logger"
)
var (
log *logger.Logger
db *SqlDb
)
func SqlDbInit(_log *logger.Logger, _db *SqlDb) error {
db = _db
log = _log
_, err := db.Exec(sqlite.CreateUserSql)
if err != nil {
log.Sugar().Errorf("Crete user table error: %v", err)
return err
}
return nil
}

44
dao/sqldb/init.go Normal file
View File

@ -0,0 +1,44 @@
package sqldb
import (
"dashboard/models"
"dashboard/settings"
"fmt"
"strings"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
type SqlDb struct {
*sqlx.DB
}
const (
mysqlDsn = "%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local"
)
func NewDb(conf *settings.SqlConfig) (*SqlDb, error) {
if strings.Contains(conf.Type, "sqlite") {
db, err := sqlx.Connect("sqlite3", conf.Database)
if err != nil {
return nil, err
}
return &SqlDb{db}, nil
}
if strings.Contains(conf.Type, "mysql") || strings.Contains(conf.Type, "maridb") {
dsn := fmt.Sprintf(mysqlDsn, conf.Username, conf.Password, conf.Host, conf.Port, conf.Database)
db, err := sqlx.Connect("mysql", dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(conf.MaxConns)
return &SqlDb{db}, nil
}
return nil, models.ErrorSqlInitErr
}

View File

@ -0,0 +1,3 @@
package sqlite
const CreateUserSql = `CREATE TABLE IF NOT EXISTS db_unis_aisub (userid TEXT NOT NULL,is_sub INTEGER,CONSTRAINT db_unis_aisub_pk PRIMARY KEY (userid));`

View File

2
go.mod
View File

@ -12,8 +12,10 @@ require (
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/golang-jwt/jwt/v5 v5.2.2 github.com/golang-jwt/jwt/v5 v5.2.2
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
github.com/jmoiron/sqlx v1.4.0
github.com/juju/ratelimit v1.0.2 github.com/juju/ratelimit v1.0.2
github.com/looplab/fsm v1.0.3 github.com/looplab/fsm v1.0.3
github.com/mattn/go-sqlite3 v1.14.28
github.com/natefinch/lumberjack v2.0.0+incompatible github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/shirou/gopsutil/v4 v4.25.4 github.com/shirou/gopsutil/v4 v4.25.4
github.com/spf13/viper v1.20.1 github.com/spf13/viper v1.20.1

11
go.sum
View File

@ -1,3 +1,5 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
@ -39,6 +41,8 @@ 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/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.26.0 h1:SP05Nqhjcvz81uJaRfEV0YBSSSGMc/iMaVtFbr3Sw2k= github.com/go-playground/validator/v10 v10.26.0 h1:SP05Nqhjcvz81uJaRfEV0YBSSSGMc/iMaVtFbr3Sw2k=
github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo= github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
@ -51,6 +55,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI= github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
@ -65,12 +71,17 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/looplab/fsm v1.0.3 h1:qtxBsa2onOs0qFOtkqwf5zE0uP0+Te+wlIvXctPKpcw= github.com/looplab/fsm v1.0.3 h1:qtxBsa2onOs0qFOtkqwf5zE0uP0+Te+wlIvXctPKpcw=
github.com/looplab/fsm v1.0.3/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4= github.com/looplab/fsm v1.0.3/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

11
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"dashboard/dao/sqldb"
"dashboard/logger" "dashboard/logger"
"dashboard/routes" "dashboard/routes"
"dashboard/settings" "dashboard/settings"
@ -42,6 +43,16 @@ func main() {
log.Info("Settings and log init ok") log.Info("Settings and log init ok")
db, err := sqldb.NewDb(sets.SqlConfig)
if err != nil {
log.Sugar().Panicf("New db error: %v", err)
}
sqldb.SqlDbInit(log, db)
if err != nil {
log.Sugar().Panicf("New db error: %v", err)
}
r := routes.Setup(log, *sets.RateLimitConfig, *sets.JwtConfig) r := routes.Setup(log, *sets.RateLimitConfig, *sets.JwtConfig)
listenAndServe(fmt.Sprintf(":%d", sets.BaseConfig.Port), r, log) listenAndServe(fmt.Sprintf(":%d", sets.BaseConfig.Port), r, log)

View File

@ -17,6 +17,7 @@ const (
var ( var (
ErrorInvalidData = errors.New("no such value") ErrorInvalidData = errors.New("no such value")
ErrorPasswordErr = errors.New("user or password invalid") ErrorPasswordErr = errors.New("user or password invalid")
ErrorSqlInitErr = errors.New("database init err")
) )
const ( const (

View File

@ -6,6 +6,7 @@ type AppConfig struct {
*SnowflakeConfig `mapstructure:"snowflake"` *SnowflakeConfig `mapstructure:"snowflake"`
*RateLimitConfig `mapstructure:"rate"` *RateLimitConfig `mapstructure:"rate"`
*JwtConfig `mapstructure:"jwt"` *JwtConfig `mapstructure:"jwt"`
*SqlConfig `mapstructure:"database"`
} }
type BaseConfig struct { type BaseConfig struct {
@ -23,6 +24,16 @@ type LogConfig struct {
MaxBackUps int `mapstructure:"max_backups"` MaxBackUps int `mapstructure:"max_backups"`
} }
type SqlConfig struct {
Port int `mapstructure:"port"`
MaxConns int `mapstructure:"max_conns"`
Host string `mapstructure:"host"`
Username string `mapstructure:"user"`
Password string `mapstructure:"password"`
Database string `mapstructure:"db_name"`
Type string `mapstructure:"type"`
}
type SnowflakeConfig struct { type SnowflakeConfig struct {
StartTime string `mapstructure:"start_time"` StartTime string `mapstructure:"start_time"`
MachineId int64 `mapstructure:"machine_id"` MachineId int64 `mapstructure:"machine_id"`