Compare commits

...

2 commits

Author SHA1 Message Date
8a11a1d5ea
add pending path
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-06-14 22:29:35 +02:00
5a5312097d
add cors handle
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-06-14 22:29:22 +02:00
3 changed files with 70 additions and 7 deletions

View file

@ -477,6 +477,29 @@ Liste les positions détaillées (par instrument) du compte jour par jour.
--- ---
### `GET /snapshots/pending`
Liste tous les comptes de l'owner ayant une invalidation en attente (i.e. snapshots non à jour suite à une mutation de transaction).
Utile pour savoir quels comptes doivent être recalculés avant de consulter leurs snapshots, ou pour déclencher un `recompute` ciblé.
**Réponse `200`** — liste vide si tout est à jour
```json
[
{ "account_id": 1, "recompute_from": "2026-06-10" },
{ "account_id": 3, "recompute_from": "2026-05-01" }
]
```
| Champ | Description |
|---|---|
| `account_id` | Identifiant du compte à recalculer |
| `recompute_from` | Date la plus ancienne depuis laquelle les snapshots sont invalides |
> Les invalidations sont insérées automatiquement à chaque create / update / delete / validate de transaction. Le job `daily-snapshot` les consomme toutes les 24h ; cet endpoint permet de les inspecter sans attendre.
---
### `POST /accounts/{id}/snapshots/recompute` ### `POST /accounts/{id}/snapshots/recompute`
Recalcule immédiatement les snapshots d'un compte depuis sa **date d'invalidation** jusqu'à `today + horizon`. Recalcule immédiatement les snapshots d'un compte depuis sa **date d'invalidation** jusqu'à `today + horizon`.

View file

@ -14,7 +14,8 @@ type Config struct {
CoinGeckoKey string CoinGeckoKey string
PriceFetchInterval time.Duration PriceFetchInterval time.Duration
PriceCleanInterval time.Duration PriceCleanInterval time.Duration
SnapshotHorizonDays int // nombre de jours dans le futur couverts par les snapshots SnapshotHorizonDays int
CORSAllowedOrigins string // "*" ou liste comma-séparée d'origines
} }
func Load() *Config { func Load() *Config {
@ -37,6 +38,7 @@ func Load() *Config {
PriceFetchInterval: fetchInterval, PriceFetchInterval: fetchInterval,
PriceCleanInterval: 24 * time.Hour, PriceCleanInterval: 24 * time.Hour,
SnapshotHorizonDays: horizonDays, SnapshotHorizonDays: horizonDays,
CORSAllowedOrigins: getenv("CORS_ALLOWED_ORIGINS", "*"),
} }
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"log/slog" "log/slog"
"net/http" "net/http"
"strings"
"git.g3e.fr/H6N/account/internal/auth" "git.g3e.fr/H6N/account/internal/auth"
"git.g3e.fr/H6N/account/internal/config" "git.g3e.fr/H6N/account/internal/config"
@ -33,12 +34,49 @@ func New(cfg *config.Config, pool *pgxpool.Pool, logger *slog.Logger) *Server {
} }
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// /health est exempt d'auth. Toutes les autres routes passent par le middleware owner. s.corsMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/health" { if r.URL.Path == "/health" {
s.mux.ServeHTTP(w, r) s.mux.ServeHTTP(w, r)
return return
} }
auth.Middleware(s.mux).ServeHTTP(w, r) auth.Middleware(s.mux).ServeHTTP(w, r)
})).ServeHTTP(w, r)
}
func (s *Server) corsMiddleware(next http.Handler) http.Handler {
allowedOrigins := strings.Split(s.cfg.CORSAllowedOrigins, ",")
wildcard := len(allowedOrigins) == 1 && strings.TrimSpace(allowedOrigins[0]) == "*"
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin != "" {
allowed := wildcard
if !allowed {
for _, o := range allowedOrigins {
if strings.TrimSpace(o) == origin {
allowed = true
break
}
}
}
if allowed {
if wildcard {
w.Header().Set("Access-Control-Allow-Origin", "*")
} else {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Add("Vary", "Origin")
}
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, X-Owner-ID")
w.Header().Set("Access-Control-Max-Age", "86400")
}
}
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
} }
func (s *Server) routes() { func (s *Server) routes() {