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,67 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func AddService(w http.ResponseWriter, req *http.Request) {
|
||||
req.ParseMultipartForm(10 << 20)
|
||||
file, handler, err := req.FormFile("img")
|
||||
if err != nil {
|
||||
fmt.Println("error retrieving the file: ", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fmt.Printf("Uploaded File: %+v\n", handler.Filename)
|
||||
fmt.Printf("File Size: %+v\n", handler.Size)
|
||||
fmt.Printf("MIME Header: %+v\n", handler.Header)
|
||||
|
||||
dst, err := os.Create(os.Getenv("IMG_UPLOAD_PATH") + "/" + handler.Filename)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating destination file:", err)
|
||||
return
|
||||
}
|
||||
defer dst.Close()
|
||||
if _, err := io.Copy(dst, file); err != nil {
|
||||
fmt.Println("Error copying file:", err)
|
||||
return
|
||||
}
|
||||
newService := make(map[string]any)
|
||||
newService["img"] = "img/" + handler.Filename
|
||||
newService["href"] = req.FormValue("href")
|
||||
newService["text"] = req.FormValue("text")
|
||||
newService["alt"] = req.FormValue("text") + "icon"
|
||||
newService["key"] = uuid.New().String()
|
||||
fmt.Println(newService)
|
||||
allservices := ReadServicesFile()
|
||||
var allservicesmap []map[string]any
|
||||
err = json.Unmarshal([]byte(allservices), &allservicesmap)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
exists := false
|
||||
for i := range allservicesmap {
|
||||
if allservicesmap[i]["href"] == newService["href"] {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
allservicesmap = append(allservicesmap, newService)
|
||||
res, err := json.Marshal(allservicesmap)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
os.WriteFile(os.Getenv("SERVICES_FILE"), res, 4755)
|
||||
fmt.Fprintf(w, "%s", res)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, "{\"status\":\"already exists\"}")
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ReadServicesFile() string {
|
||||
data, e := os.ReadFile(os.Getenv("SERVICES_FILE"))
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func Services(w http.ResponseWriter, req *http.Request) {
|
||||
switch req.Method {
|
||||
case "GET":
|
||||
fmt.Fprintf(w, "%s", ReadServicesFile())
|
||||
case "POST":
|
||||
data, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "Error to read body: %v", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
os.WriteFile(os.Getenv("SERVICES_FILE"), data, 4755)
|
||||
fmt.Fprint(w, string(data))
|
||||
}
|
||||
}
|
||||
@@ -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