Skip to content

Instantly share code, notes, and snippets.

@johntdyer
Created August 23, 2014 17:22
Show Gist options
  • Save johntdyer/19a007b045257124474a to your computer and use it in GitHub Desktop.
Save johntdyer/19a007b045257124474a to your computer and use it in GitHub Desktop.
package configReloader
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
)
// ApplicationConfig struct represents the configuration for the program
type ApplicationConfig struct {
Config ServerConfig `json:"config"`
Ppids map[string]*RouteConfig `json:"ppids"`
IPRanges map[string]*RouteConfig `json:"ip_ranges"`
Applications map[string]*RouteConfig `json:"applications"`
Accounts map[string]*RouteConfig `json:"accounts"`
}
// ServerConfig struct represents the configuration for the server
type ServerConfig struct {
DefaultAPIGateway string `json:"default_api_gateway"`
SyslogServer string `json:"syslog_server"`
DNSServer string `json:"dns_server"`
DNSDomain string `json:"dns_domain"`
Port string `json:"port"`
SessionsURL string `json:"sessions_url"`
SignalsURL string `json:"signals_url"`
}
type RouteConfig struct {
URL string `json:"url"`
Weight float64 `json:"weight"`
}
var (
AppConfig *ApplicationConfig
tempConfig *ApplicationConfig
configFile string
)
func Setup(config string) error {
configFile = config
_, err := LoadConfig()
if err != nil {
return err
}
return nil
}
// ReloadConfig opens the config file and loads
func ReloadConfig() error {
tempConfig := AppConfig
data, err := ioutil.ReadFile(configFile)
if err != nil {
return fmt.Errorf("Unable to load config file: [ %s ]", err)
}
err = json.Unmarshal(data, &tempConfig)
if err != nil {
return fmt.Errorf("Unable to parse config file, will not reload config: [ %s ]", err)
} else {
AppConfig = tempConfig
}
return nil
}
// LoadConfig loads the config the first time, if the file does not exit it fails
func LoadConfig() (string, error) {
data, err := ioutil.ReadFile(configFile)
if err != nil {
return "Unable to load config file", err
}
AppConfig = &ApplicationConfig{}
err = json.Unmarshal(data, &AppConfig)
if err != nil {
return string(data), fmt.Errorf("Unable to parse config: [ %s ]", err)
}
return fmt.Sprintf("Config loaded: ", AppConfig), nil
}
// configSignalWatcher will add a signal hook to SIGURS2 to handle reloads of the configuration file
func ConfigSignalWatcher() (string, error) {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGUSR2)
go func() {
for {
<-s
ReloadConfig()
}
}()
return fmt.Sprintf("Reloaded"), nil
}
package main
import (
log "github.com/Sirupsen/logrus"
configReloader "github.com/johntdyer/configReloader"
"time"
)
func init() {
configReloader.Setup("config.json")
//config.LoadConfig("config/config.json", *ApplicationConfig)
}
func main() {
go configReloader.ConfigSignalWatcher()
for {
time.Sleep(1000 * time.Millisecond)
log.Println("===> " + configReloader.AppConfig.Config.DefaultAPIGateway)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment