-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (62 loc) · 1.5 KB
/
main.go
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
package main
import (
"crypto/tls"
"html/template"
"io/ioutil"
"log"
"net/http"
"github.com/russross/blackfriday"
"golang.org/x/crypto/acme/autocert"
)
var (
tmpl *template.Template
content []byte
css []byte
)
func markDowner(input []byte) template.HTML {
s := blackfriday.Run(input)
return template.HTML(s)
}
func init() {
tmpl = template.Must(template.New("template.html").Funcs(template.FuncMap{"markDown": markDowner}).ParseFiles("template.html"))
content, _ = ioutil.ReadFile("home.md")
css, _ = ioutil.ReadFile("style.css")
}
func secureHandler(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/style.css" {
w.Header().Set("Content-Type", "text/css")
w.Write(css)
return
}
var p struct {
Body []byte
}
p.Body = content
err := tmpl.ExecuteTemplate(w, "template.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", secureHandler)
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
Email: "[email protected]",
Cache: autocert.DirCache("/var/cache/acme/"),
HostPolicy: autocert.HostWhitelist("matthewblair.net"),
}
insecure := &http.Server{
Handler: m.HTTPHandler(nil),
Addr: ":80",
}
go insecure.ListenAndServe()
s := http.Server{
Addr: ":https",
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
}
log.Println("Listening on 443; Go to https://matthewblair.net")
err := s.ListenAndServeTLS("", "")
if err != nil {
log.Fatal(err)
}
}