Subscription Management API
A robust subscription management system built with Go, featuring user authentication, plan management, and subscription tracking.
π Features
-
User Management
- User registration and authentication
- JWT-based authentication with access and refresh tokens
- Password encryption using bcrypt
-
Plan Management
- Create and manage subscription plans
- Flexible plan duration and pricing
- Plan details and description management
-
Subscription Handling
- Subscribe users to plans
- Track subscription status and expiration
- Automatic subscription expiration management
- Subscription statistics and analytics
π οΈ Technology Stack
- Backend Framework: Fiber (Go web framework)
- Database: PostgreSQL
- ORM: GORM
- Authentication: JWT (JSON Web Tokens)
- Configuration: Environment variables via godotenv
- Containerization: Docker and Docker Compose
- API Documentation: Markdown
π Prerequisites
- Go 1.23.4 or higher
- PostgreSQL 12 or higher
- Git
- Docker and Docker Compose (optional)
π Getting Started
You can run this application either using Docker or with a manual setup.
π³ Docker Setup
For running with Docker, see our Docker Setup Guide which includes:
- Quick start instructions
- Development workflow
- Production deployment considerations
- Container management
- Environment configuration
π» Manual Setup
-
Clone the repository
git clone https://github.com/chandra-devs/subscription_app.git
cd subscription_app
-
Install dependencies
go mod download
-
Set up environment variables
cp .env.example .env
Update the .env
file with your configuration:
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=your_database
-
Create the database
CREATE DATABASE your_database;
-
Run the application
go run main.go
The server will start at http://localhost:3000
π Project Structure
SUBSCRIPTION_APP/
βββ config/ # Configuration files
β βββ database.go # Database connection setup
β βββ jwt.go # JWT configuration
βββ controllers/ # Request handlers
β βββ auth_controller.go
β βββ plan_controller.go
β βββ subscription_controller.go
β βββ user_controller.go
βββ handlers/ # Business logic
β βββ subscription_handler.go
βββ models/ # Database models
β βββ subscription.go
β βββ swagger_types.go
β βββ user.go
βββ routes/ # Route definitions
β βββ setup.go
βββ main.go # Application entry point
π Authentication
The API uses JWT for authentication with two types of tokens:
- Access Token: Valid for 24 hours
- Refresh Token: Valid for 7 days
Include the access token in the Authorization header:
Authorization: Bearer <your_access_token>
π API Endpoints
Authentication
POST /api/v1/auth/register
- Register new user
POST /api/v1/auth/login
- User login
Users
GET /api/v1/users
- Get all users
GET /api/v1/users/:id
- Get user by ID
POST /api/v1/users
- Create new user
PUT /api/v1/users/:id
- Update user
DELETE /api/v1/users/:id
- Delete user
Plans
GET /api/v1/plans
- Get all plans
GET /api/v1/plans/:id
- Get plan by ID
POST /api/v1/plans
- Create new plan
Subscriptions
GET /api/v1/subscriptions
- Get all subscriptions
GET /api/v1/subscriptions/user/:userId
- Get user subscriptions
POST /api/v1/subscriptions/subscribe
- Subscribe user to plan
GET /api/v1/subscriptions/stats
- Get subscription statistics
For detailed API documentation, see API Documentation
π§ͺ Running Tests
go test ./...
π Database Schema
Users
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP,
updated_at TIMESTAMP,
deleted_at TIMESTAMP
);
Plans
CREATE TABLE plans (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10,2) NOT NULL,
duration INTEGER NOT NULL,
created_at TIMESTAMP,
updated_at TIMESTAMP,
deleted_at TIMESTAMP
);
Subscriptions
CREATE TABLE subscriptions (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
plan_id INTEGER REFERENCES plans(id),
status VARCHAR(50) NOT NULL,
start_date TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL,
active BOOLEAN DEFAULT true,
created_at TIMESTAMP,
updated_at TIMESTAMP,
deleted_at TIMESTAMP
);
π Security
- Passwords are hashed using bcrypt
- JWT tokens for authentication
- PostgreSQL SSL mode can be enabled
- Input validation on all endpoints
- CORS configuration available
π€ Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
)
- Commit your changes (
git commit -m 'Add some amazing feature'
)
- Push to the branch (
git push origin feature/amazing-feature
)
- Open a Pull Request
π₯ Authors
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments