This file contains hidden or 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
//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()} |
This file contains hidden or 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
//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 { |
This file contains hidden or 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 ( | |
"database/sql" | |
) | |
var ( | |
db *sql.DB | |
) | |
func main() { |
NewerOlder