Skip to content

🎧 Your Personal Streaming Service

Removed (almost) all remaining init()

Deluan23e38ec82f9c
diff --git a/api/responses/errors.go b/api/responses/errors.go
index 5e61fd530..343d49370 100644
--- a/api/responses/errors.go
+++ b/api/responses/errors.go
@@ -11,20 +11,15 @@ const (
ErrorDataNotFound
)
-var (
- errors map[int]string
-)
-
-func init() {
- errors = make(map[int]string)
- errors[ErrorGeneric] = "A generic error"
- errors[ErrorMissingParameter] = "Required parameter is missing"
- errors[ErrorClientTooOld] = "Incompatible Subsonic REST protocol version. Client must upgrade"
- errors[ErrorServerTooOld] = "Incompatible Subsonic REST protocol version. Server must upgrade"
- errors[ErrorAuthenticationFail] = "Wrong username or password"
- errors[ErrorAuthorizationFail] = "User is not authorized for the given operation"
- errors[ErrorTrialExpired] = "The trial period for the Subsonic server is over. Please upgrade to Subsonic Premium. Visit subsonic.org for details"
- errors[ErrorDataNotFound] = "The requested data was not found"
+var errors = map[int]string{
+ ErrorGeneric: "A generic error",
+ ErrorMissingParameter: "Required parameter is missing",
+ ErrorClientTooOld: "Incompatible Subsonic REST protocol version. Client must upgrade",
+ ErrorServerTooOld: "Incompatible Subsonic REST protocol version. Server must upgrade",
+ ErrorAuthenticationFail: "Wrong username or password",
+ ErrorAuthorizationFail: "User is not authorized for the given operation",
+ ErrorTrialExpired: "The trial period for the Subsonic server is over. Please upgrade to Subsonic Premium. Visit subsonic.org for details",
+ ErrorDataNotFound: "The requested data was not found",
}
func ErrorMsg(code int) string {
diff --git a/app.go b/app.go
index 106603279..ff720fad7 100644
--- a/app.go
+++ b/app.go
@@ -22,6 +22,7 @@ type App struct {
func (a *App) Initialize() {
a.logger = logrus.New()
+ initMimeTypes()
a.initRoutes()
a.initImporter()
}
diff --git a/conf/configuration.go b/conf/configuration.go
index fd85a4ab1..3bee484be 100644
--- a/conf/configuration.go
+++ b/conf/configuration.go
@@ -33,6 +33,20 @@ func LoadFromFlags() {
l.Load(Sonic)
}
+func LoadFromEnv() {
+ port := os.Getenv("PORT")
+ if port != "" {
+ Sonic.Port = port
+ }
+ l := &multiconfig.EnvironmentLoader{}
+ l.Load(Sonic)
+}
+
+func LoadFromTags() {
+ l := &multiconfig.TagLoader{}
+ l.Load(Sonic)
+}
+
func LoadFromFile(tomlFile string) {
l := &multiconfig.TOMLLoader{Path: tomlFile}
err := l.Load(Sonic)
@@ -47,11 +61,13 @@ func LoadFromLocalFile() {
}
}
+func Load() {
+ LoadFromLocalFile()
+ LoadFromEnv()
+ LoadFromFlags()
+}
+
func init() {
Sonic = new(sonic)
- var l multiconfig.Loader
- l = &multiconfig.TagLoader{}
- l.Load(Sonic)
- l = &multiconfig.EnvironmentLoader{}
- l.Load(Sonic)
+ LoadFromTags()
}
diff --git a/main.go b/main.go
index 777885e74..f346f5a0f 100644
--- a/main.go
+++ b/main.go
@@ -8,8 +8,7 @@ import (
)
func main() {
- conf.LoadFromLocalFile()
- conf.LoadFromFlags()
+ conf.Load()
fmt.Printf("\nCloudSonic Server v%s\n\n", "0.2")
diff --git a/mime_types.go b/mime_types.go
index eb63fb0bd..cac62c64c 100644
--- a/mime_types.go
+++ b/mime_types.go
@@ -2,7 +2,7 @@ package main
import "mime"
-func init() {
+func initMimeTypes() {
mt := map[string]string{
".mp3": "audio/mpeg",
".ogg": "audio/ogg",
@@ -39,6 +39,6 @@ func init() {
}
for ext, typ := range mt {
- mime.AddExtensionType(ext, typ)
+ _ = mime.AddExtensionType(ext, typ)
}
}