{"action":"created","issue":{"url":"https://api.github.com/repos/gin-gonic/gin/issues/347","labels_url":"https://api.github.com/repos/gin-gonic/gin/issues/347/labels{/name}","comments_url":"https://api.github.com/repos/gin-gonic/gin/issues/347/comments","events_url":"https://api.github.com/repos/gin-gonic/gin/issues/347/events","html_url":"https://github.com/gin-gonic/gin/issues/347","id":90265995,"number":347,"title":"[Question] How to get multidomain/subdomain (virtualhost) routing?","user":{"login":"lawlite","id":11742404,"avatar_url":"https://avatars.githubusercontent.com/u/11742404?v=3","gravatar_id":"","url":"https://api.github.com/users/lawlite","html_url":"https://github.com/lawlite","followers_url":"https://api.github.com/users/lawlite/followers","following_url":"https://api.github.com/users/lawlite/following{/other_user}","gists_url":"https://api.github.com/users/lawlite/gists{/gist_id}","starred_url":"https://api.github.com/users/lawlite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lawlite/subscriptions","organizations_url":"https://api.github.com/users/lawlite/orgs","repos_url":"https://api.github.com/users/lawlite/repos","events_url":"https://api.github.com/users/lawlite/events{/privacy}","received_events_url":"https://api.github.com/users/lawlite/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-23T02:12:30Z","updated_at":"2015-06-25T01:09:55Z","closed_at":null,"body":"Hello there!\r\nI have a question: How to get multidomain/subdomain (virtualhost) routing?\r\n\r\nI tried this,\r\n\r\n    r := gin.Default()\r\n    r.GET(\"example1.com/\", func(c *gin.Context) { // http://example1.com\r\n        c.String(200, \"welcome to example1.com\")\r\n    })\r\n    r.GET(\"example2.com/\", func(c *gin.Context) { // http://example2.com\r\n        c.String(200, \"welcome to example2.com\")\r\n    })\r\n\r\nbut it didn't work, so I looked features of HttpRouter.\r\nhttps://github.com/julienschmidt/httprouter#multi-domain--sub-domains\r\n\r\nI referred that and tried this.\r\n\r\n    r := gin.Default()\r\n    r2 := gin.Default()\r\n    ...\r\n    ...\r\n    hs := make(HostSwitch)\r\n    hs[\"example1.com\"] = r\r\n    hs[\"example2.com\"] = r2\r\n    http.ListenAndServe(\":80\", hs)\r\n\r\nNow, it works like I intended. But I think that this is not good. (Because I am a beginner of Golang.)\r\n\r\nIs there a better way to do it?\r\n\r\n\r\nAnd I wanna request one more thing.\r\n\r\nPlease support LoggerWithWriter (logger.go) function to write its host like\r\n\"[GIN] example1.com 2015/06/23 (...)\"\r\n\"[GIN] example2.com 2015/06/23 (...)\"\r\n.\r\n\r\nThank you! I started to learn Golang recently and I starred Gin just yesterday, this framework is awesome."},"comment":{"url":"https://api.github.com/repos/gin-gonic/gin/issues/comments/115059848","html_url":"https://github.com/gin-gonic/gin/issues/347#issuecomment-115059848","issue_url":"https://api.github.com/repos/gin-gonic/gin/issues/347","id":115059848,"user":{"login":"javierprovecho","id":2457529,"avatar_url":"https://avatars.githubusercontent.com/u/2457529?v=3","gravatar_id":"","url":"https://api.github.com/users/javierprovecho","html_url":"https://github.com/javierprovecho","followers_url":"https://api.github.com/users/javierprovecho/followers","following_url":"https://api.github.com/users/javierprovecho/following{/other_user}","gists_url":"https://api.github.com/users/javierprovecho/gists{/gist_id}","starred_url":"https://api.github.com/users/javierprovecho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/javierprovecho/subscriptions","organizations_url":"https://api.github.com/users/javierprovecho/orgs","repos_url":"https://api.github.com/users/javierprovecho/repos","events_url":"https://api.github.com/users/javierprovecho/events{/privacy}","received_events_url":"https://api.github.com/users/javierprovecho/received_events","type":"User","site_admin":false},"created_at":"2015-06-25T01:09:55Z","updated_at":"2015-06-25T01:09:55Z","body":"Checkout this example from httprouter:\n\n\n\n\n\nMulti-domain / Sub-domains\nHere is a quick example: Does your server serve multiple domains / hosts? You want to use sub-domains? Define a router per host!\n\n// We need an object that implements the http.Handler interface.\n// Therefore we need a type for which we implement the ServeHTTP method.\n// We just use a map here, in which we map host names (with port) to http.Handlers\ntype HostSwitch map[string]http.Handler\n\n// Implement the ServerHTTP method on our new type\nfunc (hs HostSwitch) ServeHTTP(w http.ResponseWriter, r *http.Request) {\n    // Check if a http.Handler is registered for the given host.\n    // If yes, use it to handle the request.\n    if handler := hs[r.Host]; handler != nil {\n        handler.ServeHTTP(w, r)\n    } else {\n        // Handle host names for wich no handler is registered\n        http.Error(w, \"Forbidden\", 403) // Or Redirect?\n    }\n}\n\nfunc main() {\n    // Initialize a router as usual\n    router := httprouter.New()\n    router.GET(\"/\", Index)\n    router.GET(\"/hello/:name\", Hello)\n\n    // Make a new HostSwitch and insert the router (our http handler)\n    // for example.com and port 12345\n    hs := make(HostSwitch)\n    hs[\"example.com:12345\"] = router\n\n    // Use the HostSwitch to listen and serve on port 12345\n    log.Fatal(http.ListenAndServe(\":12345\", hs))\n}\n\n\n\n\n\nNOTE: The information in this e-mail is confidential, being solely for the addressee named above. If you read this message and not the intended recipient, please note that it is completely forbidden any use, disclosure, distribution and / or duplication of this communication without the consent of the sender. If you have received this message in error, please notify us immediately by electronic mail and its elimination.\n\nOn Thu, Jun 25, 2015 at 3:06 AM, dkoston <notifications@github.com> wrote:\n\n> You'll need to use a different router per host (as shown in your working example) because the routes are expected to be different for each host. If you use named functions, it's possible to re-use the functions in both host's routes. \n> If you need to use a custom logging format, I suggest writing a logger middleware that suits your needs. You could choose 2 options:\n> 1.  Support custom logging formats based on how the middleware is instantiated (more challenging but useful to the community)\n> 2.  Write the middleware to support your specific logging format (easier but less useful to others)\n> You can use both the logger and commonlog middleware as examples:\n> https://github.com/gin-gonic/gin/blob/master/logger.go\n> https://github.com/gin-gonic/contrib/blob/master/commonlog/commonlog.go\n> ---\n> Reply to this email directly or view it on GitHub:\n> https://github.com/gin-gonic/gin/issues/347#issuecomment-115059469"}}