-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (144 loc) · 3.51 KB
/
main.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"embed"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"time"
"github.com/shirou/gopsutil/v3/host"
)
// todo! add -ldflags (build time)
// references: https://blog.alexellis.io/inject-build-time-vars-golang/
var (
//go:embed all:static
staticFiles embed.FS
commit string
buildTimestamp string
year string
)
type TemplateData struct {
Footer Footer
Stats Stats
}
type Footer struct {
// Timestamp time.Time
// FormattedTime string
BuildTimestamp string
CommitHash string
Year string
}
type Stats struct {
Hostname string
Uptime string
KernelVersion string
KernelArch string
}
// todo! add dynamic path after /pprof/{goroutine,heap,allocs,etcetc}
// with switch case maybe (?)
// type profiles string
func renderTemplate(stats Stats) (TemplateData, error) {
stats, err := getStatus()
if err != nil {
return TemplateData{}, err
}
footer := Footer{
BuildTimestamp: buildTimestamp,
CommitHash: commit,
Year: year,
}
templateData := TemplateData{
Stats: stats,
Footer: footer,
}
return templateData, nil
}
func handleStatic(tpl *template.Template) http.Handler {
rootFS, err := fs.Sub(staticFiles, "static")
if err != nil {
log.Printf("Error: Failed to load FS: %v", err)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
rendering, err := renderTemplate(Stats{})
if err != nil {
log.Printf("Error rendering template: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
err = tpl.Execute(w, rendering)
if err != nil {
log.Printf("Error rendering template: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
return
}
http.FileServer(http.FS(rootFS)).ServeHTTP(w, r)
})
}
func handlePprof(w http.ResponseWriter, req *http.Request) {
getPprof := pprof.Lookup("heap")
err := getPprof.WriteTo(w, 1)
if err != nil {
log.Printf("Error: Failed to write pprof: %v", err)
}
}
func getStatus() (Stats, error) {
stats, err := host.Info()
if err != nil {
return Stats{}, err
}
convertTime := time.Duration(stats.Uptime) * time.Second
return Stats{
Hostname: stats.Hostname,
Uptime: convertTime.String(),
KernelVersion: stats.KernelVersion,
KernelArch: stats.KernelArch,
}, nil
}
func gracefulShutdown(server *http.Server, timeout time.Duration) error {
done := make(chan error, 1)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
<-c
log.Println("Server is shutting down...")
ctx := context.Background()
var cancel context.CancelFunc
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
done <- server.Shutdown(ctx)
}()
log.Println("Starting HTTP server...")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
log.Println("Byeee")
return <-done
}
// main function
func main() {
router := http.NewServeMux()
tpl, err := template.ParseFS(staticFiles, "static/index.html")
if err != nil {
log.Fatalf("Error parsing template: %s", err)
}
// handler
router.Handle("/", handleStatic(tpl))
router.HandleFunc("/pprof", handlePprof)
server := &http.Server{
Addr: ":8080",
Handler: router,
IdleTimeout: 60 * time.Second,
}
if err := gracefulShutdown(server, 10*time.Second); err != nil {
log.Println(err)
}
}