1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

updated recipes

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-10-20 16:57:31 -07:00
parent edd558a5ca
commit 093e5c9ed6
21 changed files with 182 additions and 57 deletions

20
echo.go
View File

@ -30,7 +30,9 @@ Example:
e.GET("/", hello)
// Start server
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}
Learn more at https://echo.labstack.com
@ -53,8 +55,6 @@ import (
"github.com/rsc/letsencrypt"
"golang.org/x/net/websocket"
"github.com/labstack/gommon/color"
glog "github.com/labstack/gommon/log"
"github.com/tylerb/graceful"
@ -396,20 +396,6 @@ func (e *Echo) File(path, file string) {
})
}
// WebSocket registers a new WebSocket route for a path with matching handler in
// the router with optional route-level middleware.
func (e *Echo) WebSocket(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.GET(path, func(c Context) (err error) {
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
c.SetWebSocket(ws)
c.Response().Status = http.StatusSwitchingProtocols
err = h(c)
}).ServeHTTP(c.Response(), c.Request())
return
}, m...)
}
func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
name := handlerName(handler)
e.router.Add(method, path, func(c Context) error {

View File

@ -2,14 +2,11 @@ package echo
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"golang.org/x/net/websocket"
"reflect"
"strings"
@ -250,27 +247,6 @@ func TestEchoMatch(t *testing.T) { // JFC
})
}
func TestEchoWebSocket(t *testing.T) {
e := New()
e.WebSocket("/ws", func(c Context) error {
c.WebSocket().Write([]byte("test"))
return nil
})
srv := httptest.NewServer(e)
defer srv.Close()
addr := srv.Listener.Addr().String()
origin := "http://localhost"
url := fmt.Sprintf("ws://%s/ws", addr)
ws, err := websocket.Dial(url, "", origin)
if assert.NoError(t, err) {
ws.Write([]byte("test\n"))
defer ws.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(ws)
assert.Equal(t, "test", buf.String())
}
}
func TestEchoURL(t *testing.T) {
e := New()
static := func(Context) error { return nil }

6
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: 68fa89887f232f6f139552d50300f5fe9f958da390045b9b99d4f7d0201f42f7
updated: 2016-10-20T11:50:22.171643207-07:00
hash: b592629b56c43e0f6fcef629a3e46106b07431120d42ed45952d841655407e3e
updated: 2016-10-20T16:35:55.384756891-07:00
imports:
- name: github.com/daaku/go.zipexe
version: a5fe2436ffcb3236e175e5149162b41cd28bd27d
@ -13,6 +13,8 @@ imports:
version: 98fa357170587e470c5f27d3c3ea0947b71eb455
subpackages:
- proto
- name: github.com/gorilla/websocket
version: 460e0a996e9004dce40c3697e5ed4d7bc5a69b93
- name: github.com/kardianos/osext
version: c2c54e542fb797ad986b31721e1baedf214ca413
- name: github.com/labstack/gommon

View File

@ -16,6 +16,7 @@ import:
subpackages:
- websocket
- package: google.golang.org/appengine
- package: github.com/gorilla/websocket
testImport:
- package: github.com/stretchr/testify
subpackages:

View File

@ -33,5 +33,8 @@ func main() {
}))
e.GET("/api/users", getUsers)
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -71,5 +71,7 @@ func main() {
e.DELETE("/users/:id", deleteUser)
// Start server
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -17,5 +17,7 @@ func main() {
// servers other static files
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
e.Logger.Fatal(e.Start(":3000"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -61,5 +61,7 @@ func main() {
e.POST("/upload", upload)
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -55,5 +55,7 @@ func main() {
e.POST("/upload", upload)
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -20,5 +20,7 @@ func createMux() *echo.Echo {
}
func main() {
e.Logger.Fatal(e.Start(":8080"))
if err := e.Start(":8080"); err != nil {
panic(err)
}
}

View File

@ -21,5 +21,7 @@ func main() {
})
// Start server
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -38,5 +38,7 @@ func main() {
e := echo.New()
e.GET("/request", request)
e.GET("/stream", stream)
e.Logger.Fatal(e.StartTLS(":1323", "cert.pem", "key.pem"))
if err := e.StartTLS(":1323", "cert.pem", "key.pem"); err != nil {
panic(err)
}
}

View File

@ -30,5 +30,7 @@ func main() {
})
// Start server
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -82,5 +82,7 @@ func main() {
r.Use(middleware.JWTWithConfig(config))
r.GET("", restricted)
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -65,5 +65,7 @@ func main() {
r.Use(middleware.JWT([]byte("secret")))
r.GET("", restricted)
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -78,5 +78,7 @@ func main() {
})
// Start server
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -41,5 +41,7 @@ func main() {
}
return nil
})
e.Logger.Fatal((e.Start(":1323")))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -74,5 +74,7 @@ func main() {
return
})
e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -0,0 +1,49 @@
package main
import (
"fmt"
"log"
"github.com/labstack/echo"
"github.com/gorilla/websocket"
"github.com/labstack/echo/middleware"
)
var (
upgrader = websocket.Upgrader{}
)
func hello(c echo.Context) error {
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
return err
}
defer ws.Close()
for {
// Write
err := ws.WriteMessage(websocket.TextMessage, []byte("Hello, Client!"))
if err != nil {
log.Fatal(err)
}
// Read
_, msg, err := ws.ReadMessage()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", msg)
}
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.Static("../public"))
e.GET("/ws", hello)
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -0,0 +1,43 @@
package main
import (
"fmt"
"log"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"golang.org/x/net/websocket"
)
func hello(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
for {
// Write
err := websocket.Message.Send(ws, "Hello, Client!")
if err != nil {
log.Fatal(err)
}
// Read
msg := ""
err = websocket.Message.Receive(ws, &msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", msg)
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.Static("../public"))
e.GET("/ws", hello)
if err := e.Start(":1323"); err != nil {
panic(err)
}
}

View File

@ -0,0 +1,39 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebSocket</title>
</head>
<body>
<p id="output"></p>
<script>
var loc = window.location;
var uri = 'ws:';
if (loc.protocol === 'https:') {
uri = 'wss:';
}
uri += '//' + loc.host;
uri += loc.pathname + 'ws';
ws = new WebSocket(uri)
ws.onopen = function() {
console.log('Connected')
}
ws.onmessage = function(evt) {
var out = document.getElementById('output');
out.innerHTML += evt.data + '<br>';
}
setInterval(function() {
ws.send('Hello, Server!');
}, 1000);
</script>
</body>
</html>