Serveur : surveillance des nouvelles versions d'Excalidraw (/api/update)
- Le serveur interroge le registre npm (@excalidraw/excalidraw, dist-tag latest) au démarrage puis toutes les 12 h ; GET /api/update. UPDATE_CHECK=false désactive, NPM_REGISTRY permet un miroir ; sans réseau, la dernière valeur connue est gardée. - Dockerfile/compose : argument de build EXCALIDRAW_VERSION pour reconstruire l'image avec une autre version sans modifier le dépôt. - README : style Excalidraw, import de dossiers, mise à jour d'Excalidraw. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019EuvvuTv7AUc8ksDHv8VKx
This commit is contained in:
1 parent
9ecaab550a
commit
49a816bb12
8 files changed
+225
-7
No files matched your search
@@ -0,0 +1,102 @@
|
||||
// Package update surveille la dernière version publiée de @excalidraw/excalidraw
|
||||
// sur le registre npm. Le frontend la compare à la version embarquée au build et
|
||||
// signale qu'une reconstruction de l'image est possible.
|
||||
package update
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Package suivi sur le registre npm.
|
||||
const Package = "@excalidraw/excalidraw"
|
||||
|
||||
// Status est la réponse de GET /api/update.
|
||||
type Status struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Latest string `json:"latest,omitempty"` // vide tant que le registre n'a pas répondu
|
||||
CheckedAt *time.Time `json:"checkedAt,omitempty"` // dernière interrogation réussie
|
||||
}
|
||||
|
||||
// Checker interroge le registre au démarrage puis à intervalle régulier.
|
||||
// Une erreur réseau est journalisée et la dernière valeur connue conservée :
|
||||
// sans accès Internet, l'interface n'affiche simplement rien.
|
||||
type Checker struct {
|
||||
Registry string // ex. https://registry.npmjs.org
|
||||
Interval time.Duration // ex. 12h
|
||||
Client *http.Client
|
||||
|
||||
mu sync.RWMutex
|
||||
status Status
|
||||
}
|
||||
|
||||
// Run boucle jusqu'à l'annulation de ctx.
|
||||
func (c *Checker) Run(ctx context.Context) {
|
||||
for {
|
||||
c.check(ctx)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(c.Interval):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Checker) check(ctx context.Context) {
|
||||
v, err := c.fetch(ctx)
|
||||
if err != nil {
|
||||
if ctx.Err() == nil {
|
||||
slog.Warn("vérification de mise à jour d'Excalidraw", "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
c.mu.Lock()
|
||||
c.status = Status{Enabled: true, Latest: v, CheckedAt: &now}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *Checker) fetch(ctx context.Context) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.Registry+"/"+Package+"/latest", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
res, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("registre npm : %s", res.Status)
|
||||
}
|
||||
var body struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
|
||||
return "", fmt.Errorf("registre npm : %w", err)
|
||||
}
|
||||
if body.Version == "" {
|
||||
return "", fmt.Errorf("registre npm : version absente")
|
||||
}
|
||||
return body.Version, nil
|
||||
}
|
||||
|
||||
// Status renvoie le dernier état connu. Un Checker nil signifie « désactivé ».
|
||||
func (c *Checker) Status() Status {
|
||||
if c == nil {
|
||||
return Status{}
|
||||
}
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
s := c.status
|
||||
s.Enabled = true
|
||||
return s
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package update
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCheck(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/@excalidraw/excalidraw/latest" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"name":"@excalidraw/excalidraw","version":"0.19.0"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := &Checker{Registry: srv.URL, Interval: time.Hour, Client: srv.Client()}
|
||||
if s := c.Status(); !s.Enabled || s.Latest != "" {
|
||||
t.Fatalf("avant vérification : %+v", s)
|
||||
}
|
||||
c.check(context.Background())
|
||||
if s := c.Status(); s.Latest != "0.19.0" || s.CheckedAt == nil {
|
||||
t.Fatalf("après vérification : %+v", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckKeepsLastValueOnError(t *testing.T) {
|
||||
ok := true
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !ok {
|
||||
http.Error(w, "panne", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"version":"0.18.2"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := &Checker{Registry: srv.URL, Interval: time.Hour, Client: srv.Client()}
|
||||
c.check(context.Background())
|
||||
ok = false
|
||||
c.check(context.Background())
|
||||
if s := c.Status(); s.Latest != "0.18.2" {
|
||||
t.Fatalf("valeur perdue après une erreur : %+v", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilCheckerIsDisabled(t *testing.T) {
|
||||
var c *Checker
|
||||
if s := c.Status(); s.Enabled {
|
||||
t.Fatalf("checker nil : %+v", s)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user