27 lines
624 B
Go
27 lines
624 B
Go
package httpserver
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func (s *Server) appFileServer() http.Handler {
|
|
files := http.FileServer(http.Dir(s.config.Web.DistDir))
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
cleanPath := filepath.Clean(strings.TrimPrefix(r.URL.Path, "/"))
|
|
if cleanPath == "." {
|
|
cleanPath = "index.html"
|
|
}
|
|
|
|
target := filepath.Join(s.config.Web.DistDir, cleanPath)
|
|
if info, err := os.Stat(target); err == nil && !info.IsDir() {
|
|
files.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
http.ServeFile(w, r, filepath.Join(s.config.Web.DistDir, "index.html"))
|
|
})
|
|
}
|