-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (67 loc) · 1.72 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
package main
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
http.HandleFunc("/", serveHTML)
http.HandleFunc("/events", handleSSE)
server := &http.Server{Addr: ":4321"}
go func() {
fmt.Println("Server is running on http://localhost:4321")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server error: %v", err)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
fmt.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Printf("Error during server shutdown: %v", err)
}
fmt.Println("Server stopped")
}
//go:embed index.html
var indexHTML string
func serveHTML(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(indexHTML))
}
func handleSSE(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
log.Println("SSE connection established")
for {
combinedStats, err := getStats()
if err != nil {
log.Printf("Error getting IPFS Stats: %v", err)
time.Sleep(1 * time.Second)
continue
}
data, err := json.Marshal(combinedStats)
if err != nil {
log.Printf("Error marshaling IPFS stats: %v", err)
time.Sleep(1 * time.Second)
continue
}
_, err = fmt.Fprintf(w, "data: %s\n\n", data)
if err != nil {
log.Printf("Error writing to response: %v", err)
return
}
w.(http.Flusher).Flush()
time.Sleep(1 * time.Second)
}
}