diff --git a/dbop/mysql.go b/dbop/mysql.go new file mode 100644 index 0000000..0b69762 --- /dev/null +++ b/dbop/mysql.go @@ -0,0 +1,118 @@ +package dbop + +import ( + "database/sql" + "fmt" + "time" + + "github.com/shockliu/logger" + "github.com/go-ini/ini" + _ "github.com/go-sql-driver/mysql" +) + +const ( + imgurl = "https://center.daymele.com/res/images" + mp4url = "https://center.daymele.com/res/mp4" + apkurl = "https://center.daymele.com/res/apk" +) + +var ( + DB *sql.DB + err error // 错误信息 +) + +// 初始化链接 +type Database struct { + Host string + Port string + Username string + Password string + Dbname string +} + +type Redis struct { + Host string + Port string + Password string +} + + // 初始化链接 +func DBinit(conf string) { + Cfg, err := ini.Load(conf) + if err != nil { + panic("配置文件读取失败") + } + + var my = &Database{} + err = Cfg.Section("mysql").MapTo(my) + if err != nil { + panic("未配置mysql数据库") + } + + // 豆曲咖数据库 + dbDSN := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s", my.Username, my.Password, my.Host, my.Port, my.Dbname, "utf8mb4") + // 打开连接失败 + DB, err = sql.Open("mysql", dbDSN) + //defer MysqlDb.Close(); + if err != nil { + logger.Error("dbDSN: " + dbDSN) + panic("数据源配置不正确: " + err.Error()) + } + + // 最大连接数 + DB.SetMaxOpenConns(10) + // 闲置连接数 + DB.SetMaxIdleConns(5) + // 最大连接周期 + DB.SetConnMaxLifetime(100 * time.Second) + if err = DB.Ping(); nil != err { + panic("数据库链接失败: " + err.Error()) + } + + var rd = &Redis{} + err = Cfg.Section("redis").MapTo(rd) + if err != nil { + panic("未配置redis数据库") + } + RedisInit(rd.Host, rd.Port, rd.Password) +} + +func InsertRecord(file, original, ftype, md5 string, size int64) (ret string) { + // time.Now().Format("2006-01/") + // https://www.52shiji.com/res/images/ + if ftype == "movie/mp4" { + ret = fmt.Sprintf("%s/%s/%s", mp4url, time.Now().Format("2006-01"), file) + } else if ftype == "apk" { + ret = fmt.Sprintf("%s/%s", apkurl, file) + } else { + ret = fmt.Sprintf("%s/%s/%s", imgurl, time.Now().Format("2006-01"), file) + } + _, err := DB.Exec("INSERT INTO dqk_storage (`key`, name, type,md5,size,url,add_time) VALUES (?, ?,?,?,?,?,now());", file, original, ftype, md5, size, ret) + if err != nil { + logger.Errorf("文件[%s]入库失败:%s\n", file, err) + } else { + logger.Infof("文件入库[%s]成功。\n", file) + } + return +} + +func IsExist(name string) bool { + var cnt int + s := fmt.Sprintf("SELECT count(1) FROM dqk_storage where `key` like '%s%%';", name) + //logger.Debugf("SQL:[%s]\n",s) + err := DB.QueryRow(s).Scan(&cnt) + if err != nil { + logger.Errorf("获取资源[%s]信息失败:%s\n", name, err) + return true + } + if cnt > 0 { + return true + } else { + return false + } +} + +func GetFilebyMd5(filename, md5 string, filesize int64) (name, url string, err error) { + err = DB.QueryRow("SELECT name,url FROM dqk_storage where md5=? and size=?;", md5, filesize).Scan(&name, &url) + return +} diff --git a/dbop/redisop.go b/dbop/redisop.go new file mode 100644 index 0000000..a632c61 --- /dev/null +++ b/dbop/redisop.go @@ -0,0 +1,39 @@ +package dbop + +import ( + "fmt" + "time" + + "github.com/go-redis/redis" +) + +const ( + RD_TRADE_HEAD = "MTPAY:" +) + +var ( + RDb *redis.Client +) + +// 初始化连接 +func RedisInit(host, port, passwd string) { + RDb = redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%s", host, port), + Password: passwd, // no password set + DB: 0, // use default DB + PoolSize: 20, // 连接池大小 + }) + _, err := RDb.Ping().Result() + if err != nil { + panic("Redis初始化失败") + } +} + +func CheckRedisTrade(tradeNo string) bool { + _, err := RDb.Get(RD_TRADE_HEAD + tradeNo).Result() + if err == redis.Nil { + RDb.Set(RD_TRADE_HEAD+tradeNo, "true", 30*time.Minute) + return false + } + return true +} diff --git a/dqres.go b/dqres.go index 519b27d..2125831 100644 --- a/dqres.go +++ b/dqres.go @@ -9,7 +9,7 @@ import ( "os/signal" "path/filepath" - // "res/dbop" + "res/dbop" "time" "github.com/gin-gonic/gin" @@ -64,10 +64,10 @@ func main() { fmt.Printf(" **************************************************\n") fmt.Printf("\n\n") port := flag.Int("port", 9000, "端口") - // conf := flag.String("conf", "/conf/daymele.conf", "配置文件") + conf := flag.String("conf", "/conf/daymele.conf", "配置文件") flag.Parse() logger.Debugf("端口参数%d\n", *port) - // dbop.DBinit(*conf) + dbop.DBinit(*conf) gin.SetMode(gin.ReleaseMode) r := gin.Default() gImg := r.Group("images") diff --git a/go.mod b/go.mod index a10b32c..4275b45 100644 --- a/go.mod +++ b/go.mod @@ -13,9 +13,12 @@ require ( github.com/fatih/color v1.16.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-ini/ini v1.67.0 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.14.0 // indirect + github.com/go-redis/redis v6.15.9+incompatible + github.com/go-sql-driver/mysql v1.7.1 github.com/goccy/go-json v0.10.2 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect diff --git a/go.sum b/go.sum index cd9e6a4..e000f02 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= @@ -23,6 +25,10 @@ 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.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=