From b768ad0ca4208e8b3588b881067c43d85246e967 Mon Sep 17 00:00:00 2001 From: Collin Williams <96917990+bluedragon1221@users.noreply.github.com> Date: Fri, 2 Oct 2026 11:23:45 -0500 Subject: experiment with modular services; more unix domain sockets, clean up old code --- modules/services/nixos/jta/main.go | 142 ------------------------------------- 1 file changed, 142 deletions(-) delete mode 100644 modules/services/nixos/jta/main.go (limited to 'modules/services/nixos/jta/main.go') diff --git a/modules/services/nixos/jta/main.go b/modules/services/nixos/jta/main.go deleted file mode 100644 index e7b5a0f..0000000 --- a/modules/services/nixos/jta/main.go +++ /dev/null @@ -1,142 +0,0 @@ -package main - -import ( - "bytes" - _ "embed" - "html/template" - "net/http" - "os" - "path/filepath" - "strings" - - "github.com/yuin/goldmark" - "github.com/yuin/goldmark/extension" -) - -// Configure Goldmark with GitHub Flavored Markdown -var mdParser = goldmark.New( - goldmark.WithExtensions( - extension.GFM, - ), -) - -//go:embed md_template.html -var mdTemplate string - -//go:embed style.css -var styleCSS string - -//go:embed index.html -var indexHTML string - -//go:embed assignments.html -var assignmentsHTML string - -var pageTmpl = template.Must(template.New("md_template.html").Parse(mdTemplate)) - -const authCookieName = "jta_auth" - -func isAuthenticated(w http.ResponseWriter, r *http.Request) bool { - expectedHash := strings.TrimSpace(os.Getenv("AUTH_PASSWORD_HASH")) - if expectedHash == "" { - return false - } - - // Try cookie first - if cookie, err := r.Cookie(authCookieName); err == nil { - providedHash := strings.TrimSpace(cookie.Value) - if providedHash != "" && strings.EqualFold(providedHash, expectedHash) { - return true - } - } - - // Fallback to URL parameter - paramHash := strings.TrimSpace(r.URL.Query().Get(authCookieName)) - if paramHash != "" && strings.EqualFold(paramHash, expectedHash) { - http.SetCookie(w, &http.Cookie{ - Name: authCookieName, - Value: paramHash, - Path: "/", - HttpOnly: true, // Security: Protects against XSS cookie theft - Secure: false, // Set to true if you are hosting over HTTPS - SameSite: http.SameSiteLaxMode, // Prevents CSRF vulnerabilities - }) - return true - } - - return false -} - -func serveMarkdown(w http.ResponseWriter, r *http.Request) { - // Prevent path traversal - clean := filepath.Clean(r.URL.Path) - path := filepath.Join(rootDir, strings.TrimPrefix(clean, "/")) - - mdBytes, err := os.ReadFile(path) - if err != nil { - http.NotFound(w, r) - return - } - - var buf bytes.Buffer - if err := mdParser.Convert(mdBytes, &buf); err != nil { - http.Error(w, "failed to render markdown", 500) - return - } - - w.Header().Set("Content-Type", "text/html; charset=utf-8") - - pageTmpl.Execute(w, map[string]template.HTML{ - "Content": template.HTML(buf.String()), - }) -} - -var rootDir = "." - -func main() { - if envRootDir := os.Getenv("ROOT_DIR"); envRootDir != "" { - rootDir = envRootDir - } - - fs := http.FileServer(http.Dir(rootDir)) - port := os.Getenv("PORT") - if port == "" { - port = "8080" - } - - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/" || r.URL.Path == "/index.html" { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.Write([]byte(indexHTML)) - return - } - - if r.URL.Path == "/style.css" { - w.Header().Set("Content-Type", "text/css; charset=utf-8") - w.Write([]byte(styleCSS)) - return - } - - if !isAuthenticated(w, r) { - http.Error(w, "unauthorized", http.StatusUnauthorized) - return - } - - if r.URL.Path == "/assignments" || r.URL.Path == "/assignments.html" { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.Write([]byte(assignmentsHTML)) - return - } - - // Intercept markdown files - if strings.HasSuffix(r.URL.Path, ".md") { - serveMarkdown(w, r) - return - } - - // Otherwise serve normally - fs.ServeHTTP(w, r) - }) - - http.ListenAndServe(":"+port, nil) -} -- cgit v1.3.1