func editEvent(env mango.Env) (mango.Status, mango.Headers, mango.Body) { db := env["AssetTracker.db"].(*sql.DB) req := env.Request() switch req.Method { case "GET": break case "POST": event := models.Event{} req.ParseForm() decoder := schema.NewDecoder() err := decoder.Decode(&event, req.Form) if err != nil { log.Println(err) } log.Printf("Received Event %#v", event) _, err = crud.Insert(db, "events", "event_id", event) if err != nil { log.Println(err) return internalServerError() } return 201, mango.Headers{}, mango.Body("Event saved") } return 404, mango.Headers{}, mango.Body("404") }
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) { r := env.Request() env.Logger().Println("Got a ", r.Method, " request for ", r.RequestURI) name := r.URL.Query().Get("name") body := fmt.Sprintf("Hello %s!", name) return 200, mango.Headers{}, mango.Body(body) }
func Producer(env mango.Env) (status mango.Status, headers mango.Headers, body mango.Body) { env.Logger().Println("Got a", env.Request().Method, "request for", env.Request().RequestURI) if strings.HasPrefix(env.Request().RequestURI, "/producer.json") { status = 200 headers = mango.Headers{"Content-Type": []string{"application/json;charset=utf-8"}} json_data := make(map[string]interface{}) json_data["test"] = "NO" json_data["date"] = "2013-08-16T15:31:20+10:00" json_data["count"] = 1000 json_string, _ := json.Marshal(json_data) body = mango.Body(json_string) } else { status = 404 body = mango.Body("Invalid path") } return }
func listUsers(env mango.Env) (mango.Status, mango.Headers, mango.Body) { db := env["AssetTracker.db"].(*sql.DB) users, err := models.GetUsers(db, 0) if err != nil { log.Println(err) return internalServerError() } return 200, mango.Headers{}, mango.Body(fmt.Sprintf("%#v", users)) }
func displayAsset(env mango.Env) (mango.Status, mango.Headers, mango.Body) { db := env["AssetTracker.db"].(*sql.DB) id, err := strconv.ParseInt(env["Routing.matches"].([]string)[1], 10, 64) events, err := models.GetEventsByAssetId(db, 0, id) if err != nil { log.Println(err) return internalServerError() } return 200, mango.Headers{}, mango.Body(fmt.Sprintf("%#v", events)) }
// Handler function to accept the submitted form post with the SSN func submitHandler(env mango.Env) (mango.Status, mango.Headers, mango.Body) { log.Println("Submit GetUserData Request") cr := &myStartup.GetUserDataRequest{YourInputValue: env.Request().FormValue("YourInputValue")} resp, err := submitGetUserDataRequest(cr) if err != nil { log.Println(err.String()) } log.Println(resp) buffer := &bytes.Buffer{} respTmpl.Execute(buffer, map[string]interface{}{ "resp": resp, }) return 200, mango.Headers{}, mango.Body(buffer.String()) }
func listAssets(env mango.Env) (mango.Status, mango.Headers, mango.Body) { db := env["AssetTracker.db"].(*sql.DB) tmpl := env["AssetTracker.tpl"].(*template.Template) assets, err := models.GetAssets(db, 0) if err != nil { log.Println(err) return internalServerError() } args := map[string]interface{}{ "Title": "Asset List", "Assets": assets, } var buf bytes.Buffer if err = tmpl.ExecuteTemplate(&buf, "listAssets.tpl", args); err != nil { log.Println(err) return internalServerError() } return 200, mango.Headers{}, mango.Body(buf.String()) }
// Handler function to display the social form func homeHandler(env mango.Env) (mango.Status, mango.Headers, mango.Body) { buffer := &bytes.Buffer{} homeTmpl.Execute(buffer, nil) return 200, mango.Headers{}, mango.Body(buffer.String()) }
func bye(env mango.Env) (mango.Status, mango.Headers, mango.Body) { return 200, mango.Headers{}, mango.Body("Bye Bye!") }
func internalServerError() (mango.Status, mango.Headers, mango.Body) { return 500, mango.Headers{}, mango.Body("500 ISE") }
func hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) { return 200, mango.Headers{}, mango.Body("Hello World!") }
func login(env mango.Env) (mango.Status, mango.Headers, mango.Body) { return 404, mango.Headers{}, mango.Body("404") }
func defaultHandler(env mango.Env) (mango.Status, mango.Headers, mango.Body) { return 404, mango.Headers{}, mango.Body("404") }
func editAsset(env mango.Env) (mango.Status, mango.Headers, mango.Body) { db := env["AssetTracker.db"].(*sql.DB) tmpl := env["AssetTracker.tpl"].(*template.Template) req := env.Request() switch req.Method { case "GET": var asset models.Asset uriParams := env["Routing.matches"].([]string) if len(uriParams) > 1 { id, err := strconv.ParseInt(uriParams[1], 10, 64) if err != nil { log.Println(err) return defaultHandler(env) } asset, err = models.GetAssetById(db, id) if err != nil { log.Println(err) return internalServerError() } } else { asset = models.Asset{} } args := map[string]interface{}{ "Title": "Edit Asset", "Asset": asset, } var buf bytes.Buffer if err := tmpl.ExecuteTemplate(&buf, "editAsset.tpl", args); err != nil { log.Println(err) return internalServerError() } return 200, mango.Headers{}, mango.Body(buf.String()) case "POST": asset := models.Asset{} req.ParseForm() decoder := schema.NewDecoder() err := decoder.Decode(&asset, req.Form) if err != nil { log.Println(err) } log.Printf("Received Asset %#v", asset) // Update or create the new asset if asset.Id > 0 { err = crud.Update(db, "assets", "asset_id", asset) } else { _, err = crud.Insert(db, "assets", "asset_id", asset) } if err != nil { log.Println(err) return internalServerError() } return 201, mango.Headers{}, mango.Body("Asset saved") } return 404, mango.Headers{}, mango.Body("404") }
// Our handler for /goodbye func Goodbye(env mango.Env) (mango.Status, mango.Headers, mango.Body) { return 200, mango.Headers{}, mango.Body("Goodbye World!") }
func routeNotFound(env mango.Env) (mango.Status, mango.Headers, mango.Body) { return 404, mango.Headers{}, mango.Body("You probably got lost :(") }
// Our handler for /goodbye func Goodbye(env mango.Env) (mango.Status, mango.Headers, mango.Body) { return 200, mango.Headers{}, mango.Body("<b>Goodbye Esther!</b>") }
// Our default handler func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) { return 200, mango.Headers{}, mango.Body("<b>Hi Esther!</b>") }