Skip to content

Instantly share code, notes, and snippets.

View hashbender's full-sized avatar
👷‍♂️
Buidling

Nick Hansen hashbender

👷‍♂️
Buidling
View GitHub Profile
@hashbender
hashbender / contexted_impl.go
Last active April 20, 2017 20:35
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()}
//ContextedHandler is a wrapper to provide AppContext to our Handlers
type ContextedHandler struct {
*AppContext
//ContextedHandlerFunc is the interface which our Handlers will implement
ContextedHandlerFunc func(*AppContext, http.ResponseWriter, *http.Request) (int, error)
}
//AppContext provides the app context to handlers. This *cannot* contain request-specific keys like
//sessionId or similar. It is shared across requests.
type AppContext struct {
package main
import (
"database/sql"
)
var (
db *sql.DB
)
func main() {