From 1ebdb78c0c710427481da79260e7b7573b331334 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sun, 17 Apr 2022 21:33:34 -0230 Subject: add dev mode and tls support to server --- server.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 14 deletions(-) (limited to 'server.go') diff --git a/server.go b/server.go index d40c86c..225d77f 100644 --- a/server.go +++ b/server.go @@ -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)) + } } -- cgit v1.2.3