-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
99 lines (82 loc) · 2.24 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/dgraph-io/ristretto"
"github.com/labstack/echo-contrib/prometheus"
echov4 "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"gopkg.in/yaml.v2"
)
var GitCommit string
func main() {
fmt.Printf("Quicksilver (evince): %s\n", GitCommit)
// handle flags
var filename string
flag.StringVar(&filename, "f", "", "YAML file to parse.")
flag.Parse()
if filename == "" {
fmt.Println("Please specify a config file using the -f option")
return
}
e := echov4.New()
e.Logger.SetLevel(log.INFO)
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Skipper: func(c echov4.Context) bool {
return strings.Contains(c.Path(), "metrics")
},
}))
// YAML configuration
yamlfile, err := os.ReadFile(filename)
if err != nil {
e.Logger.Fatalf("%v: %v", ErrReadConfigFile, err)
return
}
var cfg Config
err = yaml.Unmarshal(yamlfile, &cfg)
if err != nil {
e.Logger.Fatalf("%v: %v", ErrParseConfigFile, err)
return
}
// risteretto cache
ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // Num keys to track frequency of (10M).
MaxCost: 1 << 30, // Maximum cost of cache (1GB).
BufferItems: 64, // Number of keys per Get buffer.
})
if err != nil {
e.Logger.Fatalf("unable to start risteretto cache: %v", err)
}
// quick cache service
service := NewCacheService(e, ristrettoCache, cfg)
// routing (see routes.go)
service.ConfigureRoutes()
// start server
go func() {
// Enable metrics middleware
p := prometheus.NewPrometheus("echo", nil)
p.Use(e)
if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed {
e.Logger.Fatalf("%v: %v", ErrEchoFatal, err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server. Use a timeout
// of 30 seconds. Use a buffered channel to avoid missing signals as
// recommended for signal.Notify.
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
fmt.Println("...server shutdown.")
}