package model import ( "database/sql" "embed" "log" "path/filepath" _ "github.com/mattn/go-sqlite3" ) var db *sql.DB //go:embed sql/*.sql var sqlCode embed.FS func GetSql(code string) (string, error) { query, err := sqlCode.ReadFile("sql/" + code + ".sql") if err != nil { return "", err } return string(query), nil } func InitDatabase() { var err error fn := filepath.Join(storage, "ceres.sqlite3") db, err = sql.Open("sqlite3", fn) if err != nil { log.Fatal(err) } err = db.Ping() if err != nil { log.Fatal(err) } for _, v := range []string{`migrate`, `testdata`} { query, err := GetSql(v) if err != nil { log.Fatal(err) } _, err = db.Exec(query) if err != nil { log.Fatal(err) } } } func CloseDatabase() { var err error = db.Close() if err != nil { log.Printf("Failed to close database: %v\n", err) } else { log.Println("Closed database") } }