mirror of
https://github.com/writefreely/writefreely
synced 2024-11-24 17:43:05 +00:00
Extract out common DB connection logic
T529
This commit is contained in:
parent
c6d3ef7596
commit
61de04338e
1 changed files with 10 additions and 14 deletions
24
app.go
24
app.go
|
@ -438,26 +438,22 @@ func Serve() {
|
|||
func connectToDatabase(app *app) {
|
||||
log.Info("Connecting to %s database...", app.cfg.Database.Type)
|
||||
|
||||
var db *sql.DB
|
||||
var err error
|
||||
if app.cfg.Database.Type == "mysql" {
|
||||
db, err := sql.Open(app.cfg.Database.Type, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=%s", app.cfg.Database.User, app.cfg.Database.Password, app.cfg.Database.Host, app.cfg.Database.Port, app.cfg.Database.Database, url.QueryEscape(time.Local.String())))
|
||||
if err != nil {
|
||||
log.Error("%s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
app.db = &datastore{db, "mysql"}
|
||||
app.db.SetMaxOpenConns(50)
|
||||
db, err = sql.Open(app.cfg.Database.Type, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=%s", app.cfg.Database.User, app.cfg.Database.Password, app.cfg.Database.Host, app.cfg.Database.Port, app.cfg.Database.Database, url.QueryEscape(time.Local.String())))
|
||||
} else if app.cfg.Database.Type == "sqlite3" {
|
||||
db, err := sql.Open("sqlite3", "./writefreely.db?parseTime=true")
|
||||
if err != nil {
|
||||
log.Error("%s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
app.db = &datastore{db, "sqlite3"}
|
||||
app.db.SetMaxOpenConns(50)
|
||||
db, err = sql.Open("sqlite3", "./writefreely.db?parseTime=true")
|
||||
} else {
|
||||
log.Error("Invalid database type '%s'. Only 'mysql' and 'sqlite3' are supported right now.", app.cfg.Database.Type)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("%s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
app.db = &datastore{db, app.cfg.Database.Type}
|
||||
app.db.SetMaxOpenConns(50)
|
||||
}
|
||||
|
||||
func shutdown(app *app) {
|
||||
|
|
Loading…
Reference in a new issue