Skip to content

Commit

Permalink
fix: added a fucntion to check if the dir is accessible, issue h2oai#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ammar Raza committed Dec 6, 2023
1 parent b9e5a07 commit bf61b1c
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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))))
}
Expand Down

0 comments on commit bf61b1c

Please sign in to comment.