diff --git a/server.go b/server.go index 24dd439c8e..64705ff7f2 100644 --- a/server.go +++ b/server.go @@ -22,6 +22,7 @@ import ( "io/ioutil" "log" "net/http" + "os" "path" "path/filepath" "strings" @@ -70,6 +71,13 @@ func printLaunchBar(addr, baseURL string, isTLS bool) { log.Println("# └" + bar + "┘") } +// check if the directory is accessible +func checkDirectory(path string) { + if _, err := os.Stat(path); os.IsNotExist(err) { + log.Fatalf("Directory does not exist: %s", path) + } +} + // Run runs the HTTP server. func Run(conf ServerConf) { for _, line := range strings.Split(fmt.Sprintf(logo, conf.Version, conf.BuildDate), "\n") { @@ -113,20 +121,15 @@ func Run(conf ServerConf) { handle("_f/", newFileServer(fileDir, conf.Keychain, auth, conf.BaseURL+"_f")) for _, dir := range conf.PrivateDirs { + checkDirectory(dir) prefix, src := splitDirMapping(dir) - err := newDirServer(src, conf.Keychain, auth) - if err != nil { - log.Fatalf("Failed to start server due to directory issue: %v", err) - } echo(Log{"t": "private_dir", "source": src, "address": prefix}) handle(prefix, http.StripPrefix(conf.BaseURL+prefix, newDirServer(src, conf.Keychain, auth))) } for _, dir := range conf.PublicDirs { + + checkDirectory(dir) prefix, src := splitDirMapping(dir) - err := newDirServer(src, conf.Keychain, auth) - if err != nil { - log.Fatalf("Failed to start server due to directory issue: %v", err) - } echo(Log{"t": "public_dir", "source": src, "address": prefix}) handle(prefix, http.StripPrefix(conf.BaseURL+prefix, http.FileServer(http.Dir(src)))) }