...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package servecmd
18
19 import (
20 "errors"
21 "fmt"
22 "log"
23 "net"
24 "net/http"
25
26 "github.com/sassoftware/relic/cmdline/shared"
27 "github.com/sassoftware/relic/server/daemon"
28 "github.com/spf13/cobra"
29
30 _ "net/http/pprof"
31 )
32
33 var ServeCmd = &cobra.Command{
34 Use: "serve",
35 Short: "Offer signing services over a HTTPS API",
36 RunE: serveCmd,
37 }
38
39 var argTest bool
40
41 func init() {
42 shared.RootCmd.AddCommand(ServeCmd)
43 ServeCmd.Flags().BoolP("force", "f", false, "(ignored)")
44 ServeCmd.Flags().BoolVarP(&argTest, "test", "t", false, "Test configuration and exit")
45 }
46
47 func MakeServer() (*daemon.Daemon, error) {
48 if err := shared.InitConfig(); err != nil {
49 return nil, err
50 }
51 if shared.CurrentConfig.Server == nil {
52 return nil, errors.New("Missing server section in configuration file")
53 }
54 if shared.CurrentConfig.Server.KeyFile == "" {
55 return nil, errors.New("Missing keyfile option in server configuration file")
56 }
57 if shared.CurrentConfig.Server.CertFile == "" {
58 return nil, errors.New("Missing certfile option in server configuration file")
59 }
60 if shared.CurrentConfig.Clients == nil {
61 return nil, errors.New("Missing clients section in configuration file")
62 }
63 if shared.CurrentConfig.Server.Listen == "" {
64 shared.CurrentConfig.Server.Listen = ":6300"
65 }
66 return daemon.New(shared.CurrentConfig, argTest)
67 }
68
69 func listenDebug() error {
70 if !shared.CurrentConfig.Server.ListenDebug {
71 return nil
72 }
73 lis, err := net.Listen("tcp", ":0")
74 if err != nil {
75 return err
76 }
77 fmt.Printf("Serving debug info on http://%s/debug/pprof/\n", lis.Addr())
78 go func() {
79
80 err := http.Serve(lis, nil)
81 if err != nil {
82 fmt.Println(err)
83 }
84 }()
85 return nil
86 }
87
88 func serveCmd(cmd *cobra.Command, args []string) error {
89
90 log.SetFlags(0)
91 srv, err := MakeServer()
92 if err != nil {
93 return shared.Fail(err)
94 } else if argTest {
95 fmt.Println("OK")
96 return nil
97 }
98 go watchSignals(srv)
99 if err := listenDebug(); err != nil {
100 return err
101 }
102 if err := srv.Serve(); err != nil && err != http.ErrServerClosed {
103 return shared.Fail(err)
104 }
105 return nil
106 }
107
View as plain text