diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2022-04-17 21:33:34 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2022-04-17 21:33:34 -0230 |
| commit | 1ebdb78c0c710427481da79260e7b7573b331334 (patch) | |
| tree | e40791d2a6bc780f7abfd917446d567d86a7a771 | |
| parent | fefe7774c5b75f8a1c8923a7c394e6f025223e5e (diff) | |
| download | samanthony.xyz-1ebdb78c0c710427481da79260e7b7573b331334.zip | |
add dev mode and tls support to server
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | go.sum | 0 | ||||
| -rw-r--r-- | server.go | 70 |
4 files changed, 65 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..241531c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/devserver +/webserver @@ -1,8 +1,11 @@ -build: tidy format - go build +build_dev: tidy format + go build -o devserver + +serve_dev: build_dev + ./devserver --dev -serve: tidy format - go run . +build: tidy format + GOOS=openbsd GOARCH=amd64 go build -o webserver live: rsync -rtvzP ./htdocs/ sam@samanthony.xyz:/var/www/htdocs/samanthony.xyz/ @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" tmpl "html/template" "io/fs" @@ -12,23 +13,50 @@ import ( "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 ( - host = "" - port = "6969" - root = "htdocs/" + 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(root, func(path string, d fs.DirEntry, err error) error { - if fp.Clean(path) == fp.Clean(root) || + 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(root, "base.html") { + path == fp.Join(htdocs, "base.html") { return nil } - label := path[len(fp.Clean(root)):] - tmpls[label] = tmpl.Must(tmpl.ParseFiles(fp.Join(root, "base.html"), path)) + label := path[len(fp.Clean(htdocs)):] + tmpls[label] = tmpl.Must(tmpl.ParseFiles(fp.Join(htdocs, "base.html"), path)) return nil }) if err != nil { @@ -62,7 +90,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { // If request directory, serve index.html. // ie. /software -> /software/index.html - if info, err := os.Stat(fp.Join(root, reqPath)); err == nil { + if info, err := os.Stat(fp.Join(htdocs, reqPath)); err == nil { if info.IsDir() { reqPath = path.Join(reqPath, "index.html") } @@ -70,7 +98,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } else { - fmt.Println(err) + log.Println(err) code := http.StatusInternalServerError http.Error(w, http.StatusText(code), code) return @@ -89,18 +117,32 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { err := t.Execute(w, page) if err != nil { - fmt.Println(err) + log.Println(err) code := http.StatusInternalServerError http.Error(w, http.StatusText(code), code) return } } else { - http.ServeFile(w, r, fp.Join(root, reqPath)) + http.ServeFile(w, r, fp.Join(htdocs, reqPath)) } } func main() { http.HandleFunc("/", rootHandler) - fmt.Printf("Listening on %s:%s\n", host, port) - log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", host, port), nil)) + 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)) + } } |