-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
87 lines (73 loc) · 1.75 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
package main
import (
"context"
"crypto/tls"
"database/sql"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
"github.com/gorilla/handlers"
"github.com/zeebo/errs/v2"
"github.com/zeebo/hmux"
"golang.org/x/crypto/acme/autocert"
)
const domain = "pprof.host"
func main() {
if err := run(context.Background()); err != nil {
log.Fatalf("%+v", err)
}
}
func run(ctx context.Context) error {
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?%s",
statePath("profiles.db"),
url.Values{
"_busy_timeout": {"5000"},
"_journal_mode": {"WAL"},
"_synchronous": {"NORMAL"},
}.Encode()))
if err != nil {
return errs.Wrap(err)
}
defer db.Close()
store := &dbStore{db: db}
if err := store.Init(ctx); err != nil {
return errs.Wrap(err)
}
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(domain),
Cache: autocert.DirCache(statePath("certs")),
}
handler := handlers.LoggingHandler(os.Stdout,
certManager.HTTPHandler(hmux.Dir{
"/": hmux.Method{
"GET": uiHandler{store: store},
"POST": createHandler{domain: domain, store: store},
},
"*": hmux.Arg("name").Capture(pprofHandler{store: store}),
}))
lis443, lis80, err := listen(ctx)
if err != nil {
return errs.Wrap(err)
}
defer lis443.Close()
defer lis80.Close()
srv := &http.Server{
Handler: handler,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
SessionTicketsDisabled: true,
GetCertificate: certManager.GetCertificate,
},
}
go func() { panic(srv.ServeTLS(lis443, "", "")) }()
go func() { panic(srv.Serve(lis80)) }()
select {}
}
func statePath(path string) string {
return filepath.Join(os.Getenv("STATE_DIRECTORY"), path)
}