-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (47 loc) · 1.29 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
package main
import (
"context"
"encoding/json"
"fmt"
"html"
"log"
"net/http"
"runtime"
"golang-impression-counter/pkg/redis"
"github.com/gorilla/mux"
)
var ctx = context.Background()
func GetRootRequest(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world, %q, working on %v/%v", html.EscapeString(r.URL.Path), runtime.GOOS, runtime.GOARCH)
json.NewEncoder(w)
}
func GetPing(w http.ResponseWriter, r *http.Request) {
rdc := redis.GetRedisClient()
pong, err := rdc.Ping().Result()
log.Println(ctx, pong, err)
json.NewEncoder(w).Encode(pong)
}
func GetImpression(w http.ResponseWriter, r *http.Request) {
rdc := redis.GetRedisClient()
increment, err := rdc.Incr("counter").Result()
if err != nil {
log.Println(err)
}
json.NewEncoder(w).Encode(increment)
}
func GetCounter(w http.ResponseWriter, r *http.Request) {
rdc := redis.GetRedisClient()
value, err := rdc.Get("counter").Result()
if err != nil {
log.Println(err)
}
json.NewEncoder(w).Encode(value)
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", GetImpression).Methods("GET")
router.HandleFunc("/info", GetRootRequest).Methods("GET")
router.HandleFunc("/ping", GetPing).Methods("GET")
router.HandleFunc("/admin", GetCounter).Methods("GET")
log.Fatal(http.ListenAndServe(":8080", router))
}