-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmain.go
83 lines (72 loc) · 1.88 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
// Codapi safely executes code snippets using sandboxes.
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"github.com/nalgeon/codapi/internal/config"
"github.com/nalgeon/codapi/internal/logx"
"github.com/nalgeon/codapi/internal/sandbox"
"github.com/nalgeon/codapi/internal/server"
)
// set by the build process
var (
version = "main"
commit = "none"
date = "unknown"
)
// startServer starts the HTTP API sandbox server.
func startServer(port int) *server.Server {
const host = "" // listen on all interfaces
logx.Log("codapi %s, commit %s, built at %s", version, commit, date)
logx.Log("listening on 0.0.0.0:%d...", port)
router := server.NewRouter()
srv := server.NewServer(host, port, router)
srv.Start()
return srv
}
// startDebug servers the debug handlers.
func startDebug(port int) *server.Server {
const host = "localhost"
logx.Log("debugging on localhost:%d...", port)
router := server.NewDebug()
srv := server.NewServer(host, port, router)
srv.Start()
return srv
}
// listenSignals listens for termination signals
// and performs graceful shutdown.
func listenSignals(servers []*server.Server) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
logx.Log("stopping...")
for _, srv := range servers {
err := srv.Stop()
if err != nil {
logx.Log("failed to stop: %v", err)
}
}
}
func main() {
port := flag.Int("port", 1313, "server port")
flag.Parse()
cfg, err := config.Read("configs")
if err != nil {
logx.Log("missing config file")
os.Exit(1)
}
err = sandbox.ApplyConfig(cfg)
if err != nil {
logx.Log("invalid config: %v", err)
os.Exit(1)
}
srv := startServer(*port)
logx.Verbose = cfg.Verbose
logx.Log("workers: %d", cfg.PoolSize)
logx.Log("boxes: %v", cfg.BoxNames())
logx.Log("commands: %v", cfg.CommandNames())
debug := startDebug(6060)
listenSignals([]*server.Server{srv, debug})
}