add 数据库 Web框架 初步 支持

This commit is contained in:
2024-10-28 18:59:34 +08:00
parent 221f216c3d
commit 4ceb33fbf2
14 changed files with 152 additions and 2 deletions

32
database/database.go Normal file
View File

@@ -0,0 +1,32 @@
package db
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"os"
"zsxyww.com/scheduler/config"
"zsxyww.com/scheduler/model"
)
var err error
func Connect() {
if config.DB.Type == "SQLite" {
connectSQLite()
} else {
fmt.Println("sorry,we support SQLite only so far,check **DB.Type** entry in the config file")
os.Exit(1)
}
Main.AutoMigrate(&model.Member{})
}
func connectSQLite() {
Main, err = gorm.Open(sqlite.Open(config.DB.Path), &gorm.Config{})
if err != nil {
fmt.Printf("error in connecting to SQLite:")
fmt.Println(err)
os.Exit(1)
}
}

7
database/model.go Normal file
View File

@@ -0,0 +1,7 @@
package db
import (
"gorm.io/gorm"
)
var Main *gorm.DB //Main database connection