перенес создание протокола в Go Excelize

This commit is contained in:
Artem Tsyrulnikov
2026-04-05 14:59:54 +03:00
parent 730d453371
commit ddba2dc3e1
13 changed files with 171 additions and 1330 deletions

View File

@@ -11,6 +11,8 @@ import (
"net/url"
"os"
"strings"
"github.com/go-chi/chi/v5"
)
//go:embed dist/*
@@ -31,7 +33,6 @@ func main() {
originalDirector := proxy.Director
proxy.Director = func(r *http.Request) {
originalDirector(r)
// Rewrite /api/... -> /...
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/api")
if r.URL.Path == "" {
r.URL.Path = "/"
@@ -46,31 +47,34 @@ func main() {
}
fileServer := http.FileServer(http.FS(distSub))
mux := http.NewServeMux()
r := chi.NewRouter()
// API proxy
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
// API routes
r.Route("/api", func(api chi.Router) {
// Protocol generation handled locally (xlsx fill via excelize)
api.Post("/protocols/", protocolHandler(*apiTarget))
// All other /api/* requests proxied to Python backend
api.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
})
})
// SPA: serve static files, fallback to index.html
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
// Try to serve the file directly
path := strings.TrimPrefix(r.URL.Path, "/")
if path == "" {
path = "index.html"
}
// Check if file exists in embedded FS
if f, err := distSub.Open(path); err == nil {
f.Close()
fileServer.ServeHTTP(w, r)
return
}
// Fallback to index.html for SPA routing
indexFile, err := fs.ReadFile(distSub, "index.html")
if err != nil {
http.Error(w, "index.html not found", http.StatusInternalServerError)
@@ -84,7 +88,7 @@ func main() {
log.Printf("Serving on http://localhost%s", addr)
log.Printf("API proxy -> %s", *apiTarget)
if err := http.ListenAndServe(addr, mux); err != nil {
if err := http.ListenAndServe(addr, r); err != nil {
log.Fatal(err)
os.Exit(1)
}