/
main.go
93 lines (78 loc) · 1.84 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/BurntSushi/toml"
"github.com/go-redis/redis"
"github.com/labstack/echo"
)
type TomlConfig struct {
RedisDB RedisDB `toml:"redis"`
}
type RedisDB struct {
Connection *redis.Client
DBHost string `toml:"host"`
DBPort int `toml:"port"`
DBPass string `toml:"pass"`
DBName int `toml:"name"`
}
type RedisStats struct {
Stats bool
Message string
}
type RedisVal struct {
Stats bool
Val string
}
func (rdb *RedisDB) open() error {
connection := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", rdb.DBHost, rdb.DBPort),
Password: rdb.DBPass,
DB: rdb.DBName,
})
rdb.Connection = connection
return rdb.Connection.Ping().Err()
}
func (rdb *RedisDB) close() error {
return rdb.Connection.Close()
}
func checkStatsHelper(err error, ifok string) (bool, string) {
if err != nil {
return false, err.Error()
} else {
return true, ifok
}
}
func (rdb *RedisDB) ping() RedisStats {
stats := RedisStats{}
err := rdb.Connection.Ping().Err()
stats.Stats, stats.Message = checkStatsHelper(err, "redis db is running.")
return stats
}
func (rdb *RedisDB) get(c echo.Context) error {
val := RedisVal{}
redisVal, err := rdb.Connection.Get(c.Param("key")).Result()
val.Stats, val.Val = checkStatsHelper(err, redisVal)
jsonStr, _ := json.Marshal(val)
return c.JSONBlob(http.StatusOK, jsonStr)
}
func (rdb *RedisDB) rdbping(c echo.Context) error {
jsonStr, _ := json.Marshal(rdb.ping())
return c.JSONBlob(http.StatusOK, jsonStr)
}
func main() {
var conf TomlConfig
if _, err := toml.DecodeFile("config.toml", &conf); err != nil {
panic(err)
}
rdb := conf.RedisDB
if err := rdb.open(); err != nil {
panic(err)
}
defer rdb.close()
e := echo.New()
e.GET("/", rdb.rdbping)
e.GET("/get/:key", rdb.get)
e.Logger.Fatal(e.Start(":80"))
}