redis-queue-golang-server
v0.0.1
Published
```bash sudo apt install redis sudo gedit /etc/redis/redis.conf # edit line # ... # from bind 127.0.0.1 ::1 # to bind 0.0.0.0 # ...
Downloads
2
Readme
Redis Queue Golang Server
sudo apt install redis
sudo gedit /etc/redis/redis.conf
# edit line
# ...
# from
bind 127.0.0.1 ::1
# to
bind 0.0.0.0
# ...
sudo service redis restart
sudo service redis-server restart
# OR
sudo systemctl restart redis.service
sudo systemctl restart redis-server.service
go file: main.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
"github.com/go-redis/redis"
)
var rdb = redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379", // use default Addr
Password: "", // no password set
DB: 0, // use default DB
})
type queue struct {
Count int64 `json:"count"`
Name string `json:"name"`
}
var d = `[{"count":2,"name":"queueTest01"},{"count":2,"name":"queueTest02"}]`
func main() {
var listQueue []queue
_ = json.Unmarshal([]byte(d), &listQueue)
for _, q := range listQueue {
go listenQueue(q.Count, q.Name)
}
http.HandleFunc("/addQueue", addQueue)
http.HandleFunc("/post", post)
_ = http.ListenAndServe(":8080", nil)
}
func post(writer http.ResponseWriter, request *http.Request) {
fmt.Println("RequestURI", request.RequestURI)
fmt.Println("foo", request.FormValue("foo"))
fmt.Println("bar", request.FormValue("bar"))
fmt.Println(request.Header)
}
type postData struct {
Api string `json:"api"`
Data string `json:"data"`
}
func addQueue(writer http.ResponseWriter, request *http.Request) {
var p postData
// p.Api = request.FormValue("api")
// p.Data = request.FormValue("data")
// var queueName = request.FormValue("name")
p.Api = `http://localhost:8080/post`
p.Data = `{"foo":"123","bar":"456"}`
var queueName = `queueTest01`
b, _ := json.Marshal(p)
_, _ = rdb.RPush(queueName, string(b)).Result()
}
func postRequest(requestUrl, contentType string, form url.Values) ([]byte, error) {
//form = url.Values{"key":{"value"}, "name":{"value"}}
//ContentType = "application/x-www-form-urlencoded"
buffer := bytes.NewBufferString(form.Encode())
rsp, err := http.Post(requestUrl, contentType, buffer)
if err != nil {
log.Printf("http.Post False! %v", err)
} else {
defer rsp.Body.Close()
}
return ioutil.ReadAll(rsp.Body)
}
func rawRequest(RequestUrl, Method string, CollectionHeader map[string]string, Json []byte) (StatusCode int, result []byte, error error) {
req, err := http.NewRequest(Method, RequestUrl, bytes.NewBuffer(Json))
if err == nil {
for key, value := range CollectionHeader {
req.Header.Set(key, value)
}
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("http.Post False! %v", err)
error = err
return
} else {
defer resp.Body.Close()
}
StatusCode = resp.StatusCode
result, error = ioutil.ReadAll(resp.Body)
return
}
func listenQueue(n int64, name string) {
for {
time.Sleep(1 * time.Second)
list, err := rdb.LRange(name, 0, n-1).Result()
if err == nil {
for _, s := range list {
queueReqest(s)
}
rdb.LTrim(name, int64(len(list)), -1)
}
}
}
func queueReqest(s string) {
var p postData
_ = json.Unmarshal([]byte(s), &p)
var d = make(map[string]string)
_ = json.Unmarshal([]byte(p.Data), &d)
var form = url.Values{}
for k, v := range d {
form[k] = []string{v}
}
var ContentType = "application/x-www-form-urlencoded"
_, _ = postRequest(p.Api, ContentType, form)
}