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 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)) }
// Handler func hello(c *echo.Context) error { return c.String(http.StatusOK, "Hello, World!\n") }
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 welcome(c *echo.Context) error { return c.Render(http.StatusOK, "welcome", "Joe") }