51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package spa_handler
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
type handler struct {
|
|
staticFS embed.FS
|
|
indexPath string
|
|
}
|
|
|
|
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
reqPath := r.URL.Path
|
|
lookupPath := path.Dir(h.indexPath)
|
|
f, err := h.staticFS.Open(fmt.Sprintf("%s%s", lookupPath, reqPath))
|
|
if f != nil {
|
|
f.Close()
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
indexFile, indexErr := h.staticFS.Open(h.indexPath)
|
|
if indexErr != nil {
|
|
http.Error(w, "Index file not found", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer indexFile.Close()
|
|
tryToDeliverFile(indexFile, h.indexPath, w, r)
|
|
return
|
|
} else if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
tryToDeliverFile(f, h.indexPath, w, r)
|
|
}
|
|
|
|
func HandleFiles(path string, options HandlerOptions) {
|
|
if options.AdditionalMimeTypeMapping != nil {
|
|
for key, value := range options.AdditionalMimeTypeMapping {
|
|
contentTypeMapping[key] = value
|
|
}
|
|
}
|
|
http.Handle(path, handler{
|
|
staticFS: options.Files,
|
|
indexPath: options.IndexPath,
|
|
})
|
|
}
|