package main import ( "encoding/json" "fmt" "log" "net/http" "text/template" "time" ) type WebsiteStatus struct { WebsiteUrl string Description string HasIcon bool IconUrl string } func main() { websites := []WebsiteStatus{ {WebsiteUrl: "https://media.woubery.com", Description: "Jellyfin Server", HasIcon: true, IconUrl: "jellyfin.webp"}, {WebsiteUrl: "https://printing.woubery.com", Description: "OctoPrint", HasIcon: true, IconUrl: "octoprint.png"}, {WebsiteUrl: "https://code.woubery.com", Description: "Gitea Server", HasIcon: true, IconUrl: "gitea.png"}, {WebsiteUrl: "https://stream.woubery.com", Description: "Owncast Server", HasIcon: true, IconUrl: "owncast.png"}, {WebsiteUrl: "https://julia.woubery.com", Description: "Julia JupyterHub Notebook", HasIcon: true, IconUrl: "julia.png"}, {WebsiteUrl: "https://cloud.woubery.com", Description: "Cloud Server", HasIcon: false, IconUrl: ""}, } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { renderTemplate(w, websites) }) http.HandleFunc("/check", handleCheck) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) fmt.Println("Server listening on http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) } func checkWebsite(website string) error { client := &http.Client{ Timeout: 5 * time.Second, // Set a timeout for the request } response, err := client.Head(website) if err != nil { return err } defer response.Body.Close() if response.StatusCode != http.StatusOK { return fmt.Errorf("status code: %d", response.StatusCode) } return nil } func handleCheck(w http.ResponseWriter, r *http.Request) { url := r.URL.Query().Get("url") isUp := checkWebsite(url) == nil response := struct { IsUp bool `json:"isUp"` }{ IsUp: isUp, } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(response) } func renderTemplate(w http.ResponseWriter, statuses []WebsiteStatus) { tmpl := ` Website Status

Ruby's Home Server Status

{{range .}}
{{.Description}}
{{.WebsiteUrl}} {{if .HasIcon}} Icon {{end}}
{{end}}
` t, err := template.New("webpage").Parse(tmpl) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = t.Execute(w, statuses) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }