Skip to content

Commit

Permalink
allow redirect to webDefaultFile on bad http request
Browse files Browse the repository at this point in the history
  • Loading branch information
DocSavage committed Sep 26, 2018
1 parent 2a02a37 commit 091b95a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions scripts/distro-files/config-full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ host = "mygreatserver.test.com" # Lets you specify a user-friendly alias for he
httpAddress = "localhost:8000"
rpcAddress = "localhost:8001"
webClient = "/path/to/webclient"
webDefaultFile = "index.html" # path to optional default file within webClient directory to return.

note = """
You can put anything you want in here and have it available via /api/server/note.
Expand Down
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Config interface {
// Path to web client files
WebClient() string

// Path to default file within WebClient() path to return if HTTP request doesn't exist
WebDefaultFile() string

// Set timing in HTTP header
AllowTiming() bool

Expand Down
15 changes: 10 additions & 5 deletions server/server_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ func (c *tomlConfig) WebClient() string {
return c.Server.WebClient
}

func (c *tomlConfig) WebDefaultFile() string {
return c.Server.WebDefaultFile
}

func (c *tomlConfig) AllowTiming() bool {
return c.Server.AllowTiming
}
Expand Down Expand Up @@ -258,11 +262,12 @@ func WebServer() string {

// ServerConfig holds ports, host name, and other properties of this dvid server.
type ServerConfig struct {
Host string
HTTPAddress string
RPCAddress string
WebClient string
Note string
Host string
HTTPAddress string
RPCAddress string
WebClient string
WebDefaultFile string
Note string

AllowTiming bool // If true, returns * for Timing-Allow-Origin in response headers.
StartWebhook string // http address that should be called when server is started up.
Expand Down
11 changes: 11 additions & 0 deletions server/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,17 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
dvid.SendHTTP(w, r, path, data)
} else {
filename := filepath.Join(config.WebClient(), path)
redirectURL := config.WebDefaultFile()
if len(redirectURL) > 0 {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
if redirectURL[0] != '/' {
redirectURL = "/" + redirectURL
}
http.Redirect(w, r, redirectURL, http.StatusPermanentRedirect)
return
}
}
dvid.Debugf("[%s] Serving from webclient directory: %s\n", r.Method, filename)
http.ServeFile(w, r, filename)
}
Expand Down

0 comments on commit 091b95a

Please sign in to comment.