From 55c7c104c0d548837c510ca831e0f1f29c210323 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 22 Sep 2026 17:12:59 -0500 Subject: use embed fs and add options for host and port + api_key_file --- home.go | 6 +++--- main.go | 49 ++++++++++++++++++++++++++++++++++++++++++++----- watch.go | 2 +- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/home.go b/home.go index 48feaef..14ff1ac 100644 --- a/home.go +++ b/home.go @@ -2,14 +2,14 @@ package main import ( "html/template" - "io/ioutil" + "io/fs" "log" "net/http" "github.com/gomarkdown/markdown" ) var templateIndexFiles = []string{ "templates/base.html", "templates/index.html" } -var templateIndex = template.Must(template.ParseFiles(templateIndexFiles...)) +var templateIndex = template.Must(template.ParseFS(Assets, templateIndexFiles...)) var contentHTML template.HTML // just display README.md type IndexTemplateData struct { @@ -17,7 +17,7 @@ type IndexTemplateData struct { } func loadReadme() { - mdBytes, err := ioutil.ReadFile("README.md") + mdBytes, err := fs.ReadFile(Assets, "README.md") if err != nil { log.Fatalf("Failed to read markdown file: %v", err) } diff --git a/main.go b/main.go index ff13499..537ecd6 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,9 @@ package main import ( + "cmp" + "embed" + "io/fs" "log" "net/http" "os" @@ -8,8 +11,40 @@ import ( "github.com/joho/godotenv" ) -var debug = false +/* environment */ +var debug = false var apiKey = "" +var host = "" +var port = "" + +/* embeded filesystem */ +//go:embed static templates README.md +var embedded embed.FS + +func assetsFS() fs.FS { + if debug { + return os.DirFS(".") + } + return embedded +} + +var Assets = assetsFS() + +/* api key */ +func getAPIKey() { + if path := os.Getenv("API_KEY_FILE"); path != "" { + b, err := os.ReadFile(path) + if err != nil { + log.Fatalf("Failed to read API_KEY_FILE: %v", err) + } + apiKey = strings.TrimSpace(string(b)) + } else { + apiKey = os.Getenv("API_KEY") + } + if apiKey == "" { + log.Println("Warning: neither API_KEY and API_KEY_FILE are set. Endpoints that rely on the API key will return 500") + } +} /* root handler */ func rootHandler(w http.ResponseWriter, r *http.Request) { @@ -26,20 +61,24 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { func main() { // load .env file if it exists godotenv.Load() + // setup environment debug = os.Getenv("DEBUG") != "" - apiKey = os.Getenv("API_KEY") + host = cmp.Or(os.Getenv("HOST"), "127.0.0.1") + port = cmp.Or(os.Getenv("PORT"), "8080") + getAPIKey() // setup routes mux := http.NewServeMux() registerRoutesAPI(mux, &APISourceOfficial{}) registerRoutesWatch(mux) setupHome() - mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + static, _ := fs.Sub(Assets, "static") + mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(static)))) mux.HandleFunc("/", rootHandler) // start http server - log.Println("Listening on http://localhost:8080") - err := http.ListenAndServe(":8080", mux) + log.Println("Listening on http://" + host + ":" + port) + err := http.ListenAndServe(host + ":" + port, mux) if err != nil { log.Fatal(err) } diff --git a/watch.go b/watch.go index bc96bd9..5928d82 100644 --- a/watch.go +++ b/watch.go @@ -7,7 +7,7 @@ import ( ) var templateWatchFiles = []string{ "templates/base.html", "templates/watch.html" } -var templateWatch = template.Must(template.ParseFiles(templateWatchFiles...)) +var templateWatch = template.Must(template.ParseFS(Assets, templateWatchFiles...)) /* render template */ type WatchTemplateData struct { -- cgit v1.3.1