Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions api/handlers/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/hhftechnology/middleware-manager/internal/testutil"
"github.com/hhftechnology/middleware-manager/models"
"github.com/hhftechnology/middleware-manager/services"
)

Expand All @@ -17,6 +19,35 @@ func newTestConfigProxy(t *testing.T) *services.ConfigProxy {
return services.NewConfigProxy(db, cm, "")
}

func newTraefikActiveTestConfigProxy(t *testing.T) (*services.ConfigProxy, func()) {
t.Helper()

db := testutil.NewTempDB(t)
cm := testutil.NewTestConfigManager(t)

traefikServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/version" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]string{
"Version": "3.6.12",
})
}))
if err := cm.UpdateDataSource("traefik", models.DataSourceConfig{
Type: models.TraefikAPI,
URL: traefikServer.URL,
}); err != nil {
t.Fatalf("UpdateDataSource(traefik) failed: %v", err)
}
if err := cm.SetActiveDataSource("traefik"); err != nil {
t.Fatalf("SetActiveDataSource(traefik) failed: %v", err)
}

return services.NewConfigProxy(db, cm, ""), traefikServer.Close
}

// TestNewProxyHandler tests proxy handler creation
func TestNewProxyHandler(t *testing.T) {
configProxy := newTestConfigProxy(t)
Expand Down Expand Up @@ -73,6 +104,48 @@ func TestProxyHandler_GetProxyStatus(t *testing.T) {
}
}

func TestProxyHandler_GetProxyStatus_TraefikActiveHealthy(t *testing.T) {
configProxy, cleanup := newTraefikActiveTestConfigProxy(t)
defer cleanup()
handler := NewProxyHandler(configProxy)

c, rec := testutil.NewContext(t, http.MethodGet, "/api/traefik-config/status", nil)
handler.GetProxyStatus(c)

if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
}

var response map[string]interface{}
json.Unmarshal(rec.Body.Bytes(), &response)

if got := response["status"]; got != "healthy" {
t.Fatalf("expected healthy status, got %#v", got)
}
}

func TestProxyHandler_GetTraefikConfig_TraefikActive(t *testing.T) {
configProxy, cleanup := newTraefikActiveTestConfigProxy(t)
defer cleanup()
handler := NewProxyHandler(configProxy)

c, rec := testutil.NewContext(t, http.MethodGet, "/api/traefik-config", nil)
handler.GetTraefikConfig(c)

if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
}

var response map[string]interface{}
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
t.Fatalf("failed to decode response: %v", err)
}

if response["http"] == nil {
t.Fatal("expected http section in response")
}
}

// TestProxyHandler_GetTraefikConfig tests getting merged config
func TestProxyHandler_GetTraefikConfig(t *testing.T) {
configProxy := newTestConfigProxy(t)
Expand Down
1 change: 1 addition & 0 deletions docs/docs/content/docs/configuration/data-sources.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Middleware Manager can pull routers/services from Pangolin or directly from Trae

- Set via **Settings → Active Data Source** or `config.json` (`active_data_source`).
- Pangolin uses `/api/v1/traefik-config`; Traefik uses `/api/version` checks.
- When `active_data_source` is `traefik`, the config proxy endpoint must operate without requiring Pangolin to be reachable.

## Config file (mounted)

Expand Down
5 changes: 5 additions & 0 deletions docs/docs/content/docs/getting-started/deploy-standalone.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ middleware-manager:
4) Open UI at `http://<host>:3456` and set **Active Data Source → Traefik**.
5) Validate with **Traefik Explorer** (routers/services/middlewares) and a test middleware assignment.

## Notes

- In standalone Traefik mode, `/api/v1/traefik-config` should be served from Middleware Manager's Traefik/standalone path and must not require Pangolin.
- `PANGOLIN_API_URL` may still exist in the container environment, but it should not be consulted while `active_data_source` is `traefik`.

## Port/health

- Middleware Manager: `:3456` (UI/API), `GET /health`.
Expand Down
Loading
Loading