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 Logger(env mango.Env, app mango.App) (status mango.Status, headers mango.Headers, body mango.Body) { startTime := time.Now() r := env.Request() log.Printf("Started %s \"%s\" for %s at %s\n", r.Method, r.RequestURI, r.RemoteAddr, startTime.String()) status, headers, body = app(env) execution := time.Now().Sub(startTime) / time.Millisecond log.Printf("Completed %d in %dms\n\n", status, execution) return }
func Authenticated(env mango.Env, app mango.App) (status mango.Status, headers mango.Headers, body mango.Body) { if env.Request().FormValue("Password") != "nopassword" { body = "Not authorized." status = 403 return } status, headers, body = app(env) return }
func Logger(env mango.Env, app mango.App) (status mango.Status, headers mango.Headers, body mango.Body) { startTime := time.Now() r := env.Request() if err := r.ParseMultipartForm(32 << 20); err != nil { panic(err) } log.Printf("Started %s \"%s\" for %s at %s\n", r.Method, r.RequestURI, r.RemoteAddr, startTime.String()) log.Printf("Form %v; Accept: %+v \n", r.Form, r.Header["Accept"]) status, headers, body = app(env) execution := time.Now().Sub(startTime) / time.Millisecond log.Printf("Completed %d in %dms\n\n", status, execution) return }
// 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 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 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") }