initDB.go 427 B

12345678910111213141516171819202122232425
  1. package initialize
  2. import (
  3. "dhcp/global"
  4. "dhcp/model"
  5. "github.com/glebarez/sqlite"
  6. "gorm.io/gorm"
  7. "os"
  8. "path/filepath"
  9. )
  10. func InitDB() {
  11. executablePath, err := os.Executable()
  12. if err != nil {
  13. panic(err)
  14. }
  15. dbPath := filepath.Join(filepath.Dir(executablePath), "dhcp.db")
  16. db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
  17. if err != nil {
  18. panic(err)
  19. }
  20. db.AutoMigrate(&model.DHCP{})
  21. global.DB = db
  22. }