package main import ( "encoding/json" "fmt" "io/ioutil" "log" "os" ) var Conf *Configuration type Configuration struct { TemplateDir string Port int PortSSL int SSL bool DHParam string SPDY string Root string ServerName []string Index []string AccessLog string ErrorLog string SSL_Cert string SSL_Cert_Key string UsePHP bool PHP_TCP bool Robots bool RobotsDisallow []string RobotsAllow []string Cache_Static bool Pagespeed bool Block_Facebook bool UpstreamName string Upstream []string } func NewConfiguration(path string) *Configuration { file, err := os.Open(path) if err != nil { log.Fatal("ERROR: Error opening config file. Please provide on with -config") os.Exit(-1) } decoder := json.NewDecoder(file) conf := &Configuration{} err = decoder.Decode(conf) if err != nil { log.Fatal("ERROR: Error parsing config file.", err) os.Exit(-1) } return conf } func GenConfig(path string) { if path == "" { path = "config.json" } wd, err := os.Getwd() if err != nil { wd = "." } Conf := Configuration{} Conf.TemplateDir = wd + "/templates" Conf.Port = 80 Conf.PortSSL = 443 Conf.SSL = true Conf.DHParam = "/etc/ssl/certs/dhparam.pem" Conf.SPDY = "spdy" Conf.Root = "/var/www/" Conf.ServerName = []string{"example.org", "*.example.org"} Conf.Index = []string{"index.php", "index.html", "index.htm"} Conf.AccessLog = "/var/log/nginx/" + Conf.ServerName[0] + "/access.log" Conf.ErrorLog = "/var/log/nginx/" + Conf.ServerName[0] + "/error.log" Conf.SSL_Cert = "/etc/ssl/certs/ssl-cert-snakeoil.pem" Conf.SSL_Cert_Key = "/etc/ssl/private/ssl-cert-snakeoil.key" Conf.UsePHP = true Conf.PHP_TCP = false Conf.Robots = true Conf.RobotsDisallow = []string{"/"} Conf.RobotsAllow = nil Conf.Cache_Static = true Conf.Pagespeed = true Conf.UpstreamName = "" Conf.Upstream = []string{"127.0.0.1:8080", "127.0.0.1:8081 backup"} file, err := json.MarshalIndent(&Conf, "", " ") if err != nil { log.Fatal("ERROR: Error parsing json.", err) } err = ioutil.WriteFile(path, file, 0644) if err != nil { log.Fatal("ERROR: Writing file to "+path, err) } fmt.Println("INFO: Generated new config file (" + path + ").") }