Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "log"
- "strconv"
- "net/http"
- "github.com/julienschmidt/httprouter"
- "github.com/kr/beanstalk"
- )
- var conn, _ = beanstalk.Dial("tcp", "127.0.0.1:11300")
- func ssi(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
- var err error
- var i, count int
- var id uint64
- var body []byte
- bot := ps.ByName("bot")
- if count, err = strconv.Atoi(string(ps.ByName("count"))); err != nil {
- count = 1
- }
- tubeSet := beanstalk.NewTubeSet(conn, bot)
- response := ""
- for i = 0; i < count; i++ {
- if id, body, err = tubeSet.Reserve(0); err != nil {
- break
- }
- response += string(body) + " "
- conn.Delete(id)
- }
- if i == 0 {
- response = "<!-- empty -->"
- }
- fmt.Fprintf(w, response)
- log.Print(bot, ":", i, "/", count, " [", r.RemoteAddr, "]")
- }
- func main() {
- log.Print("Start server")
- router := httprouter.New()
- router.GET("/:bot", ssi)
- router.GET("/:bot/:count", ssi)
- log.Fatal(http.ListenAndServe(":9080", router))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement