-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·65 lines (53 loc) · 1.32 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
package main
import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/ezeql/appcues-increment-simple/internal/hits"
"github.com/go-redis/redis/v7"
)
const (
// Redis host env var name
redisHostEnvName = "REDIS"
// service listen address
listenAddress = ":3333"
)
func main() {
log.SetOutput(os.Stdout)
log.Printf("Increments HTTP server started")
// check for redis env var
redisHost, found := os.LookupEnv(redisHostEnvName)
if !found {
log.Fatalf("required environment var not defined: %v\n", redisHostEnvName)
}
redisClient := redis.NewClient(&redis.Options{Addr: redisHost})
cfg := hits.Config{
RedisClient: redisClient,
}
hits, err := hits.HitsHTTP(cfg)
if err != nil {
log.Fatalf("couldn't create http server: %v\n", err)
}
// launch server with sane defaults
go func() {
log.Printf("service running at %s", listenAddress)
s := http.Server{
Addr: listenAddress,
Handler: hits.Router,
}
if err := s.ListenAndServe(); err != nil {
log.Panicf("error while serving service: %s", err)
}
}()
// TODO: Missing handle graceful shutdown
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT)
defer signal.Stop(signals)
<-signals // wait for signal
go func() {
<-signals // hard exit on second signal (in case shutdown gets stuck)
os.Exit(1)
}()
}