added backend
This commit is contained in:
1 parent
9842ccee3d
commit
40bcd02c36
10 files changed
+340
-75
No files matched your search
@@ -0,0 +1,38 @@
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user