From a45ec48ef2a48c75d7c20d9c2d20a34de2fe2ba3 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Wed, 8 Apr 2026 20:33:36 -0400 Subject: feat: JSON Configuration File Added support for a JSON configuration file to configure ceterin aspects of the website without having to modify the Go code. --- config.json | 19 +++++++++++++++++++ main.go | 56 +++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..fe38b72 --- /dev/null +++ b/config.json @@ -0,0 +1,19 @@ +{ + "port" : "8000", + "allowed" : [ + "static/rss.xml", + "static/card", + "static/css/main.css", + "static/logos/FirstInitialLogo.svg", + "static/logos/FullNameLogo.svg", + "static/logos/favicon.png", + "static/logos/favicon.svg", + "static/logos/favicon16.png", + "static/logos/favicon32.png", + "static/robots.txt", + "static/home.md" + ], + "HomeHtml": "static/home.md", + "MainTemplate" : "templates/template.html", + "ErrorTemplate" : "templates/error.html" +} diff --git a/main.go b/main.go index 7fd73d2..6f2ae77 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" @@ -13,6 +14,16 @@ import ( "slices" ) +type Config struct { + Port string + Allowed []string + HomeHtml string + MainTemplate string + ErrorTemplate string +} + +var config Config + func GetHome(w http.ResponseWriter, r *http.Request) { log.Println("Request for:", r.URL.String()) @@ -21,9 +32,9 @@ func GetHome(w http.ResponseWriter, r *http.Request) { return } - t := template.Must(template.ParseFiles("templates/template.html")) + t := template.Must(template.ParseFiles(config.MainTemplate)) - html, err := MarkdownToHTML("static/home.md") + html, err := MarkdownToHTML(config.HomeHtml) if err != nil { ErrorPage(w, "Internal Server Error", 500) } else { @@ -35,20 +46,7 @@ func GetFiles(w http.ResponseWriter, r *http.Request) { log.Println("Request for:", r.URL.String()) url := fmt.Sprintf("static%s", r.URL.String()) - allowed := []string{ - "static/rss.xml", - "static/card", - "static/css/main.css", - "static/logos/FirstInitialLogo.svg", - "static/logos/FullNameLogo.svg", - "static/logos/favicon.png", - "static/logos/favicon.svg", - "static/logos/favicon16.png", - "static/logos/favicon32.png", - "static/robots.txt", - } - - if slices.Contains(allowed, url) { + if slices.Contains(config.Allowed, url) { http.ServeFile(w, r, url) } else { ErrorPage(w, "Page Not Found.", 404) @@ -81,7 +79,7 @@ func MarkdownToHTML(path string) (string, error) { func ErrorPage(w http.ResponseWriter, message string, code int) { log.Println(code, message) - t := template.Must(template.ParseFiles("templates/error.html")) + t := template.Must(template.ParseFiles(config.ErrorTemplate)) t.Execute(w, struct { Code int Message string @@ -96,6 +94,7 @@ func Usage() { } func main() { + portFlagSet := false port := ":8000" for i := 1; i < len(os.Args); i++ { @@ -104,6 +103,7 @@ func main() { if i+1 >= len(os.Args) { Usage() } else { + portFlagSet = true port = ":" + os.Args[i+1] i++ } @@ -113,6 +113,28 @@ func main() { } } + configJson, err := os.ReadFile("config.json") + if err != nil { + panic(err) + } + + err = json.Unmarshal(configJson, &config) + if err != nil { + panic(err) + } + + if !portFlagSet { + port = ":" + config.Port + } + + fmt.Println(config.Port) + for i, a := range config.Allowed { + fmt.Printf("%d: %s\n", i, a) + } + fmt.Println(config.HomeHtml) + fmt.Println(config.MainTemplate) + fmt.Println(config.ErrorTemplate) + http.HandleFunc("/robots.txt", GetFiles) http.HandleFunc("/rss.xml", GetFiles) http.HandleFunc("/card", GetFiles) -- cgit v1.2.3