Here is how I implemented it:
 
func wwwAssetsHandler(w http.ResponseWriter, r *http.Request) {

  if r.Host == "domain.net" || r.Host == "www.domain.net" || r.Host == 
"domain.com" || r.Host == "www.domain.com" { 
       var webrootStr = homePath + "/Desktop/" + "www.domain.com"
fs := http.StripPrefix("/", http.FileServer(http.Dir(webrootStr)))
        fs.ServeHTTP(w, r)
  }
}

...
http.HandleFunc("/assets/", wwwAssetsHandler)
...



On Tuesday, January 9, 2018 at 10:32:43 AM UTC-8, Ayan George wrote:
>
> Constantine Vassilev <ths...@gmail.com <javascript:>> wrote: 
>
> > 
> > I have web server serving requests from two domains: domain1.com and 
> > domain2.com 
> > The assets directories are different for the two domains. 
> > 
> > webrootdomain1/assets 
> > and 
> > webrootdomain2/assets 
> > 
> > How to make the handler for assets to serve different directory 
> depending 
> > from 
> > the requests from different domains? So they to be dependent from the 
> host? 
>
> I think you'll have to check the http.Request.Host and conditionally 
> execute 
> the ServeHTTP method of an http.FileServer based on it. 
>
> You can find an example of what I'm describing below.  I hope I understood 
> your 
> problem. 
>
> package main 
>
> import ( 
>   "log" 
>   "net/http" 
> ) 
>
> func main() { 
>   webrootdomain1 := "./assets1" 
>   webrootdomain2 := "./assets2" 
>
>   http.HandleFunc("/assets/", func() func(http.ResponseWriter, 
> *http.Request) { 
>                 // create a closure with our file handlers. 
>     fs1 := http.StripPrefix("/assets", 
> http.FileServer(http.Dir(webrootdomain1))) 
>     fs2 := http.StripPrefix("/assets", 
> http.FileServer(http.Dir(webrootdomain2))) 
>
>                 // retrun a function that executes the ServeHTTP() method 
> of one of our 
>                 // filehandlers based on the contents of r.Host. 
>     return func(w http.ResponseWriter, r *http.Request) { 
>       log.Printf("got request for host %q, path %q", r.Host, r.URL.Path) 
>       switch r.Host { 
>       case "localhost:8080": 
>         log.Printf("trying to serve file for domain %q", r.Host) 
>         fs1.ServeHTTP(w, r) 
>       case "anotherdomain.com": 
>         fs2.ServeHTTP(w, r) 
>       } 
>     } 
>   }()) 
>
>   http.ListenAndServe(":8080", nil) 
> } 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to