forked from tombuildsstuff/builder-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (60 loc) · 1.35 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
package main
import (
"crypto/tls"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/go-redis/redis"
"github.com/tombuildsstuff/builder-app/actions"
)
func main() {
redisServer := os.Getenv("REDIS_HOST")
if redisServer == "" {
panic(fmt.Errorf("`REDIS_HOST` is not set - aborting!"))
}
redisKey := os.Getenv("REDIS_KEY")
if redisServer == "" {
panic(fmt.Errorf("`REDIS_KEY` is not set - aborting!"))
}
redisAddress := fmt.Sprintf("%s:6380", redisServer)
opts := &redis.Options{
Addr: redisAddress,
Password: redisKey,
TLSConfig: &tls.Config{
InsecureSkipVerify: false,
},
}
client := redis.NewClient(opts)
defer client.Close()
if err := run(client, 8080); err != nil {
panic(err)
}
}
func run(client *redis.Client, port int) error {
signalChan := make(chan os.Signal, 2)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
c := actions.NewClient(client, port)
errChan := make(chan error, 2)
go func() {
log.Printf("[DEBUG] Starting update of random value..")
errChan <- c.SetRandomValueInRedis()
}()
go func() {
log.Printf("[DEBUG] Starting web server to retrieve that value..")
errChan <- c.RunWebServer()
}()
for {
select {
case err := <-errChan:
if err != nil {
return err
}
case s := <-signalChan:
log.Printf(fmt.Sprintf("Captured %v. Exiting..", s))
return nil
}
}
return nil
}