Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package main

import (
"fmt"
"io/ioutil"
"net/http"

"encoding/json"
)

func initHandler() {
http.HandleFunc("/ping", handlePing)
http.HandleFunc("/get_prize", handleGetPrize)
http.HandleFunc("/pool", handleGetPrize)
// TODO: Adding more APIs

fmt.Println("serving in 8181 ")
Expand All @@ -16,3 +21,35 @@ func initHandler() {
func handlePing(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
}

func handleGetPrize(w http.ResponseWriter, r *http.Request) {
// userID := r.FormValue("user_id")

// get redis by user_id
ovo, err := redisGetRand()
if err != nil {
fmt.Println(err)
}

resp, err := json.Marshal(ovo)
if err != nil {
fmt.Println(err)
}

w.Write(resp)
}

func handlePostPrize(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {

}

ovos := []KuponOVO{}

err = json.Unmarshal([]byte(body), ovos)

for _, ovo := range ovos {
redisAddKey(ovo)
}
}
65 changes: 62 additions & 3 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"time"

redigo "github.com/garyburd/redigo/redis"
Expand All @@ -13,6 +16,14 @@ var (
redisConn *redigo.Pool
)

const prizePoolKey = "prizepool"

type KuponOVO struct {
Name string `json:"name"`
Value int64 `json:"value"`
Percentage int64 `json:"percentage"`
}

func initRedis() {
redisConn = &redigo.Pool{
MaxIdle: 100,
Expand All @@ -23,13 +34,11 @@ func initRedis() {
return redigo.Dial("tcp", redisAddress)
},
}

// do PING, if failed then Fatal
if err := pingRedis(); err != nil {
log.Fatal(err)
}
}

func pingRedis() error {
c := redisConn.Get()
defer c.Close()
Expand All @@ -43,7 +52,57 @@ func pingRedis() error {
func sampleDoCommand(command string) {
c := redisConn.Get()
defer c.Close()

reply, err := c.Do(command)
fmt.Println(reply, err)
}
func GetConn() (redigo.Conn, error) {
conn, err := redisConn.Dial()
if err != nil {
return nil, err
}
return conn, err
}
func redisAddKey(param KuponOVO) error {
conn, err := GetConn()
if err != nil {
return err
}
keyMember := normalizeParamName(param.Name)
conn.Send("MULTI")
conn.Send("ZADD", prizePoolKey, param.Percentage/100, keyMember)
conn.Send("HMSET", keyMember, getRedisHMSETKeys(param))
_, err = conn.Do("EXEC")
if err != nil {
return err
}
return nil
}
func normalizeParamName(key string) string {
return strings.Replace(key, " ", "_", -1)
}
func getRedisHMSETKeys(param KuponOVO) []string {
return []string{
"name",
param.Name,
"value",
strconv.FormatInt(param.Value, 10),
"percentage",
strconv.FormatInt(param.Percentage, 10),
}
}
func redisGetRand() (KuponOVO, error) {
conn, err := GetConn()
if err != nil {
return KuponOVO{}, err
}

randNum := rand.Intn(100)
key, err := conn.Do("ZRANGEBYSCORE", prizePoolKey, randNum, "+inf LIMIT 0 1")
if err != nil {
return KuponOVO{}, err
}

result, err := conn.Do("HMGET", key)
kuponOVO := result.(KuponOVO)
return kuponOVO, nil
}