-
Notifications
You must be signed in to change notification settings - Fork 3
/
route.go
82 lines (71 loc) · 1.83 KB
/
route.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"github.com/gorilla/handlers"
"github.com/whytheplatypus/switchboard/operator"
)
func route(args []string, ctx context.Context) {
flags := flag.NewFlagSet("route", flag.ExitOnError)
port := flags.Int("port", 80, "the port this should run on")
cdir := flags.String("cert-directory", "/var/cache/switchboard/autocert", "the directory to store the acme cert")
var domains StringArray
flags.Var(&domains, "domain", "a domain to register a tls cert for")
httpLog := flags.String("log-http", "", "The address to serve logs over, no logs are served if empty")
flags.Parse(args)
if *httpLog != "" {
configureLog(*httpLog)
}
go func() {
entries := operator.Listen(ctx)
for entry := range entries {
if err := operator.Connect(entry); err != nil {
registrationLog.Println(err)
continue
}
// register
registrationLog.Printf(`{"send":"%s","to":"http://%s:%d"}`,
entry.InfoFields[0],
entry.AddrV4,
entry.Port,
)
}
}()
router := operator.Handler()
router.ModifyResponse = func(r *http.Response) error {
info := struct {
Host string `json:"host"`
Target string `json:"target"`
Path string `json:"path"`
Query string `json:"query"`
}{
r.Request.Host,
r.Request.URL.Host,
r.Request.URL.Path,
r.Request.URL.RawQuery,
}
b, _ := json.Marshal(info)
routingLog.Println(string(b))
return nil
}
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
http.NotFound(rw, r)
}
}()
router.ServeHTTP(rw, r)
})
srv := &server{
Addr: fmt.Sprintf(":%d", *port),
Handler: handlers.LoggingHandler(&lWriter{accessLog}, h),
CertDir: *cdir,
Domains: domains,
}
if err := srv.serve(ctx); err != nil {
routingLog.Fatal(err)
}
}