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 := ` - -
-