diff --git a/README.md b/README.md index 825aff83..19a0a047 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/recipes/file-upload/server.go b/recipes/file-upload/server.go index 7438931e..2946a586 100644 --- a/recipes/file-upload/server.go +++ b/recipes/file-upload/server.go @@ -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 { diff --git a/recipes/streaming-file-upload/server.go b/recipes/streaming-file-upload/server.go index 9966541b..c39b1d4a 100644 --- a/recipes/streaming-file-upload/server.go +++ b/recipes/streaming-file-upload/server.go @@ -5,8 +5,8 @@ import ( "github.com/labstack/echo" "io" - "os" "net/http" + "os" ) func upload(c *echo.Context) error { diff --git a/recipes/subdomains/server.go b/recipes/subdomains/server.go new file mode 100644 index 00000000..78dcbdef --- /dev/null +++ b/recipes/subdomains/server.go @@ -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) +} diff --git a/website/docs/index.md b/website/docs/index.md index 2615eeab..9f486626 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -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 diff --git a/website/docs/recipes/file-upload.md b/website/docs/recipes/file-upload.md index 762c310c..2eb01ed5 100644 --- a/website/docs/recipes/file-upload.md +++ b/website/docs/recipes/file-upload.md @@ -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` diff --git a/website/docs/recipes/subdomains.md b/website/docs/recipes/subdomains.md new file mode 100644 index 00000000..700f97e2 --- /dev/null +++ b/website/docs/recipes/subdomains.md @@ -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) diff --git a/website/mkdocs.yml b/website/mkdocs.yml index cd627c69..f1a57888 100644 --- a/website/mkdocs.yml +++ b/website/mkdocs.yml @@ -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.