Manager (Backend API)
The manager/ component is the control plane: a Go service (Gin + GORM) that owns users, nodes, plans, orders, traffic, and system config, and exposes both the human-facing REST API and the node data plane.
Build & run
bash
cd manager
go build -o vgate-manager .
./vgate-manager --config config.yml # defaults to ./config.yml; HTTP on server.port (8081)Other commands (cobra subcommands):
bash
vgate-manager admin create --username X --password Y --role admin|super_adminData migrations run automatically on startup (cmd/migrate.go, idempotent). On first start the DB is auto-migrated and an initial admin is bootstrapped from admin.bootstrap in config.yml (default admin / change-me); the generated password is printed only once.
Test & lint
bash
go test ./...
go vet ./... && gofmt -l .Layered structure (internal/)
api/— HTTP layer:router.go(Gin engine + middleware wiring),handler/(one file per resource:admin_node.go,admin_user.go,server.go,user.go,order.go,plan.go, …),dto/(request/response structs). Tests:admin_test.go,server_test.go,sub_test.go, auth-level tests.middleware/—cors.go,jwt.go(user/admin auth),node_auth.go(node token auth),logger.go,ratelimit.go.service/— business logic: auth, user, node, plan, order/billing, traffic, stats, subscription, system_config, email/invite, announcement. Many have_test.go.model/— GORM models.util/— helpers.wire/— dependency wiring.pkg/crypto/— shared crypto (VLESS credential helpers).
Startup sequence (run())
- Load config (viper).
- Open DB (SQLite default via
glebarez/sqlite, or Postgres viadb.dialect/db.dsn). AutoMigrateall GORM models.- Run ordered data migrations.
- Merge DB-backed system-config overrides on top of
config.yml. - Bootstrap the admin.
- Build router via
api.NewRouter. - Start background tickers: expired-order closer (5 min), hourly traffic aggregation, daily quota reset.
API surface (all under /api/v1)
| Group | Endpoints |
|---|---|
| Public / user | /user/login, /sub/:sub_token, profile / subscribe / plans / orders, /billing/alipay/notify |
| Node data plane | GET /server/config, GET /server/users, POST /server/traffic |
| Admin | nodes, users, traffic, stats, system-config, orders, plans, admins |
| Health | GET /health |
Auth
- JWT access + refresh tokens. Login returns both; a
401on a protected endpoint triggers one automatic refresh. - Roles:
adminvssuper_admingate plan/admins endpoints. - Node endpoints use a separate node token (
node_authmiddleware).
Config split
| File / env only | DB-backed (hot-reloadable) |
|---|---|
server.port | jwt TTLs |
db.* | log.level / log.format |
jwt.secret | cors.allowed_origins |
admin.bootstrap.* | timeouts |
viper maps SERVER_PORT-style env vars. See Configuration Reference.
Database
- SQLite by default (
glebarez/sqlite) — zero external dependencies, great for small deployments. - Postgres via
db.dialect: postgresanddb.dsn.
Production notes
- Set
jwt.secretin the config; the bootstrap admin password is shown only once. - Put the manager behind a reverse proxy (nginx/Caddy) for TLS and to serve the frontends.
- Enable
ratelimitmiddleware for public endpoints.