generated from VLADIMIR/template
37 lines
729 B
Go
37 lines
729 B
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func GetStoryFilepath() string {
|
|
return getFilepath("STORY_FILENAME", "data/story/story.json")
|
|
}
|
|
|
|
func GetDBFilepath() string {
|
|
return getFilepath("DB_FILENAME", "data/db/store.db")
|
|
}
|
|
|
|
func getFilepath(env string, defaultFilepath string) string {
|
|
filepath := selectFilepath(env, defaultFilepath)
|
|
ensureDirExists(filepath)
|
|
return filepath
|
|
}
|
|
|
|
func selectFilepath(env string, defaultFilepath string) string {
|
|
filepath := os.Getenv(env)
|
|
if filepath != "" {
|
|
return filepath
|
|
}
|
|
return defaultFilepath
|
|
}
|
|
|
|
func ensureDirExists(filePath string) error {
|
|
dir := filepath.Dir(filePath)
|
|
if dir == "" || dir == "." || dir == "/" {
|
|
return nil
|
|
}
|
|
return os.MkdirAll(dir, 0755)
|
|
}
|