This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
serve.go
72 lines (57 loc) · 1.7 KB
/
serve.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
package main
import (
"fmt"
"net"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/weaveworks/footloose/pkg/api"
"github.com/weaveworks/footloose/pkg/cluster"
)
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Launch a footloose server",
RunE: serve,
}
var serveOptions struct {
listen string
keyStorePath string
debug bool
}
func baseURI(addr string) (string, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return "", err
}
if host == "" || host == "0.0.0.0" || host == "[::]" {
host = "localhost"
}
return fmt.Sprintf("http://%s:%s", host, port), nil
}
func init() {
serveCmd.Flags().StringVarP(&serveOptions.listen, "listen", "l", ":2444", "Cluster configuration file")
serveCmd.Flags().StringVar(&serveOptions.keyStorePath, "keystore-path", defaultKeyStorePath, "Path of the public keys store")
serveCmd.Flags().BoolVar(&serveOptions.debug, "debug", false, "Enable debug")
footloose.AddCommand(serveCmd)
}
func serve(cmd *cobra.Command, args []string) error {
opts := &serveOptions
baseURI, err := baseURI(opts.listen)
if err != nil {
return errors.Wrapf(err, "invalid listen address '%s'", opts.listen)
}
log.Infof("Starting server on: %s\n", opts.listen)
keyStore := cluster.NewKeyStore(opts.keyStorePath)
if err := keyStore.Init(); err != nil {
return errors.Wrapf(err, "could not init keystore")
}
log.Infof("Key store successfully initialized in path: %s\n", opts.keyStorePath)
api := api.New(baseURI, keyStore, opts.debug)
router := api.Router()
err = http.ListenAndServe(opts.listen, router)
if err != nil {
log.Fatalf("Unable to start server: %s", err)
}
return nil
}