-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
65 lines (55 loc) · 1.49 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"net/http/cgi"
"os"
"os/exec"
"github.com/google/uuid"
"github.com/povilasv/prommod"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"within.website/ln"
"within.website/ln/opname"
)
var (
mainFunc = flag.String("main-func", "_start", "main function to call (because rust takes over the name main)")
addr = flag.String("addr", ":8400", "TCP host:port to listen on")
bin = flag.String("bin", "cwagi.wasm", "cgi handler to run")
)
func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), " %s <file.wasm>\n\n", os.Args[0])
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
ctx := opname.With(context.Background(), "main")
f := ln.F{
"main_func": *mainFunc,
"addr": *addr,
}
_ = prometheus.Register(prommod.NewCollector("cwa-cgi"))
cwaPath, err := exec.LookPath("cwa")
if err != nil {
ln.FatalErr(ctx, err)
}
h := &cgi.Handler{
Path: cwaPath,
Env: []string{"RUN_ID=" + uuid.New().String(), "WORKER_ID=" + uuid.New().String()},
Args: []string{*bin},
Dir: "/tmp",
}
mux := http.NewServeMux()
mux.Handle("/", h)
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/reboot", func(w http.ResponseWriter, r *http.Request) {
os.Exit(0)
})
ln.Log(opname.With(ctx, "listenAndServe"), f)
ln.FatalErr(ctx, http.ListenAndServe(*addr, mux), f)
}