Go (Gin) API backend for Stellabill — subscription and billing plans API. This repo is backend-only; a separate frontend consumes these APIs.
- Tech stack
- What this backend provides (for the frontend)
- Background Worker
- Local setup
- Configuration
- API reference
- Contributing (open source)
- Project layout
- License
- Language: Go 1.22+
- Framework: Gin
- Config: Environment variables (no config files required for default dev)
This service is the backend only. A separate frontend (or any client) can:
- Health check —
GET /api/healthto verify the API is up. - Plans —
GET /api/plansto list billing plans (id, name, amount, currency, interval, description). Currently returns an empty list; DB integration is planned. - Subscriptions —
GET /api/subscriptionsto list subscriptions andGET /api/subscriptions/:idto fetch one. Responses include plan_id, customer, status, amount, interval, next_billing. Currently placeholder/mock data; DB integration is planned.
CORS is enabled for all origins in development so a frontend on another port or domain can call these endpoints.
The backend includes a production-ready background worker system for automated billing job scheduling and execution.
- Job Scheduling: Schedule billing operations (charges, invoices, reminders) with configurable execution times
- Distributed Locking: Prevents duplicate processing when running multiple worker instances
- Retry Policy: Automatic retry with exponential backoff (1s, 4s, 9s) for failed jobs
- Dead-Letter Queue: Failed jobs after max attempts are moved for manual review
- Graceful Shutdown: Workers complete in-flight jobs before shutting down
- Metrics Tracking: Monitor job processing statistics (processed, succeeded, failed, dead-lettered)
- Concurrent Workers: Multiple workers can run safely without duplicate processing
internal/worker/README.md- Complete worker documentationinternal/worker/INTEGRATION.md- Integration guide with examplesinternal/worker/SECURITY.md- Security analysis and threat modelWORKER_IMPLEMENTATION.md- Implementation summary
store := worker.NewMemoryStore()
executor := worker.NewBillingExecutor()
config := worker.DefaultConfig()
w := worker.NewWorker(store, executor, config)
w.Start()
defer w.Stop()
scheduler := worker.NewScheduler(store)
job, _ := scheduler.ScheduleCharge("sub-123", time.Now(), 3)-
Go 1.22 or later
- Check:
go version - Install: https://go.dev/doc/install
- Check:
-
Git (for cloning and contributing)
-
PostgreSQL (optional for now; app runs without it using default config; DB will be used when persistence is added)
git clone https://github.com/YOUR_ORG/stellabill-backend.git
cd stellabill-backendgo mod downloadCreate a .env file in the project root (do not commit it; it’s in .gitignore):
# Optional — defaults shown
ENV=development
PORT=8080
DATABASE_URL=postgres://localhost/stellarbill?sslmode=disable
JWT_SECRET=change-me-in-productionOr export them in your shell. The app will run with the defaults if you don’t set anything.
go run ./cmd/serverServer listens on http://localhost:8080 (or the port you set via PORT).
curl http://localhost:8080/api/health
# Expected: {"service":"stellarbill-backend","status":"ok"}| Variable | Default | Description |
|---|---|---|
ENV |
development |
Environment (e.g. production) |
PORT |
8080 |
HTTP server port |
DATABASE_URL |
postgres://localhost/stellarbill?sslmode=disable |
PostgreSQL connection string |
JWT_SECRET |
change-me-in-production |
Secret for JWT (change in prod) |
In production, set these via your host’s environment or secrets manager; do not commit secrets.
Base URL (local): http://localhost:8080
| Method | Path | Description |
|---|---|---|
| GET | /api/health |
Health check |
| GET | /api/plans |
List billing plans |
| GET | /api/subscriptions |
List subscriptions |
| GET | /api/subscriptions/:id |
Get one subscription |
All JSON responses. CORS allowed for * origin with common methods and headers.
We welcome contributions from the community. Below is a short guide to get you from “first look” to “merged change”.
- Be respectful and inclusive.
- Focus on constructive feedback and clear, factual communication.
-
Open an issue
- Bug: describe what you did, what you expected, and what happened.
- Feature: describe the goal and why it helps.
-
Fork and clone
- Fork the repo on GitHub, then clone your fork locally.
-
Create a branch
git checkout -b fix/your-fix # or feature/your-feature -
Make changes
- Follow existing style (format with
go fmt). - Keep commits logical and messages clear (e.g. “Add validation for plan ID”).
- Follow existing style (format with
-
Run checks
go build ./... go vet ./... go fmt ./...
Add or run tests if the project has them.
-
Commit
- Prefer small, atomic commits (one logical change per commit).
-
Push and open a PR
git push origin fix/your-fix
- Open a Pull Request against the main branch.
- Fill in the PR template (if any).
- Link related issues.
- Describe what you changed and why.
-
Review
- Address review comments. Maintainers will merge when everything looks good.
- Use the Local setup steps to run the server.
- Change code, restart the server (or use a tool like
airfor live reload if the project adds it). - Test with
curlor the frontend that consumes this API.
- Go:
go fmt,go vet, no unnecessary dependencies. - APIs: Keep JSON shape stable; document breaking changes in PRs.
- Secrets: Never commit
.env, keys, or passwords.
stellabill-backend/
├── cmd/
│ └── server/
│ └── main.go # Entry point, Gin router, server start
├── internal/
│ ├── config/
│ │ └── config.go # Loads ENV, PORT, DATABASE_URL, JWT_SECRET
│ ├── handlers/
│ │ ├── health.go # GET /api/health
│ │ ├── plans.go # GET /api/plans
│ │ └── subscriptions.go # GET /api/subscriptions, /api/subscriptions/:id
│ ├── routes/
│ └── routes.go # Registers routes and CORS middleware
│ └── worker/
│ ├── job.go # Job model and JobStore interface
│ ├── store_memory.go # In-memory JobStore implementation
│ ├── worker.go # Background worker with scheduler loop
│ ├── executor.go # Billing job executor
│ ├── scheduler.go # Job scheduling utilities
│ ├── *_test.go # Comprehensive test suite (95%+ coverage)
│ ├── README.md # Worker documentation
│ ├── SECURITY.md # Security analysis and threat model
│ └── INTEGRATION.md # Integration guide with examples
├── go.mod
├── go.sum
├── .gitignore
├── README.md
└── WORKER_IMPLEMENTATION.md # Implementation summary
See the LICENSE file in the repository (if present). If none, assume proprietary until stated otherwise.