diff --git a/home-server-status.go b/home-server-status.go new file mode 100644 index 0000000..ae6daaf --- /dev/null +++ b/home-server-status.go @@ -0,0 +1,189 @@ +package home_server_status + +import ( + "fmt" + "log" + "net/http" + "text/template" + "time" +) + +type Website struct { + WebsiteUrl string + Description string + HasIcon bool + IconUrl string +} + +type WebsiteStatus struct { + WebsiteUrl string + Description string + HasIcon bool + IconUrl string + IsUp bool +} + +func main() { + websites := []Website{ + {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) { + statuses := make([]WebsiteStatus, 0) + + for _, website := range websites { + isUp := checkWebsite(website.WebsiteUrl) == nil + statuses = append(statuses, WebsiteStatus{ + WebsiteUrl: website.WebsiteUrl, + Description: website.Description, + HasIcon: website.HasIcon, + IconUrl: website.IconUrl, + IsUp: isUp, + }) + } + renderTemplate(w, statuses) + }) + 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 renderTemplate(w http.ResponseWriter, statuses []WebsiteStatus) { + tmpl := ` + +
+