new config file for job status.

This commit is contained in:
zipfriis
2026-05-27 13:40:25 +02:00
parent 47072ef0b9
commit 7ff604143c
8 changed files with 50 additions and 749 deletions
+31 -30
View File
@@ -1,30 +1,25 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
)
// ---------- HTTP Handlers ----------
func HelloHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/hello" {
http.Error(w, "404 not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "method is not supported", http.StatusNotFound)
return
}
fmt.Fprintf(w, "Hello You")
type Config struct {
Job string `json:"job"`
}
func staticPage(path, file string) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/"+file)
})
func loadConfig() (*Config, error) {
data, err := os.ReadFile("data.json")
if err != nil {
return nil, err
}
var c Config
return &c, json.Unmarshal(data, &c)
}
// ---------- Main ----------
@@ -36,23 +31,29 @@ func main() {
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Redirect(w, r, "/", http.StatusFound)
http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/about.html") })
http.HandleFunc("GET /infra", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/infra.html") })
http.HandleFunc("GET /finance", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/finance.html") })
http.HandleFunc("GET /cinema", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/cinema.html") })
http.HandleFunc("GET /engine", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/engine.html") })
http.HandleFunc("GET /42", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/42.html") })
http.HandleFunc("GET /secret", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/secret.html") })
http.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/icon.png") })
http.HandleFunc("GET /status", func(w http.ResponseWriter, r *http.Request) {
config, err := loadConfig()
if err != nil {
http.Error(w, "failed to load config", http.StatusInternalServerError)
return
}
http.ServeFile(w, r, "./static/about.html")
})
//http.HandleFunc("/portfolio", Portfolio)
staticPage("/infra", "infra.html")
staticPage("/finance", "finance.html")
staticPage("/cinema", "cinema.html")
staticPage("/engine", "engine.html")
staticPage("/42", "42.html")
staticPage("/secret", "secret.html")
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/icon.png")
w.Header().Set("Content-Type", "text/html")
if config.Job == "" || config.Job == "none" {
fmt.Fprint(w, `<span><span class="dot dot--available"></span>available for work</span>`)
} else {
fmt.Fprintf(w, `<span><span class="dot dot--busy"></span>working at: <b>%s</b></span>`, config.Job)
}
})
//http.HandleFunc("/portfolio", Portfolio)