I have a website, which is composed by three smaller 'independent' subsites:
- mysite
- index.html
- favicons
- images
- doc
- index.html
- css
- img
- js
- ...
- editor
- index.html
- images
- js
- src
- ...
Where doc is a site created with Hugo :: A fast and modern static website engine, editor is the mxgraph Graphditor example; and the remaining files make a hand-made landing page.
Besides deploying to any web server, I'd like to distribute the site as an 'standalone application'. To allow so, I wrote this really simple server in go:
package main import ( flag "github.com/ogier/pflag" "fmt" "net/http" "net/http/httputil" "os" "path/filepath" ) var port = flag.IntP("port", "p", 80, "port to serve at") var dir = flag.StringP("dir", "d", "./", "dir to serve from") var verb = flag.BoolP("verbose", "v", false, "") func init() { flag.Parse(); } type justFilesFilesystem struct { fs http.FileSystem; } type neuteredReaddirFile struct { http.File } func (fs justFilesFilesystem) Open(name string) (http.File, error) { f, err := fs.fs.Open(name) if err != nil { return nil, err; } return neuteredReaddirFile{f}, nil } func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) { return nil, nil; } func loggingHandler(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { requestDump, err := httputil.DumpRequest(r, true) if err != nil { fmt.Println(err); } fmt.Println(string(requestDump)) h.ServeHTTP(w, r) }) } func main() { str, err := filepath.Abs(*dir) if err != nil { os.Exit(1); } fmt.Printf("Serving at port %d from dir %s\n\n",*port,str) http.ListenAndServe(fmt.Sprintf(":%d",*port), loggingHandler(http.FileServer(justFilesFilesystem{http.Dir(*dir)}))) } As a result, I can run simpleserver -d <path-to-mysite> and browse the sites through localhost, localhost/doc and localhost/editor.
Then, I'd like to use custom (sub)domain(s) such as mylocal.app, doc.mylocal.app and editor.mylocal.app. So, I added the following line to my the /etc/hosts file: 127.0.0.1 mylocal.app. Therefore, I can browse mylocal.app, mylocal.app/editor and mylocal.app/doc. Moreover, I was able to change it to mylocal.app, mylocal.app:<editor-port> and mylocal.app:<doc-port> with different packages.
However, when I try to use a subdomain, it is not properly resolved, so any reverse-proxy strategy won't work. Since wildcards are not supported, I can add additional entries in the /etc/hosts file, but I'd prefer to avoid it.
Although, an alternative solution is to run dnsmasq, I'd like to keep the application standalone. I found some equivalent golang packages. However, I feel that many features are supported which I don't really need.
Furthermore, since I don't really have to resolve any IP, but to provide an alias of localhost, I think that a proxy could suffice. This would also be easier to configure, since the user could configure the browser only, and no system-wide modification would be required.
Yet, all the traffic from the user would be 'filtered' by my app. Is this correct? If so, can you point me to any reference to implement it in the most clean way. I know this is quite subjective, but I mean a relatively short (say 10 lines of code) snippet so that users can easily check what is going on.
EDIT
I'd like to use something like:
func main() { mymux := http.NewServeMux() mymux.HandleFunc("*.mylocal.app", myHandler) mymux.HandleFunc("*", <systemDefaultHandler>) http.ListenAndServe(":8080", mymux) } or
func main() { mymux := http.NewServeMux() mymux.HandleFunc("editor.mylocal.app", editorHandler) mymux.HandleFunc("doc.mylocal.app", docHandler) mymux.HandleFunc("*.mylocal.app", rootHandler) mymux.HandleFunc("*", <systemDefaultHandler>) http.ListenAndServe(":8080", mymux) } These are only snippets. A complete example is this, which was referenced in the comments by @Steve101.
However, at now, I don't know what systemDefaultHandler is. And that is not solved there.
Apart from that, @faraz suggested using goproxy. I think that the HTTP/HTTPS transparent proxy is the default handler I am looking for. But, using a package only to do that seems excessive to me. Can I achieve the same functionality with built-in resources?