-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (91 loc) · 2.5 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
package main
import (
"context"
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"net/http/httputil"
"os"
"time"
"connectrpc.com/connect"
"golang.org/x/exp/slices"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"github.com/shirou/gopsutil/cpu"
apiv1 "connect-go-embed-vue/gen/api/v1"
"connect-go-embed-vue/gen/api/v1/apiv1connect"
)
type SayHelloServer struct{}
func (s *SayHelloServer) SayHello(
ctx context.Context,
req *connect.Request[apiv1.SayHelloRequest],
) (*connect.Response[apiv1.SayHelloResponse], error) {
res := connect.NewResponse(&apiv1.SayHelloResponse{
Reply: fmt.Sprintf("Hello, %s!", req.Msg.Name),
})
return res, nil
}
type MetricsServer struct{}
func (s *MetricsServer) CpuUsageStream(
ctx context.Context,
req *connect.Request[apiv1.CpuUsageStreamRequest],
stream *connect.ServerStream[apiv1.CpuUsageStreamResponse],
) error {
for {
percent, errPercent := cpu.Percent(1*time.Second, false)
if errPercent != nil {
log.Println(errPercent)
return connect.NewError(connect.CodeUnknown, errPercent)
}
err := stream.Send(&apiv1.CpuUsageStreamResponse{
Percent: float32(percent[0]),
})
if err != nil {
log.Println(err)
return err
}
}
}
func getEnv(key string, fallback string, acceptable []string) string {
if value, ok := os.LookupEnv(key); ok {
if len(acceptable) == 0 || slices.Contains(acceptable, value) {
return value
}
}
return fallback
}
//go:embed frontend/dist/*
var static embed.FS
func main() {
isDev := getEnv("APP_ENV", "production", []string{"development", "production"}) == "development"
host := getEnv("APP_HOST", "0.0.0.0", nil)
port := getEnv("APP_PORT", "8080", nil)
mux := http.NewServeMux()
if isDev {
log.Println("[Dev] proxy vite dev page.")
frontProxy := &httputil.ReverseProxy{Director: func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = ":5173"
}}
mux.Handle("/", frontProxy)
} else {
log.Println("[Prod] serve embed source.")
documentRoot, err := fs.Sub(static, "frontend/dist")
if err != nil {
log.Fatal(err)
}
mux.Handle("/", http.FileServer(http.FS(documentRoot)))
}
api := http.NewServeMux()
api.Handle(apiv1connect.NewSayHelloServiceHandler(&SayHelloServer{}))
api.Handle(apiv1connect.NewMetricsServiceHandler(&MetricsServer{}))
mux.Handle("/api/", http.StripPrefix("/api", api))
addr := fmt.Sprintf("%s:%s", host, port)
log.Printf("Bind address %s", addr)
http.ListenAndServe(
addr,
h2c.NewHandler(mux, &http2.Server{}), // Use h2c so we can serve HTTP/2 without TLS.
)
}