-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (64 loc) · 2.02 KB
/
Copy pathmain.go
File metadata and controls
75 lines (64 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"flag"
"fmt"
"github.com/ben-burwood/dashboard/internal/config"
"github.com/ben-burwood/dashboard/internal/render"
"net/http"
"os"
"path/filepath"
)
const (
DashboardConfigPathEnvVar = "DASHBOARD_CONFIG_PATH"
// DefaultBuildOutputPath is where the static export is written when --build is used without --out
DefaultBuildOutputPath = "build/index.html"
)
func main() {
build := flag.Bool("build", false, "render the dashboard to a static HTML file and exit (instead of serving)")
out := flag.String("out", DefaultBuildOutputPath, "output path for --build")
configPath := flag.String("config", "", "config file or directory (overrides $"+DashboardConfigPathEnvVar+")")
flag.Parse()
cfg, err := loadConfiguration(*configPath)
if err != nil {
panic(err)
}
page, err := render.Render(cfg)
if err != nil {
panic(err)
}
if *build {
if err := writeBuild(*out, page); err != nil {
panic(err)
}
fmt.Printf("Wrote %s\n", *out)
return
}
serve(page)
}
// serve renders the dashboard once at startup and serves it over HTTP on :8080
func serve(page []byte) {
mux := http.NewServeMux()
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(page)
})
http.ListenAndServe("[::]:8080", mux)
}
// writeBuild writes the rendered page to path, creating parent directories as needed
func writeBuild(path string, page []byte) error {
if dir := filepath.Dir(path); dir != "" {
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
}
return os.WriteFile(path, page, 0o644)
}
// loadConfiguration resolves the config path from the -config flag, falling back
// to the DASHBOARD_CONFIG_PATH environment variable. When neither is set, an empty
// path is passed and config.LoadConfig applies its own default (config/config.yml).
func loadConfiguration(configPath string) (*config.Config, error) {
if configPath == "" {
configPath = os.Getenv(DashboardConfigPathEnvVar)
}
return config.LoadConfig(configPath)
}