summaryrefslogtreecommitdiffstats
path: root/server.go
blob: 225d77f4e7a1100d9f47409cd0930e818dbd5773 (plain) (blame)
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
package main

import (
	"flag"
	"fmt"
	tmpl "html/template"
	"io/fs"
	"log"
	"net/http"
	"os"
	"path"
	fp "path/filepath"
	"strings"
)

var (
	host   = "samanthony.xyz"
	port   = "443"
	htdocs = "/var/www/htdocs/samanthony.xyz"
)

const (
	acmeDocs = "/var/www/acme/"
	certFile = "/etc/ssl/samanthony.xyz.fullchain.pem"
	keyFile  = "/etc/ssl/private/samanthony.xyz.key"
)

const (
	devHost   = "localhost"
	devPort   = "6969"
	devHtdocs = "htdocs/"
)

var devMode bool

func init() {
	flag.BoolVar(&devMode, "dev", false,
		"Run server in debug/development mode (on localhost without tls)")

	flag.Parse()

	if devMode {
		host = devHost
		port = devPort
		htdocs = devHtdocs
	}
}

var tmpls = make(map[string]*tmpl.Template)

func init() {
	err := fp.WalkDir(htdocs, func(path string, d fs.DirEntry, err error) error {
		if fp.Clean(path) == fp.Clean(htdocs) ||
			fp.Ext(path) != ".html" ||
			path == fp.Join(htdocs, "base.html") {
			return nil
		}
		label := path[len(fp.Clean(htdocs)):]
		tmpls[label] = tmpl.Must(tmpl.ParseFiles(fp.Join(htdocs, "base.html"), path))
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
}

type Page struct {
	Nav Nav
}

type Nav struct {
	ThisSection string
	Links       []NavLink
}

type NavLink struct {
	Href  string
	Label string
}

var nav = Nav{
	Links: []NavLink{
		{"/", "samanthony.xyz"},
		{"/software/", "software"},
	},
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
	reqPath := r.URL.Path

	// If request directory, serve index.html.
	// ie. /software -> /software/index.html
	if info, err := os.Stat(fp.Join(htdocs, reqPath)); err == nil {
		if info.IsDir() {
			reqPath = path.Join(reqPath, "index.html")
		}
	} else if os.IsNotExist(err) {
		http.NotFound(w, r)
		return
	} else {
		log.Println(err)
		code := http.StatusInternalServerError
		http.Error(w, http.StatusText(code), code)
		return
	}

	if t, ok := tmpls[reqPath]; ok {
		thisSection := ""
		for _, link := range nav.Links {
			if strings.HasPrefix(reqPath, link.Href) {
				thisSection = link.Href
			}
		}
		nav := nav
		nav.ThisSection = thisSection
		page := Page{nav}

		err := t.Execute(w, page)
		if err != nil {
			log.Println(err)
			code := http.StatusInternalServerError
			http.Error(w, http.StatusText(code), code)
			return
		}
	} else {
		http.ServeFile(w, r, fp.Join(htdocs, reqPath))
	}
}

func main() {
	http.HandleFunc("/", rootHandler)
	if !devMode {
		http.Handle("/.well-known/acme-challenge/",
			http.StripPrefix(
				"/.well-known/acme-challenge/",
				http.FileServer(http.Dir(acmeDocs)),
			),
		)
	}

	if devMode {
		log.Printf("Listening on %s:%s\n", devHost, devPort)
		log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", devHost, devPort), nil))
	} else {
		log.Printf("Listening on %s:%s\n", host, port)
		log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%s", host, port), certFile, keyFile, nil))
	}
}