2018-11-17 06:21:39 +01:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
|
|
|
"github.com/kylelemons/go-gypsy/yaml"
|
|
|
|
"github.com/photoprism/photoprism/internal/fsutil"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2018-12-18 18:38:30 +01:00
|
|
|
const (
|
2018-12-21 08:44:13 +01:00
|
|
|
DbTiDB = "internal"
|
2018-12-18 18:38:30 +01:00
|
|
|
DbMySQL = "mysql"
|
|
|
|
)
|
|
|
|
|
2018-11-17 06:21:39 +01:00
|
|
|
// Config provides a struct in which application configuration is stored.
|
|
|
|
// Application code must use functions to get config values, for two reasons:
|
|
|
|
//
|
|
|
|
// 1. Some values are computed and we don't want to leak implementation details (aims at reducing refactoring overhead).
|
|
|
|
//
|
|
|
|
// 2. Paths might actually be dynamic later (if we build a multi-user version).
|
|
|
|
//
|
|
|
|
// See https://github.com/photoprism/photoprism/issues/50#issuecomment-433856358
|
|
|
|
type Config struct {
|
2019-05-03 18:57:28 +02:00
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Copyright string
|
|
|
|
Debug bool
|
|
|
|
LogLevel string
|
|
|
|
ConfigFile string
|
|
|
|
AssetsPath string
|
|
|
|
CachePath string
|
|
|
|
OriginalsPath string
|
|
|
|
ImportPath string
|
|
|
|
ExportPath string
|
|
|
|
SqlServerHost string
|
|
|
|
SqlServerPort uint
|
|
|
|
SqlServerPath string
|
|
|
|
SqlServerPassword string
|
|
|
|
HttpServerHost string
|
|
|
|
HttpServerPort int
|
|
|
|
HttpServerMode string
|
|
|
|
HttpServerPassword string
|
|
|
|
DarktableCli string
|
|
|
|
DatabaseDriver string
|
|
|
|
DatabaseDsn string
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetValuesFromFile uses a yaml config file to initiate the configuration entity.
|
|
|
|
func (c *Config) SetValuesFromFile(fileName string) error {
|
|
|
|
yamlConfig, err := yaml.ReadFile(fileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
c.ConfigFile = fileName
|
2018-11-17 06:21:39 +01:00
|
|
|
if debug, err := yamlConfig.GetBool("debug"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.Debug = debug
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 14:10:05 +02:00
|
|
|
if logLevel, err := yamlConfig.Get("log-level"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.LogLevel = logLevel
|
2019-05-02 14:10:05 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 03:02:34 +01:00
|
|
|
if sqlServerHost, err := yamlConfig.Get("sql-host"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.SqlServerHost = sqlServerHost
|
2018-12-18 18:38:30 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 03:02:34 +01:00
|
|
|
if sqlServerPort, err := yamlConfig.GetInt("sql-port"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.SqlServerPort = uint(sqlServerPort)
|
2018-12-18 18:38:30 +01:00
|
|
|
}
|
|
|
|
|
2018-12-26 11:40:01 +01:00
|
|
|
if sqlServerPassword, err := yamlConfig.Get("sql-password"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.SqlServerPassword = sqlServerPassword
|
2018-12-26 11:40:01 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 04:05:14 +01:00
|
|
|
if sqlServerPath, err := yamlConfig.Get("sql-path"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.SqlServerPath = sqlServerPath
|
2018-12-18 18:38:30 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 03:11:06 +01:00
|
|
|
if httpServerHost, err := yamlConfig.Get("http-host"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.HttpServerHost = httpServerHost
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 03:11:06 +01:00
|
|
|
if httpServerPort, err := yamlConfig.GetInt("http-port"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.HttpServerPort = int(httpServerPort)
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-26 11:40:01 +01:00
|
|
|
if httpServerMode, err := yamlConfig.Get("http-mode"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.HttpServerMode = httpServerMode
|
2018-12-26 11:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if httpServerPassword, err := yamlConfig.Get("http-password"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.HttpServerPassword = httpServerPassword
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if assetsPath, err := yamlConfig.Get("assets-path"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.AssetsPath = fsutil.ExpandedFilename(assetsPath)
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if cachePath, err := yamlConfig.Get("cache-path"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.CachePath = fsutil.ExpandedFilename(cachePath)
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if originalsPath, err := yamlConfig.Get("originals-path"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.OriginalsPath = fsutil.ExpandedFilename(originalsPath)
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if importPath, err := yamlConfig.Get("import-path"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.ImportPath = fsutil.ExpandedFilename(importPath)
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if exportPath, err := yamlConfig.Get("export-path"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.ExportPath = fsutil.ExpandedFilename(exportPath)
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if darktableCli, err := yamlConfig.Get("darktable-cli"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.DarktableCli = fsutil.ExpandedFilename(darktableCli)
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if databaseDriver, err := yamlConfig.Get("database-driver"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.DatabaseDriver = databaseDriver
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if databaseDsn, err := yamlConfig.Get("database-dsn"); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.DatabaseDsn = databaseDsn
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValuesFromCliContext uses values from the CLI to setup configuration overrides
|
|
|
|
// for the entity.
|
|
|
|
func (c *Config) SetValuesFromCliContext(ctx *cli.Context) error {
|
|
|
|
if ctx.GlobalBool("debug") {
|
2019-05-03 18:57:28 +02:00
|
|
|
c.Debug = ctx.GlobalBool("debug")
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("log-level") || c.LogLevel == "" {
|
|
|
|
c.LogLevel = ctx.GlobalString("log-level")
|
2019-05-02 14:10:05 +02:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("assets-path") || c.AssetsPath == "" {
|
|
|
|
c.AssetsPath = fsutil.ExpandedFilename(ctx.GlobalString("assets-path"))
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("cache-path") || c.CachePath == "" {
|
|
|
|
c.CachePath = fsutil.ExpandedFilename(ctx.GlobalString("cache-path"))
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("originals-path") || c.OriginalsPath == "" {
|
|
|
|
c.OriginalsPath = fsutil.ExpandedFilename(ctx.GlobalString("originals-path"))
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("import-path") || c.ImportPath == "" {
|
|
|
|
c.ImportPath = fsutil.ExpandedFilename(ctx.GlobalString("import-path"))
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("export-path") || c.ExportPath == "" {
|
|
|
|
c.ExportPath = fsutil.ExpandedFilename(ctx.GlobalString("export-path"))
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("darktable-cli") || c.DarktableCli == "" {
|
|
|
|
c.DarktableCli = fsutil.ExpandedFilename(ctx.GlobalString("darktable-cli"))
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("database-driver") || c.DatabaseDriver == "" {
|
|
|
|
c.DatabaseDriver = ctx.GlobalString("database-driver")
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("database-dsn") || c.DatabaseDsn == "" {
|
|
|
|
c.DatabaseDsn = ctx.GlobalString("database-dsn")
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("sql-host") || c.SqlServerHost == "" {
|
|
|
|
c.SqlServerHost = ctx.GlobalString("sql-host")
|
2018-12-18 18:38:30 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("sql-port") || c.SqlServerPort == 0 {
|
|
|
|
c.SqlServerPort = ctx.GlobalUint("sql-port")
|
2018-12-18 18:38:30 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("sql-password") || c.SqlServerPassword == "" {
|
|
|
|
c.SqlServerPassword = ctx.GlobalString("sql-password")
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("sql-path") || c.SqlServerPath == "" {
|
|
|
|
c.SqlServerPath = ctx.GlobalString("sql-path")
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("http-host") || c.HttpServerHost == "" {
|
|
|
|
c.HttpServerHost = ctx.GlobalString("http-host")
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("http-port") || c.HttpServerPort == 0 {
|
|
|
|
c.HttpServerPort = ctx.GlobalInt("http-port")
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
if ctx.GlobalIsSet("http-mode") || c.HttpServerMode == "" {
|
|
|
|
c.HttpServerMode = ctx.GlobalString("http-mode")
|
2018-11-17 06:21:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|