-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
52 lines (45 loc) · 1.02 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
package main
import (
"github.com/hoisie/redis"
"net/http"
"runtime"
)
var client *redis.Client
func init() {
client = &redis.Client{
Addr: "127.0.0.1:6379",
Db: 0, // default db is 0
MaxPoolSize: 10000,
}
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
http.HandleFunc("/put", PutHandler)
http.HandleFunc("/get", GetHandler)
http.HandleFunc("/", HomeHandler)
if err := http.ListenAndServe(":1988", nil); err != nil {
panic("ListenAndServe: " + err.Error())
}
}
func HomeHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("welcome to use GoMq!"))
}
func PutHandler(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
if err := client.Lpush("queue", []byte(req.FormValue("data"))); err != nil {
w.Write([]byte("error"))
return
}
w.Write([]byte("ok"))
return
}
func GetHandler(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
value, err := client.Rpop("queue")
if err == nil {
w.Write([]byte(value))
return
}
w.Write([]byte("end"))
return
}