/
redis_demo.go
101 lines (71 loc) · 1.74 KB
/
redis_demo.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
//Redis Handler
package main
import (
"encoding/json"
"fmt"
"net"
"os"
"bytes"
"github.com/xuyu/goredis"
)
const (
CONN_HOST = ""
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
type Response struct {
ReqId int `json:id`
Payload string `json:payload`
}
func main() {
listen, err := net.Listen(CONN_TYPE, ":"+CONN_PORT)
if err != nil {
fmt.Println("Error in Listening")
os.Exit(1)
}
// close listner when server down
defer listen.Close()
fmt.Println("Listening on " + CONN_PORT)
for {
conn, error := listen.Accept()
if error != nil {
fmt.Println("Error Acception failed")
os.Exit(1)
}
// create new routine to handle connection
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
buf := make([]byte, 1024)
reqLen, err := conn.Read(buf)
n := bytes.Index(buf, []byte{0})
if err != nil {
fmt.Println("Error reading:", err)
}
fmt.Println(reqLen)
GetFromJson(buf, reqLen)
message := string(buf[:n-1])
conn.Write([]byte(message))
conn.Close()
}
func storeinRedis(res Response){
client, _ := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"})
res2B, _ := json.Marshal(res)
fmt.Println("res2B", string(res2B))
reply, _ := client.LPush("Rqueue", string(res2B))
fmt.Println("Reply: ", reply)
/*
fmt.Println("Stored to Redis Hash...")
fmt.Println(err)
fmt.Println(err2)
fmt.Println(reply) */
}
func GetFromJson(buf[] byte,n int) {
var res Response
err := json.Unmarshal(buf[:n-1], &res)
fmt.Println(err)
fmt.Println("ReqId: ",res.ReqId)
fmt.Println("Payload: ", res.Payload)
storeinRedis(res)
}