mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Added subdomains recipe closes #121
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
992f5ef005
commit
99aa92eb06
@ -82,6 +82,7 @@ $ go get github.com/labstack/echo
|
||||
- [Streaming File Upload](http://echo.labstack.com/recipes/streaming-file-upload)
|
||||
- [Streaming Response](http://echo.labstack.com/recipes/streaming-response)
|
||||
- [WebSocket](http://echo.labstack.com/recipes/websocket)
|
||||
- [Subdomains](http://echo.labstack.com/recipes/subdomains)
|
||||
- [Graceful Shutdown](http://echo.labstack.com/recipes/graceful-shutdown)
|
||||
|
||||
##[Guide](http://echo.labstack.com/guide)
|
||||
|
@ -20,7 +20,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 {
|
||||
|
@ -5,8 +5,8 @@ import (
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"io"
|
||||
"os"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
###[Hello, World!](https://github.com/labstack/echo/tree/master/examples/hello)
|
||||
### Hello, World!
|
||||
|
||||
Create `server.go` with the following content
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
- Multiple form fields and files
|
||||
|
||||
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`
|
||||
|
||||
|
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 Response: recipes/streaming-response.md
|
||||
- WebSocket: recipes/websocket.md
|
||||
- Subdomains: recipes/subdomains.md
|
||||
- Graceful Shutdown: recipes/graceful-shutdown.md
|
||||
extra:
|
||||
site_title: Echo, a fast and unfancy micro web framework for Golang.
|
||||
|
Loading…
Reference in New Issue
Block a user