2024-10-04 05:28:05 +02:00
|
|
|
package docsite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wavebase"
|
|
|
|
)
|
|
|
|
|
|
|
|
var docsiteHandler http.Handler
|
|
|
|
|
|
|
|
func GetDocsiteHandler() http.Handler {
|
2024-11-12 21:30:37 +01:00
|
|
|
docsiteStaticPath := filepath.Join(wavebase.GetWaveAppPath(), "docsite")
|
2024-10-04 05:28:05 +02:00
|
|
|
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)
|
2024-10-09 21:42:33 +02:00
|
|
|
docsiteHandler = http.FileServer(HTMLDir{http.Dir(docsiteStaticPath)})
|
2024-10-04 05:28:05 +02:00
|
|
|
} else {
|
2024-11-12 21:30:37 +01:00
|
|
|
log.Printf("Did not find static site at %s, serving not found handler. stat: %v, err: %v\n", docsiteStaticPath, stat, err)
|
2024-10-04 05:28:05 +02:00
|
|
|
docsiteHandler = http.NotFoundHandler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return docsiteHandler
|
|
|
|
}
|
2024-10-09 21:42:33 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|