mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 19:29:15 +00:00
Refactor handling of environment variables
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
)
|
||||
@@ -12,7 +11,7 @@ import (
|
||||
func ApiListenUrl() *url.URL {
|
||||
const defaultPort = "4001"
|
||||
|
||||
shouldServeUI := os.Getenv("PHOTOVIEW_SERVE_UI") == "1"
|
||||
shouldServeUI := ShouldServeUI()
|
||||
|
||||
apiPrefix := "/"
|
||||
if shouldServeUI {
|
||||
@@ -21,19 +20,19 @@ func ApiListenUrl() *url.URL {
|
||||
|
||||
var listenAddr string
|
||||
|
||||
listenAddr = os.Getenv("PHOTOVIEW_LISTEN_IP")
|
||||
listenAddr = EnvListenIP.GetValue()
|
||||
if listenAddr == "" {
|
||||
listenAddr = "127.0.0.1"
|
||||
}
|
||||
|
||||
listenPortStr := os.Getenv("PHOTOVIEW_LISTEN_PORT")
|
||||
listenPortStr := EnvListenPort.GetValue()
|
||||
if listenPortStr == "" {
|
||||
listenPortStr = defaultPort
|
||||
}
|
||||
|
||||
listenPort, err := strconv.Atoi(listenPortStr)
|
||||
if err != nil {
|
||||
log.Fatalf("PHOTOVIEW_LISTEN_PORT must be a number: '%s'\n%s", listenPortStr, err)
|
||||
log.Fatalf("%s must be a number: '%s'\n%s", EnvListenPort.GetName(), listenPortStr, err)
|
||||
}
|
||||
|
||||
apiUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", listenAddr, listenPort))
|
||||
@@ -46,16 +45,16 @@ func ApiListenUrl() *url.URL {
|
||||
}
|
||||
|
||||
func ApiEndpointUrl() *url.URL {
|
||||
apiEndpointStr := os.Getenv("PHOTOVIEW_API_ENDPOINT")
|
||||
apiEndpointStr := EnvAPIEndpoint.GetValue()
|
||||
|
||||
shouldServeUI := os.Getenv("PHOTOVIEW_SERVE_UI") == "1"
|
||||
shouldServeUI := ShouldServeUI()
|
||||
if shouldServeUI {
|
||||
apiEndpointStr = os.Getenv("PHOTOVIEW_PUBLIC_ENDPOINT")
|
||||
apiEndpointStr = EnvPublicEndpoint.GetValue()
|
||||
}
|
||||
|
||||
apiEndpointUrl, err := url.Parse(apiEndpointStr)
|
||||
if err != nil {
|
||||
log.Fatalf("ERROR: Environment variable PHOTOVIEW_API_ENDPOINT is not a proper url")
|
||||
log.Fatalf("ERROR: Environment variable %s is not a proper url (%s)", EnvAPIEndpoint.GetName(), EnvAPIEndpoint.GetValue())
|
||||
}
|
||||
|
||||
if shouldServeUI {
|
||||
@@ -66,16 +65,16 @@ func ApiEndpointUrl() *url.URL {
|
||||
}
|
||||
|
||||
func UiEndpointUrl() *url.URL {
|
||||
uiEndpointStr := os.Getenv("PHOTOVIEW_UI_ENDPOINT")
|
||||
uiEndpointStr := EnvUIEndpoint.GetValue()
|
||||
|
||||
shouldServeUI := os.Getenv("PHOTOVIEW_SERVE_UI") == "1"
|
||||
shouldServeUI := ShouldServeUI()
|
||||
if shouldServeUI {
|
||||
uiEndpointStr = os.Getenv("PHOTOVIEW_PUBLIC_ENDPOINT")
|
||||
uiEndpointStr = EnvPublicEndpoint.GetValue()
|
||||
}
|
||||
|
||||
uiEndpointUrl, err := url.Parse(uiEndpointStr)
|
||||
if err != nil {
|
||||
log.Fatalf("ERROR: Environment variable PHOTOVIEW_UI_ENDPOINT is not a proper url")
|
||||
log.Fatalf("ERROR: Environment variable %s is not a proper url (%s)", EnvUIEndpoint.GetName(), EnvUIEndpoint.GetValue())
|
||||
}
|
||||
|
||||
return uiEndpointUrl
|
||||
|
||||
50
api/utils/environment_variables.go
Normal file
50
api/utils/environment_variables.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package utils
|
||||
|
||||
import "os"
|
||||
|
||||
// EnvironmentVariable represents the name of an environment variable used to configure Photoview
|
||||
type EnvironmentVariable string
|
||||
|
||||
// General options
|
||||
const (
|
||||
EnvDevelopmentMode EnvironmentVariable = "PHOTOVIEW_DEVELOPMENT_MODE"
|
||||
EnvServeUI EnvironmentVariable = "PHOTOVIEW_SERVE_UI"
|
||||
EnvMediaCachePath EnvironmentVariable = "PHOTOVIEW_MEDIA_CACHE"
|
||||
)
|
||||
|
||||
// Network related
|
||||
const (
|
||||
EnvListenIP EnvironmentVariable = "PHOTOVIEW_LISTEN_IP"
|
||||
EnvListenPort EnvironmentVariable = "PHOTOVIEW_LISTEN_PORT"
|
||||
EnvAPIEndpoint EnvironmentVariable = "PHOTOVIEW_API_ENDPOINT"
|
||||
EnvUIEndpoint EnvironmentVariable = "PHOTOVIEW_UI_ENDPOINT"
|
||||
EnvPublicEndpoint EnvironmentVariable = "PHOTOVIEW_PUBLIC_ENDPOINT"
|
||||
)
|
||||
|
||||
// Database related
|
||||
const (
|
||||
EnvDatabaseDriver EnvironmentVariable = "PHOTOVIEW_DATABASE_DRIVER"
|
||||
EnvMysqlURL EnvironmentVariable = "PHOTOVIEW_MYSQL_URL"
|
||||
EnvSqlitePath EnvironmentVariable = "PHOTOVIEW_SQLITE_PATH"
|
||||
)
|
||||
|
||||
// GetName returns the name of the environment variable itself
|
||||
func (v EnvironmentVariable) GetName() string {
|
||||
return string(v)
|
||||
}
|
||||
|
||||
// GetValue returns the value of the environment
|
||||
func (v EnvironmentVariable) GetValue() string {
|
||||
return os.Getenv(string(v))
|
||||
}
|
||||
|
||||
// ShouldServeUI whether or not the "serve ui" option is enabled
|
||||
func ShouldServeUI() bool {
|
||||
return EnvServeUI.GetValue() == "1"
|
||||
}
|
||||
|
||||
// DevelopmentMode describes whether or not the server is running in development mode,
|
||||
// and should thus print debug informations and enable other features related to developing.
|
||||
func DevelopmentMode() bool {
|
||||
return EnvDevelopmentMode.GetValue() == "1"
|
||||
}
|
||||
Reference in New Issue
Block a user