Use generate static assets, and allow to develop through proxy
This commit is contained in:
parent
addf0e96a1
commit
a5e7f79b18
|
@ -1 +1,2 @@
|
|||
libredns
|
||||
libredns
|
||||
bindata.go
|
2
main.go
2
main.go
|
@ -53,11 +53,11 @@ func StripPrefix(prefix string, h http.Handler) http.Handler {
|
|||
|
||||
func main() {
|
||||
// Read parameters from command line
|
||||
flag.StringVar(&DevProxy, "dev", DevProxy, "Proxify traffic to this host for static assets")
|
||||
var bind = flag.String("bind", ":8081", "Bind port/socket")
|
||||
var dsn = flag.String("dsn", DSNGenerator(), "DSN to connect to the MySQL server")
|
||||
var baseURL = flag.String("baseurl", "/", "URL prepended to each URL")
|
||||
flag.StringVar(&api.DefaultNameServer, "defaultns", api.DefaultNameServer, "Adress to the default name server")
|
||||
flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files")
|
||||
flag.Parse()
|
||||
|
||||
// Sanitize options
|
||||
|
|
143
static.go
143
static.go
|
@ -1,7 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"git.nemunai.re/libredns/api"
|
||||
|
@ -9,29 +12,151 @@ import (
|
|||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
var StaticDir = "./htdocs/"
|
||||
//go:generate yarn --cwd htdocs --offline build
|
||||
//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" htdocs/dist/...
|
||||
//go:generate go fmt bindata.go
|
||||
|
||||
const StaticDir string = "htdocs/dist"
|
||||
var DevProxy string
|
||||
|
||||
func init() {
|
||||
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset("htdocs/dist/index.html"); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/zones/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset("htdocs/dist/index.html"); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
r.URL.Path = "/"
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
|
||||
api.Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset(path.Join(StaticDir, r.URL.Path)); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset(path.Join(StaticDir, r.URL.Path)); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset(path.Join(StaticDir, r.URL.Path)); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset(path.Join(StaticDir, r.URL.Path)); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "text/javascript")
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
|
||||
|
||||
api.Router().GET("/favicon.ico", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset(path.Join(StaticDir, r.URL.Path)); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/manifest.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset(path.Join(StaticDir, r.URL.Path)); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/robots.txt", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset(path.Join(StaticDir, r.URL.Path)); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/service-worker.js", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset(path.Join(StaticDir, r.URL.Path)); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "text/javascript")
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func fwd_request(w http.ResponseWriter, r *http.Request, fwd string) {
|
||||
if u, err := url.Parse(fwd); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue