-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.go
61 lines (49 loc) · 1.21 KB
/
run.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
package cmd
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/barnbridge/internal-api/api"
"github.com/barnbridge/internal-api/config"
"github.com/barnbridge/internal-api/db"
)
var runCmd = &cobra.Command{
Use: "run",
Short: "Say hello!",
Long: "Address a wonderful greeting to the majestic executioner of this CLI",
Run: func(cmd *cobra.Command, args []string) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
listenOn := fmt.Sprintf(":%d", config.Store.Metrics.Port)
sm := http.NewServeMux()
sm.Handle("/metrics", promhttp.Handler())
metricsSrv := &http.Server{Addr: listenOn, Handler: sm}
go func() {
log.Infof("serving metrics on %s", listenOn)
err := metricsSrv.ListenAndServe()
if err != nil && ctx.Err() == nil {
log.Fatal(err)
}
}()
db, err := db.New()
if err != nil {
log.Fatal(err)
}
a := api.New(db)
go a.Run()
<-ctx.Done()
// cleanup
_ = metricsSrv.Close()
log.Info("Work done. Goodbye!")
},
}
func init() {
RootCmd.AddCommand(runCmd)
addAPIFlags(runCmd)
addDBFlags(runCmd)
addAddressesFlags(runCmd)
}