Skip to content

Instantly share code, notes, and snippets.

@sunshinekitty
Created April 5, 2017 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sunshinekitty/9169efd5fc0607ccde4b5b8000b329c2 to your computer and use it in GitHub Desktop.
Save sunshinekitty/9169efd5fc0607ccde4b5b8000b329c2 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"net/http"
"goji.io"
"goji.io/pat"
"github.com/Sirupsen/logrus"
"github.com/paddycarey/gophy"
)
func fart(w http.ResponseWriter, r *http.Request) {
co := &gophy.ClientOptions{}
client := gophy.NewClient(co)
gifs, _, err := client.SearchGifs("farts", "pg", 100, 0)
if err != nil {
panic(err)
}
use := rand.Intn(len(gifs))
w.Write([]byte(fmt.Sprintf("<html><img src=\"https://i.giphy.com/%s.gif\" /></html>", gifs[use].Id)))
}
func main() {
mux := goji.NewMux()
mux.Use(logging)
mux.HandleFunc(pat.Get("/fart"), fart)
http.ListenAndServe("localhost:9555", mux)
}
func logging(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
logrus.WithFields(logrus.Fields{
"method": r.Method,
"path": r.URL.Path,
}).Info("Finished request")
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment