mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Updated recipes examples and docs.
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
99aa92eb06
commit
4686f882d6
21
echo.go
21
echo.go
@ -389,7 +389,7 @@ func (e *Echo) URI(h Handler, params ...interface{}) string {
|
||||
return uri.String()
|
||||
}
|
||||
|
||||
// URL is an alias for URI.
|
||||
// URL is an alias for `URI` function.
|
||||
func (e *Echo) URL(h Handler, params ...interface{}) string {
|
||||
return e.URI(h, params...)
|
||||
}
|
||||
@ -399,6 +399,7 @@ func (e *Echo) Routes() []Route {
|
||||
return e.router.routes
|
||||
}
|
||||
|
||||
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
|
||||
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
c := e.pool.Get().(*Context)
|
||||
h, echo := e.router.Find(r.Method, r.URL.Path, c)
|
||||
@ -423,7 +424,7 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
e.pool.Put(c)
|
||||
}
|
||||
|
||||
// Server returns the internal *http.Server
|
||||
// Server returns the internal *http.Server.
|
||||
func (e *Echo) Server(addr string) *http.Server {
|
||||
s := &http.Server{Addr: addr}
|
||||
s.Handler = e
|
||||
@ -446,13 +447,13 @@ func (e *Echo) RunTLS(addr, certFile, keyFile string) {
|
||||
}
|
||||
|
||||
// RunServer runs a custom server.
|
||||
func (e *Echo) RunServer(srv *http.Server) {
|
||||
e.run(srv)
|
||||
func (e *Echo) RunServer(s *http.Server) {
|
||||
e.run(s)
|
||||
}
|
||||
|
||||
// RunTLSServer runs a custom server with TLS configuration.
|
||||
func (e *Echo) RunTLSServer(srv *http.Server, certFile, keyFile string) {
|
||||
e.run(srv, certFile, keyFile)
|
||||
func (e *Echo) RunTLSServer(s *http.Server, certFile, keyFile string) {
|
||||
e.run(s, certFile, keyFile)
|
||||
}
|
||||
|
||||
func (e *Echo) run(s *http.Server, files ...string) {
|
||||
@ -489,7 +490,7 @@ func (e *HTTPError) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
// wraps middleware
|
||||
// wrapMiddleware wraps middleware.
|
||||
func wrapMiddleware(m Middleware) MiddlewareFunc {
|
||||
switch m := m.(type) {
|
||||
case MiddlewareFunc:
|
||||
@ -520,7 +521,7 @@ func wrapMiddleware(m Middleware) MiddlewareFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// Wraps HandlerFunc middleware
|
||||
// wrapHandlerFuncMW wraps HandlerFunc middleware.
|
||||
func wrapHandlerFuncMW(m HandlerFunc) MiddlewareFunc {
|
||||
return func(h HandlerFunc) HandlerFunc {
|
||||
return func(c *Context) error {
|
||||
@ -532,7 +533,7 @@ func wrapHandlerFuncMW(m HandlerFunc) MiddlewareFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// Wraps http.HandlerFunc middleware
|
||||
// wrapHTTPHandlerFuncMW wraps http.HandlerFunc middleware.
|
||||
func wrapHTTPHandlerFuncMW(m http.HandlerFunc) MiddlewareFunc {
|
||||
return func(h HandlerFunc) HandlerFunc {
|
||||
return func(c *Context) error {
|
||||
@ -544,7 +545,7 @@ func wrapHTTPHandlerFuncMW(m http.HandlerFunc) MiddlewareFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// wraps handler
|
||||
// wrapHandler wraps handler.
|
||||
func wrapHandler(h Handler) HandlerFunc {
|
||||
switch h := h.(type) {
|
||||
case HandlerFunc:
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
mw "github.com/labstack/echo/middleware"
|
||||
)
|
||||
|
||||
func upload(c *echo.Context) error {
|
||||
@ -45,7 +46,12 @@ func upload(c *echo.Context) error {
|
||||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
e.Index("public/index.html")
|
||||
|
||||
e.Use(mw.Logger())
|
||||
e.Use(mw.Recover())
|
||||
|
||||
e.Static("/", "public")
|
||||
e.Post("/upload", upload)
|
||||
|
||||
e.Run(":1323")
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
mw "github.com/labstack/echo/middleware"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -68,8 +69,12 @@ func upload(c *echo.Context) error {
|
||||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
e.SetDebug(true)
|
||||
e.Index("public/index.html")
|
||||
|
||||
e.Use(mw.Logger())
|
||||
e.Use(mw.Recover())
|
||||
|
||||
e.Static("/", "public")
|
||||
e.Post("/upload", upload)
|
||||
|
||||
e.Run(":1323")
|
||||
}
|
||||
|
@ -40,8 +40,8 @@ func main() {
|
||||
//------
|
||||
|
||||
blog := echo.New()
|
||||
api.Use(mw.Logger())
|
||||
api.Use(mw.Recover())
|
||||
blog.Use(mw.Logger())
|
||||
blog.Use(mw.Recover())
|
||||
|
||||
hosts["blog.localhost:1323"] = blog
|
||||
|
||||
|
33
recipes/websocket/public/index.html
Normal file
33
recipes/websocket/public/index.html
Normal file
@ -0,0 +1,33 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>WebSocket</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre>
|
||||
</pre>
|
||||
<script>
|
||||
var loc = window.location;
|
||||
var uri;
|
||||
|
||||
if (loc.protocol === 'https:') {
|
||||
uri = 'wss:';
|
||||
} else {
|
||||
uri = 'ws:';
|
||||
}
|
||||
uri += '//' + loc.host;
|
||||
uri += loc.pathname + 'ws';
|
||||
|
||||
ws = new WebSocket(uri)
|
||||
|
||||
ws.onopen = function() {
|
||||
console.log('Connected')
|
||||
}
|
||||
|
||||
ws.onmessage = function(evt) {
|
||||
console.log('Message')
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -9,10 +9,15 @@ import (
|
||||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
|
||||
e.Use(mw.Logger())
|
||||
e.Use(mw.Recover())
|
||||
|
||||
e.Static("/", "public")
|
||||
e.WebSocket("/ws", func(c *echo.Context) error {
|
||||
io.Copy(c.Socket(), c.Socket())
|
||||
return nil
|
||||
})
|
||||
|
||||
e.Run(":1323")
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
mw "github.com/labstack/echo/middleware"
|
||||
)
|
||||
|
||||
func upload(c *echo.Context) error {
|
||||
@ -31,7 +32,7 @@ func upload(c *echo.Context) error {
|
||||
|
||||
// Read files
|
||||
files := req.MultipartForm.File["files"]
|
||||
for _, f := range(files) {
|
||||
for _, f := range files {
|
||||
// Source file
|
||||
src, err := f.Open()
|
||||
if err != nil {
|
||||
@ -56,11 +57,15 @@ func upload(c *echo.Context) error {
|
||||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
e.Index("public/index.html")
|
||||
|
||||
e.Use(mw.Logger())
|
||||
e.Use(mw.Recover())
|
||||
|
||||
e.Static("/", "public")
|
||||
e.Post("/upload", upload)
|
||||
|
||||
e.Run(":1323")
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
`index.html`
|
||||
|
@ -12,9 +12,10 @@ import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
mw "github.com/labstack/echo/middleware"
|
||||
"io"
|
||||
"os"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func upload(c *echo.Context) error {
|
||||
@ -76,9 +77,13 @@ func upload(c *echo.Context) error {
|
||||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
e.SetDebug(true)
|
||||
e.Index("../file-upload/public/index.html")
|
||||
|
||||
e.Use(mw.Logger())
|
||||
e.Use(mw.Recover())
|
||||
|
||||
e.Static("/", "public")
|
||||
e.Post("/upload", upload)
|
||||
|
||||
e.Run(":1323")
|
||||
}
|
||||
```
|
||||
|
@ -45,8 +45,8 @@ func main() {
|
||||
//------
|
||||
|
||||
blog := echo.New()
|
||||
api.Use(mw.Logger())
|
||||
api.Use(mw.Recover())
|
||||
blog.Use(mw.Logger())
|
||||
blog.Use(mw.Recover())
|
||||
|
||||
hosts["blog.localhost:1323"] = blog
|
||||
|
||||
@ -54,17 +54,17 @@ func main() {
|
||||
return c.String(http.StatusOK, "Blog")
|
||||
})
|
||||
|
||||
//------
|
||||
// Main
|
||||
//------
|
||||
//---------
|
||||
// Website
|
||||
//---------
|
||||
|
||||
main := echo.New()
|
||||
main.Use(mw.Logger())
|
||||
main.Use(mw.Recover())
|
||||
site := echo.New()
|
||||
site.Use(mw.Logger())
|
||||
site.Use(mw.Recover())
|
||||
|
||||
hosts["localhost:1323"] = main
|
||||
hosts["localhost:1323"] = site
|
||||
|
||||
main.Get("/", func(c *echo.Context) error {
|
||||
site.Get("/", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Welcome!")
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user