12345678910111213141516171819202122232425 |
- package initialize
- import (
- "dhcp/global"
- "dhcp/model"
- "github.com/glebarez/sqlite"
- "gorm.io/gorm"
- "os"
- "path/filepath"
- )
- func InitDB() {
- executablePath, err := os.Executable()
- if err != nil {
- panic(err)
- }
- dbPath := filepath.Join(filepath.Dir(executablePath), "dhcp.db")
- db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
- if err != nil {
- panic(err)
- }
- db.AutoMigrate(&model.DHCP{})
- global.DB = db
- }
|