Last active
November 1, 2017 21:15
-
-
Save lkiesow/0b85affbe2c6e5e1dfa00c14ef1d0baf to your computer and use it in GitHub Desktop.
Minimal golang backend for pullrequests.opencast.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"github.com/codegangsta/martini" | |
"github.com/garyburd/redigo/redis" | |
"github.com/martini-contrib/render" | |
) | |
var ( | |
redisAddress = flag.String("redis-address", ":6379", "Address to the Redis server") | |
maxConnections = flag.Int("max-connections", 10, "Max connections to Redis") | |
) | |
func main() { | |
martini.Env = martini.Prod | |
flag.Parse() | |
redisPool := redis.NewPool(func() (redis.Conn, error) { | |
c, err := redis.Dial("tcp", *redisAddress) | |
if err != nil { | |
return nil, err | |
} | |
return c, err | |
}, *maxConnections) | |
defer redisPool.Close() | |
m := martini.Classic() | |
m.Map(redisPool) | |
m.Use(render.Renderer()) | |
m.Get("/", func() string { | |
return "Hello from Martini!" | |
}) | |
m.Get("/get/:key", func(pool *redis.Pool, params martini.Params) string { | |
//key := params["key"] | |
c := pool.Get() | |
defer c.Close() | |
// Get keys | |
keys, err := redis.Values(c.Do("KEYS", "*pr_*")) | |
if err != nil { | |
return fmt.Sprintf("Could not get keys: %s", err) | |
} | |
last := len(keys) - 1 | |
result := "[" | |
for index, key := range keys { | |
value, err := redis.String(c.Do("GET", key)) | |
if err != nil { | |
return fmt.Sprintf("Could not GET %s", key) | |
} else { | |
result += value | |
if index < last { | |
result += "," | |
} | |
} | |
} | |
return result + "]" | |
}) | |
m.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment