waveterm/pkg/docsite/docsite.go
Evan Simkowitz b81ab63ddc
Fix 404 for docsite subpages when hard reloading app (#995)
This adds a custom resolver to the docsite server to fall back to .html
addresses when it gets a not found for a given path. This fixes an issue
where subpages would return a 404 after a hard reload of the frontend. I
also added an effect that will run on startup to resolve the latest
docsite url and set it in the metadata, since the backend port changes
on every run of the app. I also made it so that setting the default
homepage in the webview widget will also unset any block-specific
homepage.
2024-10-09 12:42:33 -07:00

46 lines
1016 B
Go

package docsite
import (
"log"
"net/http"
"os"
"path/filepath"
"github.com/wavetermdev/waveterm/pkg/wavebase"
)
var docsiteStaticPath = filepath.Join(wavebase.GetWaveAppPath(), "docsite")
var docsiteHandler http.Handler
func GetDocsiteHandler() http.Handler {
stat, err := os.Stat(docsiteStaticPath)
if docsiteHandler == nil {
log.Println("Docsite is nil, initializing")
if err == nil && stat.IsDir() {
log.Printf("Found static site at %s, serving\n", docsiteStaticPath)
docsiteHandler = http.FileServer(HTMLDir{http.Dir(docsiteStaticPath)})
} else {
log.Println("Did not find static site, serving not found handler")
docsiteHandler = http.NotFoundHandler()
}
}
return docsiteHandler
}
type HTMLDir struct {
d http.Dir
}
func (d HTMLDir) Open(name string) (http.File, error) {
// Try name as supplied
f, err := d.d.Open(name)
if os.IsNotExist(err) {
// Not found, try with .html
if f, err := d.d.Open(name + ".html"); err == nil {
return f, nil
}
}
return f, err
}