I want to create a system where I can run multiple domains at single Go app. But, the app should show me a different content based on the requesting domain.
Suppose, the app runs on localhost:9090 and all the domains will be pointed to this using nginx reverse proxy. But, I should see totally different content based on incoming website request.
I thought of one way but is looking for another way which might be better.
1 - Save the content & data in DB based on domain
2 - Read the incoming URL and fetch data based on that & show that.
Is it a good model? Please provide your valuable suggestions.
If the sites are static content I suggest looking at Caddy
If you want to dynamically serve content then using the stdlib http listener and router (or one of the 3rd party libraries) should do the trick. Look at Writing Web Applications from the Go blog or LearnServerProgramming from the Go wiki.
r := mux.NewRouter()
// Only matches if domain is "www.example.com".
r.Host("www.example.com")
// Matches a dynamic subdomain.
r.Host("{subdomain:[a-z]+}.domain.com")
if you need to test this on localhost, you can edit /etc/hosts to add more sub/domain names to verify
Thanks for your replies. I totally fell in love with this forum.
@pieterlouw - I really appreciate you for providing the links but I have already read those and created the website core logic. I was just using different ports to run multiple websites but was avoiding that model as either I had to create a separate executable file for each domain or had to use multiple goroutine in same file.
@sairam - Thanks for the info sairam. I did checked that code earlier too but it will be a part of the concept I am making.
I think in my case a http middleware would be the solution.
How about this logic:
1 - I create a middleware which checks the domain using mux (as @sairam suggested) and search it in DB.
2 - Then, shows the content by setting up the cookies.
I will test it out and let you guys know how it went.