feat(web,server): embed статики SPA + fallback, монтирование в cmd/server

This commit is contained in:
2026-07-03 18:14:18 +07:00
parent 388bf4aeb6
commit bba72cc70f
5 changed files with 100 additions and 1 deletions
+21 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"log"
"net/http"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
@@ -14,6 +15,7 @@ import (
"github.com/vasyakrg/dns-autoresolver/internal/provider/selectel"
"github.com/vasyakrg/dns-autoresolver/internal/service"
"github.com/vasyakrg/dns-autoresolver/internal/store"
"github.com/vasyakrg/dns-autoresolver/internal/web"
)
func main() {
@@ -42,9 +44,27 @@ func main() {
svc := service.New(st, st, reg, cipher)
a := &api.API{Svc: svc, Store: st, Cipher: cipher, Reg: reg}
apiRouter := api.NewRouter(a)
webHandler, err := web.Handler()
if err != nil {
log.Printf("web: static UI unavailable: %v", err)
}
mux := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/") {
apiRouter.ServeHTTP(w, r)
return
}
if webHandler != nil {
webHandler.ServeHTTP(w, r)
return
}
http.NotFound(w, r)
})
log.Printf("listening on %s", cfg.ListenAddr)
if err := http.ListenAndServe(cfg.ListenAddr, api.NewRouter(a)); err != nil {
if err := http.ListenAndServe(cfg.ListenAddr, mux); err != nil {
log.Fatal(err)
}
}