-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
164 lines (136 loc) · 3.38 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
// Document of redigo
// https://godoc.org/github.com/gomodule/redigo/redis
// Commands of redis
// https://redis.io/commands
import (
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
redis "github.com/gomodule/redigo/redis"
"github.com/labstack/echo"
)
var (
redisPool *redis.Pool
)
type Renderer struct {
templates *template.Template
}
func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return r.templates.ExecuteTemplate(w, name, data)
}
func redisIncr(key string) error {
redisConn := redisPool.Get()
defer redisConn.Close()
// 基本的にredisのコマンドをそのまま呼び出す
_, err := redisConn.Do("INCR", key)
return err
}
func redisSet(key string, val string) error {
redisConn := redisPool.Get()
defer redisConn.Close()
_, err := redisConn.Do("SET", key, val)
return err
}
func redisSetInt(key string, val int64) error {
redisConn := redisPool.Get()
defer redisConn.Close()
// 引数はstringにしておく必要がある
_, err := redisConn.Do("SET", key, strconv.FormatInt(val, 10))
return err
}
func redisGet(key string) string {
redisConn := redisPool.Get()
defer redisConn.Close()
// 値を取り出す時には redis.XXX を使う
str, err := redis.String(redisConn.Do("GET", key))
if err == nil {
// 正常
} else if err == redis.ErrNil {
// nilが返ってきた
return ""
} else {
log.Printf("something wrong: %v\n", str)
// panic()
return ""
}
return str
}
func redisGetInt(key string) int64 {
redisConn := redisPool.Get()
defer redisConn.Close()
res, err := redis.Int64(redisConn.Do("GET", key)) // keyがない時には0を返す
if err == nil {
// 正常
} else if err == redis.ErrNil {
// nilが返ってきた
} else {
log.Printf("something wrong: %v\n", res)
// panic()
return 0
}
return res
}
func getInitialize(c echo.Context) error {
redisConn := redisPool.Get()
defer redisConn.Close()
redisConn.Do("FLUSHALL")
return c.String(204, "")
}
func getObject(c echo.Context) error {
key := c.QueryParam("key")
str := redisGet(key)
return c.String(http.StatusOK, str)
}
func postObject(c echo.Context) error {
key := c.FormValue("key")
val := c.FormValue("val")
if err := redisSet(key, val); err != nil {
return c.String(http.StatusInternalServerError, "InternalServerError")
}
return c.String(http.StatusOK, "Success")
}
func postIncrObject(c echo.Context) error {
key := c.FormValue("key")
if err := redisIncr(key); err != nil {
return c.String(http.StatusInternalServerError, "InternalServerError")
}
return c.String(http.StatusOK, "Success")
}
func getIndex(c echo.Context) error {
return c.Render(http.StatusOK, "index", nil)
}
func main() {
e := echo.New()
redisHost := os.Getenv("REDIS_HOST")
if redisHost == "" {
redisHost = "localhost"
}
// 接続を確立する
redisPool = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", fmt.Sprintf("%v:6379", redisHost))
if err != nil {
log.Fatalln("Can not connect to redis.")
return nil, err
}
return c, err
},
}
e.Renderer = &Renderer{
templates: template.Must(template.ParseGlob("views/*.html")),
}
e.GET("/initialize", getInitialize)
e.GET("/", getIndex)
e.GET("/get", getObject)
e.POST("/set", postObject)
e.POST("/increment", postIncrObject)
e.Start(":5000")
}