-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathroute.go
More file actions
75 lines (65 loc) · 1.82 KB
/
route.go
File metadata and controls
75 lines (65 loc) · 1.82 KB
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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strings"
"github.com/gorilla/handlers"
"github.com/whytheplatypus/switchboard/config"
"github.com/whytheplatypus/switchboard/operator"
)
func route(args []string, ctx context.Context) {
flags := flag.NewFlagSet("route", flag.ContinueOnError)
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.StringVar(&config.Iface, "iface", "", "interface to listen on")
if err := flags.Parse(args); err != nil && !strings.HasPrefix(err.Error(), "flag provided but not defined") {
log.Fatal(err)
}
if *httpLog != "" {
configureLog(*httpLog)
}
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,
}
go func() {
operator.Listen(ctx, operator.DefaultRouter)
}()
if err := srv.serve(ctx); err != nil {
routingLog.Fatal(err)
}
}