From 8aa255b91a333a6ffe506f77bfeafc5793e48392 Mon Sep 17 00:00:00 2001 From: Anton Reinhard Date: Fri, 2 Jun 2023 15:47:33 +0200 Subject: [PATCH] Split into multiple files --- functions.go | 92 ++++++++++++++++++++++++ go.mod | 3 + main.go | 188 ++------------------------------------------------ template.html | 125 +++++++++++++++++++++++++++++++++ websites.conf | 6 ++ 5 files changed, 231 insertions(+), 183 deletions(-) create mode 100644 functions.go create mode 100644 go.mod create mode 100644 template.html create mode 100644 websites.conf diff --git a/functions.go b/functions.go new file mode 100644 index 0000000..5980595 --- /dev/null +++ b/functions.go @@ -0,0 +1,92 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "html/template" + "net/http" + "os" + "strconv" + "strings" + "time" +) + +func readWebsitesFromFile(filepath string) ([]WebsiteStatus, error) { + file, err := os.Open(filepath) + if err != nil { + return nil, err + } + defer file.Close() + + var websites []WebsiteStatus + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + fields := strings.Split(line, ",") + if len(fields) != 4 { + continue + } + + hasIcon, _ := strconv.ParseBool(fields[2]) + + website := WebsiteStatus{ + WebsiteURL: fields[0], + Description: fields[1], + HasIcon: hasIcon, + IconURL: fields[3], + } + + websites = append(websites, website) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return websites, 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) { + t, err := template.ParseFiles("template.html") + 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) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0201b99 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module home_server_status + +go 1.20 diff --git a/main.go b/main.go index 30aad34..c24d614 100644 --- a/main.go +++ b/main.go @@ -1,29 +1,22 @@ package main import ( - "encoding/json" "fmt" "log" "net/http" - "text/template" - "time" ) type WebsiteStatus struct { - WebsiteUrl string + WebsiteURL string Description string HasIcon bool - IconUrl string + 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: ""}, + websites, err := readWebsitesFromFile("websites.conf") + if err != nil { + log.Fatal(err) } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -36,174 +29,3 @@ func main() { 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 .}} - - {{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) - } -} diff --git a/template.html b/template.html new file mode 100644 index 0000000..260bfe6 --- /dev/null +++ b/template.html @@ -0,0 +1,125 @@ + + + Website Status + + + + + + +
+

Ruby's Home Server Status

+ {{range .}} + + {{end}} +
+ + \ No newline at end of file diff --git a/websites.conf b/websites.conf new file mode 100644 index 0000000..2a4667b --- /dev/null +++ b/websites.conf @@ -0,0 +1,6 @@ +https://media.woubery.com,Jellyfin Server,true,jellyfin.webp +https://printing.woubery.com,OctoPrint,true,octoprint.png +https://code.woubery.com,Gitea Server,true,gitea.png +https://stream.woubery.com,Owncast Server,true,owncast.png +https://julia.woubery.com,Julia JupyterHub Notebook,true,julia.png +https://cloud.woubery.com,Cloud Server,false, \ No newline at end of file