commit 30f1ad4f6320fd1419bdba3cde22d0898433b250
parent ad743708e54424cdf3bdfe28f7bb56b63a7209bf
Author: Brian C. Lane <bcl@brianlane.com>
Date: Sat, 2 Apr 2022 15:09:17 -0700
Add -path DIR to serve files from a different directory
Diffstat:
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/main.go b/main.go
@@ -41,6 +41,7 @@ type cmdlineArgs struct {
Cert string
Key string
SinglePage bool
+ Path string
}
var cfg = cmdlineArgs{
@@ -50,6 +51,7 @@ var cfg = cmdlineArgs{
Cert: "/var/tmp/cert.pem",
Key: "/var/tmp/key.pem",
SinglePage: false,
+ Path: "./",
}
func init() {
@@ -59,6 +61,7 @@ func init() {
flag.StringVar(&cfg.Cert, "cert", cfg.Cert, "Path to temporary cert.pem")
flag.StringVar(&cfg.Key, "key", cfg.Key, "Path to temporary key.pem")
flag.BoolVar(&cfg.SinglePage, "single", cfg.SinglePage, "Single page app, direct all unknown routes to index.html")
+ flag.StringVar(&cfg.Path, "path", cfg.Path, "Path to serve files from (./)")
flag.Parse()
}
@@ -162,9 +165,9 @@ func (fs singlePageFileServer) Open(name string) (http.File, error) {
func main() {
if !cfg.SinglePage {
- http.Handle("/", http.FileServer(http.Dir("./")))
+ http.Handle("/", http.FileServer(http.Dir(cfg.Path)))
} else {
- fs := singlePageFileServer{http.Dir("./")}
+ fs := singlePageFileServer{http.Dir(cfg.Path)}
http.Handle("/", http.FileServer(fs))
}
listen := fmt.Sprintf("%s:%d", cfg.ListenIP, cfg.ListenPort)