forked from nuggien/devops_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (48 loc) · 1.19 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/go-redis/redis/v8"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
"time"
)
type server struct {
redis redis.UniversalClient
logger *zap.Logger
}
func main() {
logger, err := zap.NewProduction()
if err != nil {
log.Fatal("unable to initialize logger")
}
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{os.Getenv("REDIS_ADDR")},
Password: "",
DB: 0,
})
srv := &server{
redis: rdb,
logger: logger,
}
router := httprouter.New()
router.GET("/", srv.indexHandler)
logger.Info("server started on port 8080")
log.Fatal(http.ListenAndServe(":8080", router))
}
func (s *server) indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var v string
var err error
if v, err = s.redis.Get(context.Background(), "updated_time").Result(); err != nil {
s.logger.Info("updated_time not found, setting it")
v = time.Now().Format("2006-01-02 03:04:05")
s.redis.Set(context.Background(), "updated_time", v, 5*time.Second)
} else {
s.logger.Info("got updated_time")
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "hello world: updated_time=%s\n", v)
}