2023-09-30 15:35:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-30 16:56:37 +00:00
|
|
|
"log"
|
2023-10-01 00:17:09 +00:00
|
|
|
"net"
|
2023-09-30 15:35:41 +00:00
|
|
|
"net/http"
|
2023-10-01 00:17:09 +00:00
|
|
|
"os"
|
2023-09-30 15:35:41 +00:00
|
|
|
|
2023-09-30 16:56:37 +00:00
|
|
|
"git.exozy.me/exozyme/status/scanner"
|
2023-09-30 15:35:41 +00:00
|
|
|
"github.com/cbroglie/mustache"
|
|
|
|
)
|
|
|
|
|
|
|
|
func parseAndRenderTemplate(templateLocation string, data interface{}) (string, error) {
|
|
|
|
tmpl, err := mustache.ParseFile(templateLocation)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to parse template:", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := tmpl.Render(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to render template:", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-10-01 03:07:29 +00:00
|
|
|
var config scanner.Config = scanner.LoadTOMLConfig()
|
2023-09-30 16:56:37 +00:00
|
|
|
|
2023-09-30 15:35:41 +00:00
|
|
|
func rootPage(w http.ResponseWriter, r *http.Request) {
|
2023-09-30 17:04:55 +00:00
|
|
|
if r.Method != "GET" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-30 16:15:11 +00:00
|
|
|
// get service status'
|
2023-09-30 16:56:37 +00:00
|
|
|
var data []scanner.ServiceStatus
|
|
|
|
for _, v := range config.Service {
|
2023-09-30 17:04:55 +00:00
|
|
|
data = append(data, scanner.TestService(v))
|
2023-09-30 16:56:37 +00:00
|
|
|
}
|
2023-09-30 17:04:55 +00:00
|
|
|
|
2023-09-30 16:56:37 +00:00
|
|
|
log.Printf("%v", data)
|
2023-09-30 17:04:55 +00:00
|
|
|
|
2023-09-30 17:57:42 +00:00
|
|
|
parsedTemplate, err := parseAndRenderTemplate("templates/index.html", data)
|
2023-09-30 15:35:41 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(parsedTemplate))
|
|
|
|
}
|
|
|
|
|
2023-10-01 00:17:09 +00:00
|
|
|
func removeFile(path string) {
|
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 15:35:41 +00:00
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", rootPage)
|
2023-09-30 17:57:42 +00:00
|
|
|
fs := http.FileServer(http.Dir("./public"))
|
|
|
|
http.Handle("/public/", http.StripPrefix("/public/", fs))
|
2023-09-30 15:35:41 +00:00
|
|
|
|
2023-10-01 00:17:09 +00:00
|
|
|
protocol := "tcp"
|
|
|
|
addr := "localhost:3333"
|
|
|
|
|
|
|
|
unix_socket_path, is_unix := os.LookupEnv("PORT")
|
|
|
|
if is_unix {
|
|
|
|
protocol = "unix"
|
|
|
|
addr = unix_socket_path
|
|
|
|
|
|
|
|
removeFile(addr) // ideally this should be run at program exit. sadly Go's defer doesn't care about signals or panic
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := &http.Server{Addr: addr, Handler: nil}
|
|
|
|
ln, err := net.Listen(protocol, addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-10-01 00:24:30 +00:00
|
|
|
if is_unix {
|
|
|
|
err = os.Chmod(addr, 0660)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Printf("Listening on %v\n", addr)
|
|
|
|
} else {
|
|
|
|
log.Printf("Listening on http://%v/\n", addr)
|
|
|
|
}
|
2023-10-01 00:17:09 +00:00
|
|
|
log.Fatal(srv.Serve(ln))
|
2023-09-30 15:35:41 +00:00
|
|
|
}
|