Add backend status timestamp endpoint

This commit is contained in:
2026-07-20 17:13:39 +08:00
parent 875b326ddc
commit c64daa8657
2 changed files with 34 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package httpx
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"senlinai-agent/backend/internal/config"
@@ -33,6 +34,9 @@ func newRouter(cfg config.Config, tokenVerifier func(string) (uint, error), regi
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
api := router.Group("/api")
api.GET("/status", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"timestamp_ms": time.Now().UnixMilli()})
})
if tokenVerifier != nil {
api.Use(auth.RequireUser(tokenVerifier))
}

View File

@@ -1,9 +1,11 @@
package httpx
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
@@ -32,6 +34,19 @@ func TestNewRouterRegistersFeatureRoutesUnderAPI(t *testing.T) {
require.JSONEq(t, `{"pong":true}`, rec.Body.String())
}
func TestAPIStatusReturnsChangingTimestampMillisecondsWithoutAuth(t *testing.T) {
router := NewProtectedRouter(config.Config{Env: "test"}, func(token string) (uint, error) {
return 0, http.ErrNoCookie
})
first := getStatusTimestamp(t, router)
time.Sleep(2 * time.Millisecond)
second := getStatusTimestamp(t, router)
require.Greater(t, first, int64(0))
require.Greater(t, second, first)
}
func TestRouterAddsCORSHeadersForLocalWebClient(t *testing.T) {
router := NewRouter(config.Config{Env: "test"}, testRegistrar{})
req := httptest.NewRequest(http.MethodGet, "/api/ping", nil)
@@ -69,3 +84,18 @@ func (testRegistrar) Register(router gin.IRouter) {
c.JSON(http.StatusOK, gin.H{"pong": true})
})
}
func getStatusTimestamp(t *testing.T, router http.Handler) int64 {
t.Helper()
req := httptest.NewRequest(http.MethodGet, "/api/status", nil)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
var payload struct {
TimestampMS int64 `json:"timestamp_ms"`
}
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &payload))
return payload.TimestampMS
}