From 88f7537bbdceaac5a50a0818d98bfbd4d057c277 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Thu, 29 Sep 2016 19:55:33 +0200 Subject: [PATCH] Add the XDG specification for configuration file location. --- README.md | 4 ++-- httpAuth.cfg => httpAuth.conf | 0 main.go | 32 ++++++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 6 deletions(-) rename httpAuth.cfg => httpAuth.conf (100%) diff --git a/README.md b/README.md index 601204e..79587da 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,8 @@ Or, in order to build the project you can run the command ``go build main.go``. It will generate a binary that you can run as any binary file. ### Configure -Configure the gateway by editing the ``httpAuth.cfg`` file in order to give all XMPP and HTTP server informations. -An example of the config file can be found in [the repos](https://git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP/src/master/httpAuth.cfg). +Configure the gateway by editing the ``httpAuth.conf`` file in order to give all XMPP and HTTP server informations. This configuration file has to be placed following the [XDG specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). +An example of the config file can be found in [the repos](https://git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP/src/master/httpAuth.conf). XMPP * xmpp_server_address : Component server address connection (default: 127.0.0.1) diff --git a/httpAuth.cfg b/httpAuth.conf similarity index 100% rename from httpAuth.cfg rename to httpAuth.conf diff --git a/main.go b/main.go index 35e7bbf..6838092 100644 --- a/main.go +++ b/main.go @@ -10,13 +10,16 @@ import ( "os" "os/signal" "strconv" + "strings" "syscall" "time" ) const ( Version = "v0.5-dev" - configurationFilePath = "httpAuth.cfg" + configurationFilePath = "httpAuth.conf" + PathConfEnvVariable = "XDG_CONFIG_DIRS" + DefaultXdgConfigDirs = "/etc/xdg" ) var ( @@ -26,9 +29,8 @@ var ( func init() { log.Printf("Running HTTP-Auth %v", Version) - err := cfg.Load(configurationFilePath, mapConfig) - if err != nil { - log.Fatal("Failed to load configuration file.", err) + if !loadConfigFile() { + log.Fatal("Failed to load configuration file.") } // HTTP config @@ -74,6 +76,28 @@ func init() { xmpp.VerifyCertValidity = mapConfig["xmpp_verify_cert_validity"] != "false" // Default TRUE } +func loadConfigFile() bool { + ret := false + envVariable := os.Getenv(PathConfEnvVariable) + if envVariable == "" { + envVariable = DefaultXdgConfigDirs + } + for _, path := range strings.Split(envVariable, ":") { + log.Println("Try to find configuration file into " + path) + configFile := path + "/" + configurationFilePath + if _, err := os.Stat(configFile); err == nil { + // The config file exist + if cfg.Load(configFile, mapConfig) == nil { + // And has been loaded succesfully + log.Println("Find configuration file at " + configFile) + ret = true + break + } + } + } + return ret +} + func main() { go http.Run()