mirror of
https://github.com/labstack/echo.git
synced 2025-07-05 00:58:47 +02:00
Added subdomains recipe closes #121
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -82,6 +82,7 @@ $ go get github.com/labstack/echo
|
|||||||
- [Streaming File Upload](http://echo.labstack.com/recipes/streaming-file-upload)
|
- [Streaming File Upload](http://echo.labstack.com/recipes/streaming-file-upload)
|
||||||
- [Streaming Response](http://echo.labstack.com/recipes/streaming-response)
|
- [Streaming Response](http://echo.labstack.com/recipes/streaming-response)
|
||||||
- [WebSocket](http://echo.labstack.com/recipes/websocket)
|
- [WebSocket](http://echo.labstack.com/recipes/websocket)
|
||||||
|
- [Subdomains](http://echo.labstack.com/recipes/subdomains)
|
||||||
- [Graceful Shutdown](http://echo.labstack.com/recipes/graceful-shutdown)
|
- [Graceful Shutdown](http://echo.labstack.com/recipes/graceful-shutdown)
|
||||||
|
|
||||||
##[Guide](http://echo.labstack.com/guide)
|
##[Guide](http://echo.labstack.com/guide)
|
||||||
|
@ -20,7 +20,7 @@ func upload(c *echo.Context) error {
|
|||||||
|
|
||||||
// Read files
|
// Read files
|
||||||
files := req.MultipartForm.File["files"]
|
files := req.MultipartForm.File["files"]
|
||||||
for _, f := range(files) {
|
for _, f := range files {
|
||||||
// Source file
|
// Source file
|
||||||
src, err := f.Open()
|
src, err := f.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func upload(c *echo.Context) error {
|
func upload(c *echo.Context) error {
|
||||||
|
67
recipes/subdomains/server.go
Normal file
67
recipes/subdomains/server.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/labstack/echo"
|
||||||
|
mw "github.com/labstack/echo/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Hosts map[string]http.Handler
|
||||||
|
|
||||||
|
func (h Hosts) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if handler := h[r.Host]; handler != nil {
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Host map
|
||||||
|
hosts := make(Hosts)
|
||||||
|
|
||||||
|
//-----
|
||||||
|
// API
|
||||||
|
//-----
|
||||||
|
|
||||||
|
api := echo.New()
|
||||||
|
api.Use(mw.Logger())
|
||||||
|
api.Use(mw.Recover())
|
||||||
|
|
||||||
|
hosts["api.localhost:1323"] = api
|
||||||
|
|
||||||
|
api.Get("/", func(c *echo.Context) error {
|
||||||
|
return c.String(http.StatusOK, "API")
|
||||||
|
})
|
||||||
|
|
||||||
|
//------
|
||||||
|
// Blog
|
||||||
|
//------
|
||||||
|
|
||||||
|
blog := echo.New()
|
||||||
|
api.Use(mw.Logger())
|
||||||
|
api.Use(mw.Recover())
|
||||||
|
|
||||||
|
hosts["blog.localhost:1323"] = blog
|
||||||
|
|
||||||
|
blog.Get("/", func(c *echo.Context) error {
|
||||||
|
return c.String(http.StatusOK, "Blog")
|
||||||
|
})
|
||||||
|
|
||||||
|
//---------
|
||||||
|
// Website
|
||||||
|
//---------
|
||||||
|
|
||||||
|
site := echo.New()
|
||||||
|
site.Use(mw.Logger())
|
||||||
|
site.Use(mw.Recover())
|
||||||
|
|
||||||
|
hosts["localhost:1323"] = site
|
||||||
|
|
||||||
|
site.Get("/", func(c *echo.Context) error {
|
||||||
|
return c.String(http.StatusOK, "Welcome!")
|
||||||
|
})
|
||||||
|
|
||||||
|
http.ListenAndServe(":1323", hosts)
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
# Echo
|
# Echo
|
||||||
|
|
||||||
A fast and light web framework for Golang.
|
A fast and unfancy micro web framework for Golang.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ A fast and light web framework for Golang.
|
|||||||
$ go get github.com/labstack/echo
|
$ go get github.com/labstack/echo
|
||||||
```
|
```
|
||||||
|
|
||||||
###[Hello, World!](https://github.com/labstack/echo/tree/master/examples/hello)
|
### Hello, World!
|
||||||
|
|
||||||
Create `server.go` with the following content
|
Create `server.go` with the following content
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
- Multiple form fields and files
|
- Multiple form fields and files
|
||||||
|
|
||||||
Use `req.ParseMultipartForm(16 << 20)` for manually parsing multipart form. It gives
|
Use `req.ParseMultipartForm(16 << 20)` for manually parsing multipart form. It gives
|
||||||
us option to specify maximum memory used while parsing the request body.
|
us an option to specify the maximum memory used while parsing the request body.
|
||||||
|
|
||||||
`server.go`
|
`server.go`
|
||||||
|
|
||||||
|
75
website/docs/recipes/subdomains.md
Normal file
75
website/docs/recipes/subdomains.md
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
## Subdomains
|
||||||
|
|
||||||
|
`server.go`
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/labstack/echo"
|
||||||
|
mw "github.com/labstack/echo/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Hosts map[string]http.Handler
|
||||||
|
|
||||||
|
func (h Hosts) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if handler := h[r.Host]; handler != nil {
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Host map
|
||||||
|
hosts := make(Hosts)
|
||||||
|
|
||||||
|
//-----
|
||||||
|
// API
|
||||||
|
//-----
|
||||||
|
|
||||||
|
api := echo.New()
|
||||||
|
api.Use(mw.Logger())
|
||||||
|
api.Use(mw.Recover())
|
||||||
|
|
||||||
|
hosts["api.localhost:1323"] = api
|
||||||
|
|
||||||
|
api.Get("/", func(c *echo.Context) error {
|
||||||
|
return c.String(http.StatusOK, "API")
|
||||||
|
})
|
||||||
|
|
||||||
|
//------
|
||||||
|
// Blog
|
||||||
|
//------
|
||||||
|
|
||||||
|
blog := echo.New()
|
||||||
|
api.Use(mw.Logger())
|
||||||
|
api.Use(mw.Recover())
|
||||||
|
|
||||||
|
hosts["blog.localhost:1323"] = blog
|
||||||
|
|
||||||
|
blog.Get("/", func(c *echo.Context) error {
|
||||||
|
return c.String(http.StatusOK, "Blog")
|
||||||
|
})
|
||||||
|
|
||||||
|
//------
|
||||||
|
// Main
|
||||||
|
//------
|
||||||
|
|
||||||
|
main := echo.New()
|
||||||
|
main.Use(mw.Logger())
|
||||||
|
main.Use(mw.Recover())
|
||||||
|
|
||||||
|
hosts["localhost:1323"] = main
|
||||||
|
|
||||||
|
main.Get("/", func(c *echo.Context) error {
|
||||||
|
return c.String(http.StatusOK, "Welcome!")
|
||||||
|
})
|
||||||
|
|
||||||
|
http.ListenAndServe(":1323", hosts)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## [Source Code](https://github.com/labstack/echo/blob/master/recipes/subdomains)
|
@ -12,6 +12,7 @@ pages:
|
|||||||
- Streaming File Upload: recipes/streaming-file-upload.md
|
- Streaming File Upload: recipes/streaming-file-upload.md
|
||||||
- Streaming Response: recipes/streaming-response.md
|
- Streaming Response: recipes/streaming-response.md
|
||||||
- WebSocket: recipes/websocket.md
|
- WebSocket: recipes/websocket.md
|
||||||
|
- Subdomains: recipes/subdomains.md
|
||||||
- Graceful Shutdown: recipes/graceful-shutdown.md
|
- Graceful Shutdown: recipes/graceful-shutdown.md
|
||||||
extra:
|
extra:
|
||||||
site_title: Echo, a fast and unfancy micro web framework for Golang.
|
site_title: Echo, a fast and unfancy micro web framework for Golang.
|
||||||
|
Reference in New Issue
Block a user