2020-01-02 00:03:07 +01:00
|
|
|
package config
|
|
|
|
|
2020-05-05 18:26:44 +02:00
|
|
|
import (
|
|
|
|
"path/filepath"
|
2022-07-05 23:13:34 +02:00
|
|
|
"regexp"
|
2021-01-10 15:28:42 +01:00
|
|
|
"strings"
|
2020-05-05 18:26:44 +02:00
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/server/header"
|
2020-05-05 18:26:44 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
2020-01-31 15:29:06 +01:00
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// DetachServer checks if server should detach from console (daemon mode).
|
2020-01-02 00:03:07 +01:00
|
|
|
func (c *Config) DetachServer() bool {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.DetachServer
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// Proxies returns proxy server ranges from which client and protocol headers can be trusted.
|
|
|
|
func (c *Config) Proxies() []string {
|
|
|
|
return c.options.Proxy
|
|
|
|
}
|
2020-01-02 00:03:07 +01:00
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// Proxy returns the list of trusted proxy servers as comma-separated list.
|
|
|
|
func (c *Config) Proxy() string {
|
|
|
|
return strings.Join(c.options.Proxy, ", ")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// ProxyProtoHeader returns the proxy protocol header names.
|
|
|
|
func (c *Config) ProxyProtoHeader() []string {
|
|
|
|
return c.options.ProxyProtoHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProxyProtoHttps returns the proxy protocol header HTTPS values.
|
|
|
|
func (c *Config) ProxyProtoHttps() []string {
|
|
|
|
return c.options.ProxyProtoHttps
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProxyHttpsHeaders returns a map with the proxy https protocol headers.
|
|
|
|
func (c *Config) ProxyHttpsHeaders() map[string]string {
|
|
|
|
p := len(c.options.ProxyProtoHeader)
|
|
|
|
h := make(map[string]string, p+1)
|
|
|
|
|
|
|
|
if p == 0 {
|
|
|
|
h[header.ForwardedProto] = header.ProtoHttps
|
|
|
|
return h
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
for k, v := range c.options.ProxyProtoHeader {
|
|
|
|
if l := len(c.options.ProxyProtoHttps); l == 0 {
|
|
|
|
h[v] = header.ProtoHttps
|
|
|
|
} else if l > k {
|
|
|
|
h[v] = c.options.ProxyProtoHttps[k]
|
|
|
|
} else {
|
|
|
|
h[v] = c.options.ProxyProtoHttps[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
// HttpMode returns the server mode.
|
|
|
|
func (c *Config) HttpMode() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.HttpMode == "" {
|
2020-01-02 00:03:07 +01:00
|
|
|
if c.Debug() {
|
|
|
|
return "debug"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "release"
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.HttpMode
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2021-01-10 15:28:42 +01:00
|
|
|
// HttpCompression returns the http compression method (none or gzip).
|
|
|
|
func (c *Config) HttpCompression() string {
|
|
|
|
return strings.ToLower(strings.TrimSpace(c.options.HttpCompression))
|
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// HttpHost returns the built-in HTTP server host name or IP address (empty for all interfaces).
|
|
|
|
func (c *Config) HttpHost() string {
|
|
|
|
if c.options.HttpHost == "" {
|
|
|
|
return "0.0.0.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.HttpHost
|
|
|
|
}
|
|
|
|
|
|
|
|
// HttpPort returns the HTTP server port number.
|
|
|
|
func (c *Config) HttpPort() int {
|
|
|
|
if c.options.HttpPort == 0 {
|
|
|
|
return 2342
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.HttpPort
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// TemplatesPath returns the server templates path.
|
|
|
|
func (c *Config) TemplatesPath() string {
|
|
|
|
return filepath.Join(c.AssetsPath(), "templates")
|
2020-05-05 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 23:13:34 +02:00
|
|
|
// CustomTemplatesPath returns the path to custom templates.
|
|
|
|
func (c *Config) CustomTemplatesPath() string {
|
|
|
|
if p := c.CustomAssetsPath(); p != "" {
|
|
|
|
return filepath.Join(p, "templates")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// TemplateFiles returns the file paths of all templates found.
|
|
|
|
func (c *Config) TemplateFiles() []string {
|
|
|
|
results := make([]string, 0, 32)
|
|
|
|
|
|
|
|
tmplPaths := []string{c.TemplatesPath(), c.CustomTemplatesPath()}
|
|
|
|
|
|
|
|
for _, p := range tmplPaths {
|
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(p) + "/[A-Za-z0-9]*.*")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tmplName := range matches {
|
|
|
|
results = append(results, tmplName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// TemplateExists checks if a template with the given name exists (e.g. index.tmpl).
|
2020-05-31 02:09:52 +02:00
|
|
|
func (c *Config) TemplateExists(name string) bool {
|
2022-07-05 23:13:34 +02:00
|
|
|
if found := fs.FileExists(filepath.Join(c.TemplatesPath(), name)); found {
|
|
|
|
return true
|
|
|
|
} else if p := c.CustomTemplatesPath(); p != "" {
|
|
|
|
return fs.FileExists(filepath.Join(p, name))
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-05 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:11:56 +02:00
|
|
|
// TemplateName returns the name of the default template (e.g. index.tmpl).
|
|
|
|
func (c *Config) TemplateName() string {
|
2020-10-08 08:52:03 +02:00
|
|
|
if s := c.Settings(); s != nil {
|
|
|
|
if c.TemplateExists(s.Templates.Default) {
|
|
|
|
return s.Templates.Default
|
|
|
|
}
|
2020-05-05 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "index.tmpl"
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 06:59:22 +02:00
|
|
|
// StaticPath returns the static assets' path.
|
2020-05-31 02:09:52 +02:00
|
|
|
func (c *Config) StaticPath() string {
|
|
|
|
return filepath.Join(c.AssetsPath(), "static")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 06:59:22 +02:00
|
|
|
// StaticFile returns the path to a static file.
|
|
|
|
func (c *Config) StaticFile(fileName string) string {
|
|
|
|
return filepath.Join(c.AssetsPath(), "static", fileName)
|
|
|
|
}
|
|
|
|
|
2020-06-26 16:11:56 +02:00
|
|
|
// BuildPath returns the static build path.
|
|
|
|
func (c *Config) BuildPath() string {
|
2020-05-31 02:09:52 +02:00
|
|
|
return filepath.Join(c.StaticPath(), "build")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
2020-06-26 16:11:56 +02:00
|
|
|
|
2021-11-25 17:24:53 +01:00
|
|
|
// ImgPath returns the path to static image files.
|
2020-06-26 16:11:56 +02:00
|
|
|
func (c *Config) ImgPath() string {
|
|
|
|
return filepath.Join(c.StaticPath(), "img")
|
|
|
|
}
|