mirror of
https://github.com/go-shiori/shiori.git
synced 2025-02-23 15:35:21 +08:00
For development mode, create database in woking dir
This commit is contained in:
parent
997781f96e
commit
ec53fe6113
2 changed files with 43 additions and 33 deletions
35
main.go
35
main.go
|
@ -3,23 +3,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
fp "path/filepath"
|
|
||||||
|
|
||||||
"github.com/RadhiFadlillah/shiori/cmd"
|
"github.com/RadhiFadlillah/shiori/cmd"
|
||||||
dt "github.com/RadhiFadlillah/shiori/database"
|
dt "github.com/RadhiFadlillah/shiori/database"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
apppaths "github.com/muesli/go-app-paths"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var dbPath = "shiori.db"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create database path
|
|
||||||
dbPath := createDatabasePath()
|
|
||||||
|
|
||||||
// Make sure directory exist
|
|
||||||
os.MkdirAll(fp.Dir(dbPath), os.ModePerm)
|
|
||||||
|
|
||||||
// Open database
|
// Open database
|
||||||
sqliteDB, err := dt.OpenSQLiteDatabase(dbPath)
|
sqliteDB, err := dt.OpenSQLiteDatabase(dbPath)
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
@ -31,29 +23,6 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createDatabasePath() string {
|
|
||||||
// Try to look at environment variables
|
|
||||||
dbPath, found := os.LookupEnv("ENV_SHIORI_DB")
|
|
||||||
if found {
|
|
||||||
// If ENV_SHIORI_DB is directory, append "shiori.db" as filename
|
|
||||||
if f1, err := os.Stat(dbPath); err == nil && f1.IsDir() {
|
|
||||||
dbPath = fp.Join(dbPath, "shiori.db")
|
|
||||||
}
|
|
||||||
|
|
||||||
return dbPath
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to use platform specific app path
|
|
||||||
userScope := apppaths.NewScope(apppaths.User, "shiori", "shiori")
|
|
||||||
dataDir, err := userScope.DataDir()
|
|
||||||
if err == nil {
|
|
||||||
return fp.Join(dataDir, "shiori.db")
|
|
||||||
}
|
|
||||||
|
|
||||||
// When all fail, create database in working directory
|
|
||||||
return "shiori.db"
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkError(err error) {
|
func checkError(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
41
path-generator.go
Normal file
41
path-generator.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// +build !dev
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
fp "path/filepath"
|
||||||
|
|
||||||
|
apppaths "github.com/muesli/go-app-paths"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Set database path
|
||||||
|
dbPath = createDatabasePath()
|
||||||
|
|
||||||
|
// Make sure directory exist
|
||||||
|
os.MkdirAll(fp.Dir(dbPath), os.ModePerm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDatabasePath() string {
|
||||||
|
// Try to look at environment variables
|
||||||
|
dbPath, found := os.LookupEnv("ENV_SHIORI_DB")
|
||||||
|
if found {
|
||||||
|
// If ENV_SHIORI_DB is directory, append "shiori.db" as filename
|
||||||
|
if f1, err := os.Stat(dbPath); err == nil && f1.IsDir() {
|
||||||
|
dbPath = fp.Join(dbPath, "shiori.db")
|
||||||
|
}
|
||||||
|
|
||||||
|
return dbPath
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to use platform specific app path
|
||||||
|
userScope := apppaths.NewScope(apppaths.User, "shiori", "shiori")
|
||||||
|
dataDir, err := userScope.DataDir()
|
||||||
|
if err == nil {
|
||||||
|
return fp.Join(dataDir, "shiori.db")
|
||||||
|
}
|
||||||
|
|
||||||
|
// When all fail, create database in working directory
|
||||||
|
return "shiori.db"
|
||||||
|
}
|
Loading…
Reference in a new issue