Skip to content

Commit

Permalink
recipe & website in the main repo
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Oct 20, 2016
1 parent d72b8b8 commit b6547dd
Show file tree
Hide file tree
Showing 103 changed files with 4,938 additions and 21 deletions.
42 changes: 27 additions & 15 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import:
- log
- random
- package: github.com/mattn/go-isatty
- package: github.com/stretchr/testify
subpackages:
- assert
- package: github.com/rsc/letsencrypt
- package: github.com/tylerb/graceful
- package: github.com/valyala/fasttemplate
- package: golang.org/x/net
subpackages:
- context
- package: gopkg.in/tylerb/graceful.v1
- package: github.com/rsc/letsencrypt
- websocket
- package: google.golang.org/appengine
testImport:
- package: github.com/stretchr/testify
subpackages:
- assert
37 changes: 37 additions & 0 deletions recipe/cors/server.go
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"))
}
75 changes: 75 additions & 0 deletions recipe/crud/server.go
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"))
}
2 changes: 2 additions & 0 deletions recipe/embed-resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rice
app.rice-box.go
11 changes: 11 additions & 0 deletions recipe/embed-resources/app/index.html
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>
1 change: 1 addition & 0 deletions recipe/embed-resources/app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert("main.js");
19 changes: 19 additions & 0 deletions recipe/embed-resources/server.go
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"))
}
17 changes: 17 additions & 0 deletions recipe/file-upload/multiple/public/index.html
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>
65 changes: 65 additions & 0 deletions recipe/file-upload/multiple/server.go
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"))
}
17 changes: 17 additions & 0 deletions recipe/file-upload/single/public/index.html
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>
Loading

0 comments on commit b6547dd

Please sign in to comment.