summaryrefslogtreecommitdiff
path: root/config.go
blob: ba1b237bd42498dffb498eba18c353e20311ba78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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 + ").")
}