Created
April 5, 2017 14:18
-
-
Save sunshinekitty/9169efd5fc0607ccde4b5b8000b329c2 to your computer and use it in GitHub Desktop.
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 ( | |
"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