-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
70 lines (61 loc) · 1.32 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
package main
import (
"log"
"net/http"
"os"
"strconv"
"github.com/garyburd/redigo/redis"
"github.com/gorilla/mux"
"github.com/jbelmont/containerized-golang-and-vuejs/model"
"github.com/jbelmont/containerized-golang-and-vuejs/routes"
)
const (
localDial = "localhost:6379"
redisURL = "REDIS_URL"
)
func getDial() string {
var dial string
if os.Getenv(redisURL) == "redis:6379" {
dial = os.Getenv(redisURL)
} else {
dial = localDial
}
return dial
}
func getRouter() *mux.Router {
return routes.NewRouter()
}
func initRedis() redis.Conn {
connect := SetHash()
return connect
}
// Connect returns redis connection
func Connect() redis.Conn {
var err error
connect, err := redis.Dial("tcp", getDial())
if err != nil {
panic(err)
}
return connect
}
// SetHash sets a redis hash set
func SetHash() redis.Conn {
connect := Connect()
users := getData()
for key, user := range users {
key := "user:" + strconv.Itoa(key)
_, err := connect.Do("HMSET", key, "id", user.ID, "firstname", user.FirstName, "lastname", user.LastName, "email", user.Email, "gender", user.Gender)
if err != nil {
panic(err)
}
}
return connect
}
func main() {
connect := initRedis()
defer connect.Close()
context := model.MongoSetup()
defer context.Close()
router := getRouter()
log.Fatal(http.ListenAndServe(":3001", router))
}