Skip to content

Commit bf61b1c

Browse files
author
Ammar Raza
committed
fix: added a fucntion to check if the dir is accessible, issue h2oai#2130
1 parent b9e5a07 commit bf61b1c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

server.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/ioutil"
2323
"log"
2424
"net/http"
25+
"os"
2526
"path"
2627
"path/filepath"
2728
"strings"
@@ -70,6 +71,13 @@ func printLaunchBar(addr, baseURL string, isTLS bool) {
7071
log.Println("# └" + bar + "┘")
7172
}
7273

74+
// check if the directory is accessible
75+
func checkDirectory(path string) {
76+
if _, err := os.Stat(path); os.IsNotExist(err) {
77+
log.Fatalf("Directory does not exist: %s", path)
78+
}
79+
}
80+
7381
// Run runs the HTTP server.
7482
func Run(conf ServerConf) {
7583
for _, line := range strings.Split(fmt.Sprintf(logo, conf.Version, conf.BuildDate), "\n") {
@@ -113,20 +121,15 @@ func Run(conf ServerConf) {
113121
handle("_f/", newFileServer(fileDir, conf.Keychain, auth, conf.BaseURL+"_f"))
114122
for _, dir := range conf.PrivateDirs {
115123

124+
checkDirectory(dir)
116125
prefix, src := splitDirMapping(dir)
117-
err := newDirServer(src, conf.Keychain, auth)
118-
if err != nil {
119-
log.Fatalf("Failed to start server due to directory issue: %v", err)
120-
}
121126
echo(Log{"t": "private_dir", "source": src, "address": prefix})
122127
handle(prefix, http.StripPrefix(conf.BaseURL+prefix, newDirServer(src, conf.Keychain, auth)))
123128
}
124129
for _, dir := range conf.PublicDirs {
130+
131+
checkDirectory(dir)
125132
prefix, src := splitDirMapping(dir)
126-
err := newDirServer(src, conf.Keychain, auth)
127-
if err != nil {
128-
log.Fatalf("Failed to start server due to directory issue: %v", err)
129-
}
130133
echo(Log{"t": "public_dir", "source": src, "address": prefix})
131134
handle(prefix, http.StripPrefix(conf.BaseURL+prefix, http.FileServer(http.Dir(src))))
132135
}

0 commit comments

Comments
 (0)