Skip to content

Instantly share code, notes, and snippets.

@hashbender
Last active April 20, 2017 20:35
Show Gist options
  • Save hashbender/462abecfe1ca903b0978a9444cc9342d to your computer and use it in GitHub Desktop.
Save hashbender/462abecfe1ca903b0978a9444cc9342d to your computer and use it in GitHub Desktop.
Implements ContextedHandlerFunc
//HelloWorldHandler just prints a Hello World on a GET request
func HelloWorldHandler(c *AppContext, w http.ResponseWriter, req *http.Request) (int, error) {
//So in this handler we now have the context provided
fmt.Fprint(w, "Hello World")
return http.StatusOK, nil
}
func main() {
context := AppContext{Db: initDb(), Redis: initRedis()}
//Here we instantiate a ContextedHandler, which staisfies the ServeHTTP
//interface and can thus be used as a Handler on a router instance
contextedHandler := &ContextedHandler{&context, HelloWorldHandler}
router := mux.NewRouter()
router.Methods("GET").Path("/hello").Name("HelloWorldHandler").Handler(contextedHandler)
http.ListenAndServe(":5000", router)
}
//TODO do your DB initialization here
func initDb() *sql.DB {
return nil
}
//TODO do your redis pool initialization here
func initRedis() *redis.Pool {
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment