// This function shows how to access the custom DB handler can be accessed from globals func MySQLSample(ctx *webgo.Context, w http.ResponseWriter, r *http.Request) { route := ctx.Get("routeHandler").(*webgo.Route) g := route.G mh := g.App["mysql"].(MySQLHandler) dbHandler := mh.Db webgo.Log.Println("Talk to MySQL using, dbHandler:", dbHandler) webgo.R200(w, "MySQL Sample") }
/* This function shows how to access URL parameter `name`, and find all records in the database with the name and return them. */ func DbSample(ctx *webgo.Context, w http.ResponseWriter, r *http.Request) { // Access the Globals from context route := ctx.Get("routeHandler").(*webgo.Route) g := route.G // Get all the URL parameters in a map of string of string, from context params := ctx.Get("params").(map[string]string) // Query string and request body can be accessed directly from `r http.Request` //g.Db.Get(<database name>, <collection name>, <condition>, <struct to parse result into this>) results, err := g.Db.Get(g.Cfg.DBC.Name, "users", bson.M{"name": params["name"]}, nil) if err != nil { webgo.Log.Println(err) webgo.R500(w, "Sorry, an unknown error occurred") } webgo.R200(w, results) }