func createUser(c *echo.Context) error { u := new(user) if err := c.Bind(u); err != nil { return err } users[u.ID] = *u return c.JSON(http.StatusCreated, u) }
func upload(c *echo.Context) error { mr, err := c.Request().MultipartReader() if err != nil { return err } // Read form field `name` part, err := mr.NextPart() if err != nil { return err } defer part.Close() b, err := ioutil.ReadAll(part) if err != nil { return err } name := string(b) // Read form field `email` part, err = mr.NextPart() if err != nil { return err } defer part.Close() b, err = ioutil.ReadAll(part) if err != nil { return err } email := string(b) // Read files i := 0 for { part, err := mr.NextPart() if err != nil { if err == io.EOF { break } return err } defer part.Close() file, err := os.Create(part.FileName()) if err != nil { return err } defer file.Close() if _, err := io.Copy(file, part); err != nil { return err } i++ } return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.", name, email, i) }
func createUser(c *echo.Context) error { u := &user{ ID: seq, } if err := c.Bind(u); err != nil { return err } users[u.ID] = u seq++ return c.JSON(http.StatusCreated, u) }
func upload(c *echo.Context) error { req := c.Request() req.ParseMultipartForm(16 << 20) // Max memory 16 MiB // Read form fields name := c.Form("name") email := c.Form("email") // Read files files := req.MultipartForm.File["files"] for _, f := range files { // Source file src, err := f.Open() if err != nil { return err } defer src.Close() // Destination file dst, err := os.Create(f.Filename) if err != nil { return err } defer dst.Close() if _, err = io.Copy(dst, src); err != nil { return err } } return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.", name, email, len(files)) }
func updateUser(c *echo.Context) error { u := new(user) if err := c.Bind(u); err != nil { return err } id, _ := strconv.Atoi(c.Param("id")) users[id].Name = u.Name return c.JSON(http.StatusOK, users[id]) }
// Handler func hello(c *echo.Context) error { return c.String(http.StatusOK, "Hello, World\n") }
func welcome(c *echo.Context) error { return c.Render(http.StatusOK, "welcome", "Joe") }
func deleteUser(c *echo.Context) error { id, _ := strconv.Atoi(c.Param("id")) delete(users, id) return c.NoContent(http.StatusNoContent) }
func getUser(c *echo.Context) error { id, _ := strconv.Atoi(c.Param("id")) return c.JSON(http.StatusOK, users[id]) }
func getUser(c *echo.Context) error { return c.JSON(http.StatusOK, users[c.P(0)]) }
func getUsers(c *echo.Context) error { return c.JSON(http.StatusOK, users) }
func restricted(c *echo.Context) error { return c.String(http.StatusOK, "Access granted with JWT.\n") }
func accessible(c *echo.Context) error { return c.String(http.StatusOK, "No auth required for this route.\n") }