-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (56 loc) · 1.37 KB
/
main.go
File metadata and controls
66 lines (56 loc) · 1.37 KB
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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/go-redis/redis/v7"
)
type config struct {
bind string
config string
password string
redisURL string
}
func parseFlags() (config, error) {
c := config{}
flag.StringVar(&c.bind, "bind", "0.0.0.0:8080", "The host:port to bind to.")
flag.StringVar(&c.config, "config", "", "The location of the config file.")
flag.StringVar(&c.password, "read-password", "", "The password to read keys from this service.")
flag.StringVar(&c.redisURL, "redis-url", "", "URL for connecting to redis")
flag.Parse()
if c.config == "" {
return c, fmt.Errorf("you must specify --config")
}
if c.redisURL == "" {
return c, fmt.Errorf("you must specify --redis-url")
}
return c, nil
}
func main() {
c, err := parseFlags()
if err != nil {
log.Println(err)
os.Exit(2)
}
redisOptions, err := redis.ParseURL(c.redisURL)
if err != nil {
log.Printf("Couldn't parse redis URL: %v\n", err)
os.Exit(1)
}
r := redis.NewClient(redisOptions)
if _, err := r.Ping().Result(); err != nil {
log.Printf("Couldn't communicate with redis: %v.\n", err)
os.Exit(1)
}
sm, err := NewStreamMapper(c.config)
if err != nil {
log.Printf("Couldn't parse stream mapping: %v.\n", err)
os.Exit(1)
}
sm.Run()
st := NewStreamTracker(sm, r, c.password)
http.Handle("/", st)
log.Fatal(http.ListenAndServe(c.bind, nil))
}