34 lines
645 B
Go
34 lines
645 B
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
type SpaHandler struct {
|
||
|
StaticPath string
|
||
|
IndexPath string
|
||
|
}
|
||
|
|
||
|
func (h SpaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
|
path, err := filepath.Abs(r.URL.Path)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
path = filepath.Join(h.StaticPath, path)
|
||
|
|
||
|
_, err = os.Stat(path)
|
||
|
if os.IsNotExist(err) {
|
||
|
http.ServeFile(w, r, filepath.Join(h.StaticPath, h.IndexPath))
|
||
|
return
|
||
|
} else if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
http.FileServer(http.Dir(h.StaticPath)).ServeHTTP(w, r)
|
||
|
}
|