2015-10-02 03:24:38 +02:00
|
|
|
---
|
|
|
|
title: Subdomains
|
|
|
|
menu:
|
|
|
|
main:
|
|
|
|
parent: recipes
|
|
|
|
---
|
2015-07-02 06:22:51 +02:00
|
|
|
|
|
|
|
`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()
|
2015-07-02 20:14:09 +02:00
|
|
|
blog.Use(mw.Logger())
|
|
|
|
blog.Use(mw.Recover())
|
2015-07-02 06:22:51 +02:00
|
|
|
|
|
|
|
hosts["blog.localhost:1323"] = blog
|
|
|
|
|
|
|
|
blog.Get("/", func(c *echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "Blog")
|
|
|
|
})
|
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
//---------
|
|
|
|
// Website
|
|
|
|
//---------
|
2015-07-02 06:22:51 +02:00
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
site := echo.New()
|
|
|
|
site.Use(mw.Logger())
|
|
|
|
site.Use(mw.Recover())
|
2015-07-02 06:22:51 +02:00
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
hosts["localhost:1323"] = site
|
2015-07-02 06:22:51 +02:00
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
site.Get("/", func(c *echo.Context) error {
|
2015-07-02 06:22:51 +02:00
|
|
|
return c.String(http.StatusOK, "Welcome!")
|
|
|
|
})
|
|
|
|
|
|
|
|
http.ListenAndServe(":1323", hosts)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-10-08 00:41:15 +02:00
|
|
|
## Maintainers
|
|
|
|
|
|
|
|
- [axdg](http://github.com/axdg)
|
|
|
|
- [vishr](http://github.com/axdg)
|
|
|
|
|
2015-07-02 06:22:51 +02:00
|
|
|
## [Source Code](https://github.com/labstack/echo/blob/master/recipes/subdomains)
|