-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
201 lines (181 loc) · 5.99 KB
/
main.go
File metadata and controls
201 lines (181 loc) · 5.99 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// go-html-boilerplate loads configuration from a file and starts a HTTP server
// that can render HTML templates and static assets.
//
// See config.yml for an explanation of the configuration options for the
// server, and the Makefile for various tasks you can run in coordination with
// the server (run tests, build assets, start the server).
package main
import (
"bytes"
"errors"
"flag"
"html/template"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
log "github.com/inconshreveable/log15"
"github.com/kevinburke/go-html-boilerplate/assets"
"github.com/kevinburke/handlers"
"github.com/kevinburke/rest"
yaml "gopkg.in/yaml.v2"
)
// DefaultPort is the listening port if no other port is specified.
var DefaultPort = 7065
// The server's Version.
const Version = "0.1"
var errWrongLength = errors.New("Secret key has wrong length. Should be a 64-byte hex string")
var homepageTpl *template.Template
var logger log.Logger
func init() {
homepageHTML := assets.MustAssetString("templates/index.html")
homepageTpl = template.Must(template.New("homepage").Parse(homepageHTML))
logger = handlers.Logger
// Add more templates here.
}
// A HTTP server for static files. All assets are packaged up in the assets
// directory with the go-bindata binary. Run "make assets" to rerun the
// go-bindata binary.
type static struct {
modTime time.Time
}
func (s *static) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/favicon.ico" {
r.URL.Path = "/static/favicon.ico"
}
bits, err := assets.Asset(strings.TrimPrefix(r.URL.Path, "/"))
if err != nil {
rest.NotFound(w, r)
return
}
http.ServeContent(w, r, r.URL.Path, s.modTime, bytes.NewReader(bits))
}
// Render a template, or a server error.
func render(w http.ResponseWriter, r *http.Request, tpl *template.Template, name string, data interface{}) {
buf := new(bytes.Buffer)
if err := tpl.ExecuteTemplate(buf, name, data); err != nil {
rest.ServerError(w, r, err)
return
}
w.Write(buf.Bytes())
}
// NewServeMux returns a HTTP handler that covers all routes known to the
// server.
func NewServeMux() http.Handler {
staticServer := &static{
modTime: time.Now().UTC(),
}
r := new(handlers.Regexp)
r.Handle(regexp.MustCompile(`(^/static|^/favicon.ico$)`), []string{"GET"}, handlers.GZip(staticServer))
r.HandleFunc(regexp.MustCompile(`^/$`), []string{"GET"}, func(w http.ResponseWriter, r *http.Request) {
push(w, "/static/style.css", "style")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
render(w, r, homepageTpl, "homepage", nil)
})
// Add more routes here. Routes not matched will get a 404 error page.
// Call rest.RegisterHandler(404, http.HandlerFunc) to provide your own 404
// page instead of the default.
return r
}
// FileConfig represents the data in a config file.
type FileConfig struct {
// SecretKey is used to encrypt sessions and other data before serving it to
// the client. It should be a hex string that's exactly 64 bytes long. For
// example:
//
// d7211b215341871968869dontusethisc0ff1789fc88e0ac6e296ba36703edf8
//
// That key is invalid - you can generate a random key by running:
//
// openssl rand -hex 32
//
// If no secret key is present, we'll generate one when the server starts.
// However, this means that sessions may error when the server restarts.
//
// If a server key is present, but invalid, the server will not start.
SecretKey string `yaml:"secret_key"`
// Port to listen on. Set to 0 to choose a port at random. If unspecified,
// defaults to 7065.
Port *int `yaml:"port"`
// Set to true to listen for HTTP traffic (instead of TLS traffic). Note
// you need to terminate TLS to use HTTP server push.
HTTPOnly bool `yaml:"http_only"`
// For TLS configuration.
CertFile string `yaml:"cert_file"`
KeyFile string `yaml:"key_file"`
// Add other configuration settings here.
}
var cfg = flag.String("config", "config.yml", "Path to a config file")
func main() {
flag.Parse()
data, err := ioutil.ReadFile(*cfg)
c := new(FileConfig)
if err == nil {
if err := yaml.Unmarshal(data, c); err != nil {
logger.Error("Couldn't parse config file", "err", err)
os.Exit(2)
}
} else {
logger.Error("Couldn't find config file", "err", err)
os.Exit(2)
}
key, err := getSecretKey(c.SecretKey)
if err != nil {
logger.Error("Error getting secret key", "err", err)
os.Exit(2)
}
// You can use the secret key with secretbox
// (godoc.org/golang.org/x/crypto/nacl/secretbox/) to generate cookies and
// secrets. See flash.go and crypto.go for examples.
_ = key
if c.Port == nil {
port, ok := os.LookupEnv("PORT")
if ok {
iPort, err := strconv.Atoi(port)
if err != nil {
logger.Error("Invalid port", "err", err, "port", port)
os.Exit(2)
}
c.Port = &iPort
} else {
c.Port = &DefaultPort
}
}
mux := NewServeMux()
mux = handlers.UUID(mux) // add UUID header
mux = handlers.Server(mux, "go-html-boilerplate/"+Version) // add Server header
mux = handlers.Log(mux) // log requests/responses
mux = handlers.Duration(mux) // add Duration header
addr := ":" + strconv.Itoa(*c.Port)
if c.HTTPOnly {
ln, err := net.Listen("tcp", addr)
if err != nil {
logger.Error("Error listening", "addr", addr, "err", err)
os.Exit(2)
}
logger.Info("Started server", "port", *c.Port)
http.Serve(ln, mux)
} else {
if c.CertFile == "" {
c.CertFile = "cert.pem"
}
if _, err := os.Stat(c.CertFile); os.IsNotExist(err) {
logger.Error("Could not find a cert file; generate using 'make generate_cert'", "file", c.CertFile)
os.Exit(2)
}
if c.KeyFile == "" {
c.KeyFile = "key.pem"
}
if _, err := os.Stat(c.KeyFile); os.IsNotExist(err) {
logger.Error("Could not find a key file; generate using 'make generate_cert'", "file", c.KeyFile)
os.Exit(2)
}
logger.Info("Starting server", "port", *c.Port)
listenErr := http.ListenAndServeTLS(addr, c.CertFile, c.KeyFile, mux)
logger.Error("server shut down", "err", listenErr)
}
}