1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2025-06-28 16:32:46 +02:00

Restructure configuration

This commit is contained in:
Alexandre Iooss
2020-09-22 11:42:57 +02:00
parent c799a5b613
commit 5ac336393b
11 changed files with 107 additions and 104 deletions

View File

@ -1,77 +0,0 @@
/*
* Copyright (C) 2020 Cr@ns <roots@crans.org>
* Authors : Alexandre Iooss <erdnaxe@crans.org>
* SPDX-License-Identifier: MIT
*/
package config
import (
"log"
"strings"
"github.com/spf13/viper"
)
// Config holds app configuration
type Config struct {
AuthBackend string
LDAP struct {
URI string
UserDn string
}
Prometheus struct {
ListenAddress string
}
Site struct {
ListenAddress string
Name string
Hostname string
Favicon string
WidgetURL string
}
}
// New configuration
func New() (*Config, error) {
// Load configuration from environnement variables
// Replace "." to "_" for nested structs
// e.g. GHOSTREAM_LDAP_URI will apply to Config.LDAP.URI
viper.SetEnvPrefix("ghostream")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
// Load configuration file if exists
viper.SetConfigName("ghostream")
viper.SetConfigType("yaml")
viper.AddConfigPath("$HOME/.ghostream")
viper.AddConfigPath("/etc/ghostream")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found, ignore and use defaults
log.Print(err)
} else {
// Config file was found but another error was produced
log.Fatal(err)
}
} else {
// Config loaded
log.Printf("Using config file: %s", viper.ConfigFileUsed())
}
// Define configuration default values
viper.SetDefault("AuthBackend", "LDAP")
viper.SetDefault("LDAP.URI", "ldap://127.0.0.1:389")
viper.SetDefault("LDAP.UserDn", "cn=users,dc=example,dc=com")
viper.SetDefault("Prometheus.ListenAddress", "0.0.0.0:2112")
viper.SetDefault("Site.ListenAddress", "127.0.0.1:8080")
viper.SetDefault("Site.Name", "Ghostream")
viper.SetDefault("Site.Hostname", "localhost")
viper.SetDefault("Site.Favicon", "/favicon.ico")
config := &Config{}
err := viper.Unmarshal(config)
return config, err
}

View File

@ -7,9 +7,13 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gitlab.crans.org/nounous/ghostream/internal/config"
)
// Options holds web package configuration
type Options struct {
ListenAddress string
}
var (
// ViewerServed is the total amount of viewer page served
ViewerServed = promauto.NewCounter(prometheus.CounterOpts{
@ -19,9 +23,9 @@ var (
)
// ServeHTTP server that expose prometheus metrics
func ServeHTTP(cfg *config.Config) {
func ServeHTTP(cfg *Options) {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
log.Printf("Monitoring listening on http://%s/", cfg.Prometheus.ListenAddress)
log.Fatal(http.ListenAndServe(cfg.Prometheus.ListenAddress, mux))
log.Printf("Monitoring HTTP server listening on %s", cfg.ListenAddress)
log.Fatal(http.ListenAndServe(cfg.ListenAddress, mux))
}