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-10-13 23:44:51 +02:00
|
|
|
const (
|
|
|
|
HttpModeProd = "release"
|
|
|
|
HttpModeDebug = "debug"
|
|
|
|
)
|
|
|
|
|
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-12 14:57:33 +02:00
|
|
|
// TrustedProxy returns the list of trusted proxy servers as comma-separated list.
|
|
|
|
func (c *Config) TrustedProxy() string {
|
|
|
|
return strings.Join(c.options.TrustedProxies, ", ")
|
2022-10-11 22:44:11 +02:00
|
|
|
}
|
2020-01-02 00:03:07 +01:00
|
|
|
|
2022-10-12 14:57:33 +02:00
|
|
|
// TrustedProxies returns proxy server ranges from which client and protocol headers can be trusted.
|
|
|
|
func (c *Config) TrustedProxies() []string {
|
|
|
|
return c.options.TrustedProxies
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-12 14:57:33 +02:00
|
|
|
// HttpsProxyHeader returns the proxy protocol header names.
|
|
|
|
func (c *Config) HttpsProxyHeader() []string {
|
|
|
|
return c.options.HttpsProxyHeaders
|
2022-10-11 22:44:11 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 14:57:33 +02:00
|
|
|
// HttpsProxyProto returns the proxy protocol header HTTPS values.
|
|
|
|
func (c *Config) HttpsProxyProto() []string {
|
|
|
|
return c.options.HttpsProxyProto
|
2022-10-11 22:44:11 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 14:57:33 +02:00
|
|
|
// HttpsProxyHeaders returns a map with the proxy https protocol headers.
|
|
|
|
func (c *Config) HttpsProxyHeaders() map[string]string {
|
|
|
|
p := len(c.options.HttpsProxyHeaders)
|
2022-10-11 22:44:11 +02:00
|
|
|
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-12 14:57:33 +02:00
|
|
|
for k, v := range c.options.HttpsProxyHeaders {
|
|
|
|
if l := len(c.options.HttpsProxyProto); l == 0 {
|
2022-10-11 22:44:11 +02:00
|
|
|
h[v] = header.ProtoHttps
|
|
|
|
} else if l > k {
|
2022-10-12 14:57:33 +02:00
|
|
|
h[v] = c.options.HttpsProxyProto[k]
|
2022-10-11 22:44:11 +02:00
|
|
|
} else {
|
2022-10-12 14:57:33 +02:00
|
|
|
h[v] = c.options.HttpsProxyProto[0]
|
2022-10-11 22:44:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-10-13 23:44:51 +02:00
|
|
|
if c.Prod() {
|
|
|
|
return HttpModeProd
|
|
|
|
} else if c.options.HttpMode == "" {
|
2020-01-02 00:03:07 +01:00
|
|
|
if c.Debug() {
|
2022-10-13 23:44:51 +02:00
|
|
|
return HttpModeDebug
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-13 23:44:51 +02:00
|
|
|
return HttpModeProd
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
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-10-18 14:21:23 +02:00
|
|
|
// TemplateExists checks if a template with the given name exists (e.g. index.gohtml).
|
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
|
|
|
}
|
|
|
|
|
2022-10-18 14:21:23 +02:00
|
|
|
// TemplateName returns the name of the default template (e.g. index.gohtml).
|
2020-06-26 16:11:56 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-18 14:21:23 +02:00
|
|
|
return "index.gohtml"
|
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")
|
|
|
|
}
|