mirror of
https://github.com/go-shiori/shiori.git
synced 2024-12-28 19:30:42 +08:00
Tidy up code for opening database
This commit is contained in:
parent
c2eee1d59a
commit
9f145e27ef
1 changed files with 31 additions and 20 deletions
|
@ -101,28 +101,39 @@ func getDataDir(portableMode bool) (string, error) {
|
|||
}
|
||||
|
||||
func openDatabase() (database.DB, error) {
|
||||
// Check if it uses MySQL
|
||||
if dbms, _ := os.LookupEnv("SHIORI_DBMS"); dbms == "mysql" {
|
||||
user, _ := os.LookupEnv("SHIORI_MYSQL_USER")
|
||||
password, _ := os.LookupEnv("SHIORI_MYSQL_PASS")
|
||||
dbName, _ := os.LookupEnv("SHIORI_MYSQL_NAME")
|
||||
dbAddress, _ := os.LookupEnv("SHIORI_MYSQL_ADDRESS")
|
||||
|
||||
connString := fmt.Sprintf("%s:%s@%s/%s", user, password, dbAddress, dbName)
|
||||
return database.OpenMySQLDatabase(connString)
|
||||
switch dbms, _ := os.LookupEnv("SHIORI_DBMS"); dbms {
|
||||
case "mysql":
|
||||
return openMySQLDatabase()
|
||||
case "postgresql":
|
||||
return openPostgreSQLDatabase()
|
||||
default:
|
||||
return openSQLiteDatabase()
|
||||
}
|
||||
// Check if it uses PostgreSQL
|
||||
if dbms, _ := os.LookupEnv("SHIORI_DBMS"); dbms == "postgresql" {
|
||||
host, _ := os.LookupEnv("SHIORI_PG_HOST")
|
||||
port, _ := os.LookupEnv("SHIORI_PG_PORT")
|
||||
user, _ := os.LookupEnv("SHIORI_PG_USER")
|
||||
password, _ := os.LookupEnv("SHIORI_PG_PASS")
|
||||
dbName, _ := os.LookupEnv("SHIORI_PG_NAME")
|
||||
}
|
||||
|
||||
connString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbName)
|
||||
return database.OpenPGDatabase(connString)
|
||||
}
|
||||
// If not, just uses SQLite
|
||||
func openSQLiteDatabase() (database.DB, error) {
|
||||
dbPath := fp.Join(dataDir, "shiori.db")
|
||||
return database.OpenSQLiteDatabase(dbPath)
|
||||
}
|
||||
|
||||
func openMySQLDatabase() (database.DB, error) {
|
||||
user, _ := os.LookupEnv("SHIORI_MYSQL_USER")
|
||||
password, _ := os.LookupEnv("SHIORI_MYSQL_PASS")
|
||||
dbName, _ := os.LookupEnv("SHIORI_MYSQL_NAME")
|
||||
dbAddress, _ := os.LookupEnv("SHIORI_MYSQL_ADDRESS")
|
||||
|
||||
connString := fmt.Sprintf("%s:%s@%s/%s", user, password, dbAddress, dbName)
|
||||
return database.OpenMySQLDatabase(connString)
|
||||
}
|
||||
|
||||
func openPostgreSQLDatabase() (database.DB, error) {
|
||||
host, _ := os.LookupEnv("SHIORI_PG_HOST")
|
||||
port, _ := os.LookupEnv("SHIORI_PG_PORT")
|
||||
user, _ := os.LookupEnv("SHIORI_PG_USER")
|
||||
password, _ := os.LookupEnv("SHIORI_PG_PASS")
|
||||
dbName, _ := os.LookupEnv("SHIORI_PG_NAME")
|
||||
|
||||
connString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||||
host, port, user, password, dbName)
|
||||
return database.OpenPGDatabase(connString)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue