Add the XDG specification for configuration file location.

This commit is contained in:
Chteufleur 2016-09-29 19:55:33 +02:00
parent dfa6cd1b32
commit 88f7537bbd
3 changed files with 30 additions and 6 deletions

View File

@ -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)

32
main.go
View File

@ -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()