2023-09-30 15:35:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-10-01 15:40:22 +00:00
|
|
|
"io/fs"
|
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-10-01 15:40:22 +00:00
|
|
|
"embed"
|
|
|
|
|
2023-10-01 08:21:30 +00:00
|
|
|
"git.exozy.me/exozyme/status/core"
|
2023-10-01 15:40:22 +00:00
|
|
|
"git.exozy.me/exozyme/status/scanner"
|
2023-09-30 15:35:41 +00:00
|
|
|
"github.com/cbroglie/mustache"
|
|
|
|
)
|
|
|
|
|
2023-10-01 15:40:22 +00:00
|
|
|
//go:embed all:public
|
|
|
|
var staticAssets embed.FS
|
|
|
|
|
|
|
|
//go:embed all:templates
|
|
|
|
var templateAssets embed.FS
|
|
|
|
|
|
|
|
func parseAndRenderTemplate(templateName string, data interface{}) (string, error) {
|
|
|
|
rawTemplate, err := templateAssets.ReadFile(templateName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := mustache.ParseString(string(rawTemplate))
|
2023-09-30 15:35:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootPage(w http.ResponseWriter, r *http.Request) {
|
2023-09-30 17:04:55 +00:00
|
|
|
if r.Method != "GET" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-01 08:49:02 +00:00
|
|
|
config_new, err := core.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("[WARN] failed to load config:", err)
|
|
|
|
} else {
|
|
|
|
config = config_new
|
|
|
|
}
|
|
|
|
|
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-10-01 08:21:30 +00:00
|
|
|
var config *core.Config
|
|
|
|
|
2023-09-30 15:35:41 +00:00
|
|
|
func main() {
|
2023-10-01 08:32:35 +00:00
|
|
|
var err error
|
|
|
|
config, err = core.LoadConfig()
|
2023-10-01 08:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-09-30 15:35:41 +00:00
|
|
|
http.HandleFunc("/", rootPage)
|
2023-10-01 15:40:22 +00:00
|
|
|
|
|
|
|
// serve public folder from embedfs
|
|
|
|
htmlContent, err := fs.Sub(fs.FS(staticAssets), "public")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fs := http.FileServer(http.FS(htmlContent))
|
2023-09-30 17:57:42 +00:00
|
|
|
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"
|
|
|
|
|
2023-10-01 03:34:12 +00:00
|
|
|
var unix_socket_path string
|
|
|
|
var is_unix bool
|
|
|
|
|
|
|
|
if config.UnixSocket != "" {
|
|
|
|
// get unix socket path from the config else
|
|
|
|
unix_socket_path = config.UnixSocket
|
|
|
|
is_unix = true
|
|
|
|
} else {
|
|
|
|
// fallback
|
|
|
|
unix_socket_path, is_unix = os.LookupEnv("PORT")
|
|
|
|
}
|
|
|
|
|
2023-10-01 00:17:09 +00:00
|
|
|
if is_unix {
|
|
|
|
protocol = "unix"
|
|
|
|
addr = unix_socket_path
|
|
|
|
|
2023-10-01 03:34:12 +00:00
|
|
|
// VnPower: is this unsafe?
|
2023-10-01 00:17:09 +00:00
|
|
|
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
|
|
|
}
|