home-server-status/home_server_status.go

164 lines
3.7 KiB
Go
Raw Permalink Normal View History

package main
2023-06-02 04:32:10 +02:00
import (
2023-06-02 16:30:52 +02:00
"flag"
2023-06-02 04:32:10 +02:00
"fmt"
"log"
"net/http"
2023-06-02 16:30:52 +02:00
"os"
2023-06-02 04:32:10 +02:00
)
2023-06-02 17:06:48 +02:00
var Bold = "\033[1m"
var Reset = "\033[0m"
var Red = "\033[31m"
var Green = "\033[32m"
var Yellow = "\033[33m"
var Blue = "\033[34m"
var Purple = "\033[35m"
var Cyan = "\033[36m"
var Gray = "\033[37m"
var White = "\033[97m"
2023-06-02 04:32:10 +02:00
type WebsiteStatus struct {
2023-06-02 15:47:33 +02:00
WebsiteURL string
2023-06-02 04:32:10 +02:00
Description string
HasIcon bool
2023-06-02 15:47:33 +02:00
IconURL string
2023-06-02 04:32:10 +02:00
}
2023-06-02 16:30:52 +02:00
type Config struct {
Mode string
WebsiteConfFile string
ListenAddress string
Port int
Verbose bool
2023-06-02 17:06:48 +02:00
Color bool
2023-06-02 16:30:52 +02:00
}
var config Config
2023-06-02 04:32:10 +02:00
func main() {
2023-06-02 16:30:52 +02:00
config = parseFlags()
if config.Verbose {
fmt.Printf("Reading websites from file %s", config.WebsiteConfFile)
}
websites, err := readWebsitesFromFile(config.WebsiteConfFile)
2023-06-02 15:47:33 +02:00
if err != nil {
log.Fatal(err)
2023-06-02 04:32:10 +02:00
}
2023-06-02 17:06:48 +02:00
switch config.Mode {
case "terminal":
if config.Verbose {
fmt.Printf("Running in terminal mode\n")
}
runTerminal(websites)
case "server":
if config.Verbose {
fmt.Printf("Running in server mode\n")
}
runWebsite(fmt.Sprintf("%s:%d", config.ListenAddress, config.Port), websites)
}
2023-06-02 16:30:52 +02:00
}
func parseFlags() Config {
// Define command-line flags
websiteConfFileFlag := flag.String("config", "websites.conf", "Path to the config file")
verboseFlag := flag.Bool("verbose", false, "Enable verbose mode")
listenAddressFlag := flag.String("address", "localhost", "The address the server will listen on")
portFlag := flag.Int("port", 8080, "The port the server will listen on")
2023-06-02 17:06:48 +02:00
modeFlag := flag.String("mode", "server", "The mode the program will run in. Options are \"terminal\" and \"server\"")
colorFlag := flag.Bool("color", true, "Use terminal color")
2023-06-02 16:30:52 +02:00
// Parse command-line flags
flag.Parse()
// Access the parsed values
config := Config{
WebsiteConfFile: *websiteConfFileFlag,
Verbose: *verboseFlag,
ListenAddress: *listenAddressFlag,
Port: *portFlag,
Mode: *modeFlag,
2023-06-02 17:06:48 +02:00
Color: *colorFlag,
2023-06-02 16:30:52 +02:00
}
success := true
// Check if the config file exists
if _, err := os.Stat(config.WebsiteConfFile); os.IsNotExist(err) {
success = false
fmt.Printf("File '%s' does not exist\n", config.WebsiteConfFile)
}
if config.Port < 0 || config.Port > 65535 {
success = false
fmt.Printf("Port '%d' is out of range for a port, must be between 0 and 65535\n", config.Port)
}
2023-06-02 17:06:48 +02:00
if config.Mode != "terminal" && config.Mode != "server" {
2023-06-02 16:30:52 +02:00
success = false
2023-06-02 17:06:48 +02:00
fmt.Printf("Mode has to be either terminal or server, but was '%s'\n", config.Mode)
2023-06-02 16:30:52 +02:00
}
if !success {
fmt.Printf("Config error(s), aborting...\n")
os.Exit(1)
}
return config
}
2023-06-02 17:06:48 +02:00
func runTerminal(websites []WebsiteStatus) {
var status = make(map[string]string)
for _, website := range websites {
isUp := ""
if config.Color {
isUp = Bold + Red + "down" + Reset
} else {
isUp = "down"
}
if checkWebsite(website.WebsiteURL) == nil {
if config.Color {
isUp = Bold + Green + "up" + Reset
} else {
isUp = "up"
}
}
status[website.WebsiteURL] = isUp
}
labelWidth := 0
for _, website := range websites {
if len(website.Description) > labelWidth {
labelWidth = len(website.Description)
}
}
labelWidth += 1
for _, website := range websites {
fmt.Printf("%-*s %s\n", labelWidth, website.Description+":", status[website.WebsiteURL])
}
}
2023-06-02 16:30:52 +02:00
func runWebsite(url string, websites []WebsiteStatus) {
2023-06-02 04:32:10 +02:00
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, websites)
2023-06-02 04:32:10 +02:00
})
http.HandleFunc("/check", handleCheck)
2023-06-02 04:32:10 +02:00
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
2023-06-02 16:30:52 +02:00
if config.Verbose {
fmt.Println("Server listening on ", url)
}
2023-06-02 16:30:52 +02:00
log.Fatal(http.ListenAndServe(url, nil))
2023-06-02 04:32:10 +02:00
}