Files
dns-autoresolver/cmd/server/main.go
T
vasyansk 4140847a15 fix(web,server): плейсхолдер dist для воспроизводимой сборки + /api без слэша → API
Коммитим internal/web/dist/index.html как минимальный плейсхолдер, чтобы
//go:embed all:dist находил совпадения на чистом клоне без npm/`make web`
(CRITICAL: go build ./... падал с "pattern all:dist: no matching files
found"). .gitignore теперь игнорирует только реальные build-ассеты
(internal/web/dist/* кроме index.html); `make web` перезаписывает
плейсхолдер настоящей сборкой.

Также чинит MEDIUM: голый /api (без хвостового слэша) уходил в
SPA-fallback вместо API-роутера — вынесен isAPIPath() с явной проверкой
path == "/api", покрыт TestIsAPIPath.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BwxdSt4reTm7Dj1oxRvpP3
2026-07-03 18:24:24 +07:00

79 lines
2.0 KiB
Go

package main
import (
"context"
"log"
"net/http"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/vasyakrg/dns-autoresolver/internal/api"
"github.com/vasyakrg/dns-autoresolver/internal/config"
"github.com/vasyakrg/dns-autoresolver/internal/crypto"
"github.com/vasyakrg/dns-autoresolver/internal/provider/registry"
"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"
)
// isAPIPath reports whether path must be routed to the API router rather
// than the SPA. "/api" (no trailing slash) counts as an API path too —
// only strings.HasPrefix(path, "/api/") would otherwise miss it and fall
// through to the SPA fallback.
func isAPIPath(path string) bool {
return path == "/api" || strings.HasPrefix(path, "/api/")
}
func main() {
ctx := context.Background()
cfg, err := config.Load()
if err != nil {
log.Fatalf("config: %v", err)
}
if err := store.Migrate(ctx, cfg.DBDSN); err != nil {
log.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, cfg.DBDSN)
if err != nil {
log.Fatalf("pool: %v", err)
}
defer pool.Close()
cipher, err := crypto.NewCipher(cfg.EncKey)
if err != nil {
log.Fatalf("cipher: %v", err)
}
st := store.New(pool)
reg := registry.New()
reg.Register(selectel.New())
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 isAPIPath(r.URL.Path) {
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, mux); err != nil {
log.Fatal(err)
}
}