Add a --dev flag to forward requests to node server
This commit is contained in:
parent
d7d7bfa103
commit
684e2e7fa4
2 changed files with 44 additions and 3 deletions
38
static.go
38
static.go
|
@ -1,17 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
var DevProxy string
|
||||
|
||||
func serveOrReverse(forced_url string) func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if forced_url != "" {
|
||||
r.URL.Path = forced_url
|
||||
if DevProxy != "" {
|
||||
if u, err := url.Parse(DevProxy); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
if forced_url != "" {
|
||||
u.Path = path.Join(u.Path, forced_url)
|
||||
} else {
|
||||
u.Path = path.Join(u.Path, r.URL.Path)
|
||||
}
|
||||
|
||||
if r, err := http.NewRequest(r.Method, u.String(), r.Body); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
} else if resp, err := http.DefaultClient.Do(r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
|
||||
for key := range resp.Header {
|
||||
w.Header().Add(key, resp.Header.Get(key))
|
||||
}
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
|
||||
io.Copy(w, resp.Body)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if forced_url != "" {
|
||||
r.URL.Path = forced_url
|
||||
}
|
||||
http.FileServer(Assets).ServeHTTP(w, r)
|
||||
}
|
||||
http.FileServer(Assets).ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue