39 lines
1002 B
Go
39 lines
1002 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
func SpaHandler(staticPath, indexPath string) http.Handler {
|
|
fs := http.FileServer(http.Dir(staticPath))
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Игнорируем API-запросы
|
|
if strings.HasPrefix(r.URL.Path, "/api/") {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
// Проверяем существование файла
|
|
filePath := filepath.Join(staticPath, r.URL.Path)
|
|
_, err := os.Stat(filePath)
|
|
|
|
// Если файл не существует или путь - SPA-маршрут
|
|
if os.IsNotExist(err) || isSPARoute(r.URL.Path) {
|
|
r.URL.Path = "/" // Переписываем путь для index.html
|
|
}
|
|
|
|
// Отдаем статический файл
|
|
fs.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func isSPARoute(path string) bool {
|
|
spaRoutes := []string{"/", "/update", "/trx"} // Добавьте свои маршруты
|
|
return slices.Contains(spaRoutes, path)
|
|
}
|