/
main.go
67 lines (57 loc) · 1.33 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
package main
import (
"flag"
"log"
"os"
"os/signal"
"path/filepath"
"runtime/pprof"
"syscall"
"github.com/flynn/json5"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
var (
err error
confPath string
confData []byte
server server
)
if confPath, err = os.UserHomeDir(); err != nil {
log.Fatal("cannot find user home dir")
}
confPath = filepath.Join(confPath, ".config", "usebin", "config.json")
if confData, err = os.ReadFile(confPath); err != nil {
log.Fatal("cannot read config file", err)
}
if err = json5.Unmarshal(confData, &server); err != nil {
log.Fatal("cannot parse config file", err)
}
if server.CertFile != "" {
server.CertFile = filepath.Join(confPath, server.CertFile)
}
if server.KeyFile != "" {
server.KeyFile = filepath.Join(confPath, server.KeyFile)
}
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
log.Printf("CPU profiling started: %s", *cpuprofile)
defer pprof.StopCPUProfile()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
pprof.StopCPUProfile()
log.Printf("CPU profiling saved at %s", *cpuprofile)
os.Exit(1)
}()
}
if err := server.Serve(); err != nil {
log.Fatal(err)
}
}