forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vishal Rana <[email protected]>
- Loading branch information
Showing
103 changed files
with
4,938 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labstack/echo" | ||
"github.com/labstack/echo/middleware" | ||
) | ||
|
||
var ( | ||
users = []string{"Joe", "Veer", "Zion"} | ||
) | ||
|
||
func getUsers(c echo.Context) error { | ||
return c.JSON(http.StatusOK, users) | ||
} | ||
|
||
func main() { | ||
e := echo.New() | ||
e.Use(middleware.Logger()) | ||
e.Use(middleware.Recover()) | ||
|
||
// CORS default | ||
// Allows requests from any origin wth GET, HEAD, PUT, POST or DELETE method. | ||
// e.Use(middleware.CORS()) | ||
|
||
// CORS restricted | ||
// Allows requests from any `https://labstack.com` or `https://labstack.net` origin | ||
// wth GET, PUT, POST or DELETE method. | ||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ | ||
AllowOrigins: []string{"https://labstack.com", "https://labstack.net"}, | ||
AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE}, | ||
})) | ||
|
||
e.GET("/api/users", getUsers) | ||
panic(e.Start(":1323")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/labstack/echo" | ||
"github.com/labstack/echo/middleware" | ||
) | ||
|
||
type ( | ||
user struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
) | ||
|
||
var ( | ||
users = map[int]*user{} | ||
seq = 1 | ||
) | ||
|
||
//---------- | ||
// Handlers | ||
//---------- | ||
|
||
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 getUser(c echo.Context) error { | ||
id, _ := strconv.Atoi(c.Param("id")) | ||
return c.JSON(http.StatusOK, users[id]) | ||
} | ||
|
||
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]) | ||
} | ||
|
||
func deleteUser(c echo.Context) error { | ||
id, _ := strconv.Atoi(c.Param("id")) | ||
delete(users, id) | ||
return c.NoContent(http.StatusNoContent) | ||
} | ||
|
||
func main() { | ||
e := echo.New() | ||
|
||
// Middleware | ||
e.Use(middleware.Logger()) | ||
e.Use(middleware.Recover()) | ||
|
||
// Routes | ||
e.POST("/users", createUser) | ||
e.GET("/users/:id", getUser) | ||
e.PUT("/users/:id", updateUser) | ||
e.DELETE("/users/:id", deleteUser) | ||
|
||
// Start server | ||
panic(e.Start(":1323")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rice | ||
app.rice-box.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>go.rice Example</title> | ||
<script src="/static/main.js" charset="utf-8"></script> | ||
</head> | ||
<body> | ||
<h1>go.rice Example</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alert("main.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labstack/echo" | ||
) | ||
|
||
func main() { | ||
e := echo.New() | ||
// the file server for rice. "app" is the folder where the files come from. | ||
assetHandler := http.FileServer(rice.MustFindBox("app").HTTPBox()) | ||
// serves the index.html from rice | ||
e.GET("/", standard.WrapHandler(assetHandler)) | ||
|
||
// servers other static files | ||
e.GET("/static/*", standard.WrapHandler(http.StripPrefix("/static/", assetHandler))) | ||
panic(e.Start(":3000")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Multiple file upload</title> | ||
</head> | ||
<body> | ||
<h1>Upload multiple files with fields</h1> | ||
|
||
<form action="/upload" method="post" enctype="multipart/form-data"> | ||
Name: <input type="text" name="name"><br> | ||
Email: <input type="email" name="email"><br> | ||
Files: <input type="file" name="files" multiple><br><br> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"net/http" | ||
|
||
"github.com/labstack/echo" | ||
"github.com/labstack/echo/middleware" | ||
) | ||
|
||
func upload(c echo.Context) error { | ||
// Read form fields | ||
name := c.FormValue("name") | ||
email := c.FormValue("email") | ||
|
||
//------------ | ||
// Read files | ||
//------------ | ||
|
||
// Multipart form | ||
form, err := c.MultipartForm() | ||
if err != nil { | ||
return err | ||
} | ||
files := form.File["files"] | ||
|
||
for _, file := range files { | ||
// Source | ||
src, err := file.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer src.Close() | ||
|
||
// Destination | ||
dst, err := os.Create(file.Filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer dst.Close() | ||
|
||
// Copy | ||
if _, err = io.Copy(dst, src); err != nil { | ||
return err | ||
} | ||
|
||
} | ||
|
||
return c.HTML(http.StatusOK, fmt.Sprintf("<p>Uploaded successfully %d files with fields name=%s and email=%s.</p>", len(files), name, email)) | ||
} | ||
|
||
func main() { | ||
e := echo.New() | ||
|
||
e.Use(middleware.Logger()) | ||
e.Use(middleware.Recover()) | ||
e.Use(middleware.Static("public")) | ||
|
||
e.POST("/upload", upload) | ||
|
||
panic(e.Start(":1323")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Single file upload</title> | ||
</head> | ||
<body> | ||
<h1>Upload single file with fields</h1> | ||
|
||
<form action="/upload" method="post" enctype="multipart/form-data"> | ||
Name: <input type="text" name="name"><br> | ||
Email: <input type="email" name="email"><br> | ||
Files: <input type="file" name="file"><br><br> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
</body> | ||
</html> |
Oops, something went wrong.