subscription_app

command module
v0.0.0-...-33c0e0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2024 License: MIT Imports: 11 Imported by: 0

README ΒΆ

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
  1. Clone the repository

    git clone https://github.com/chandra-devs/subscription_app.git
    cd subscription_app
    
  2. Install dependencies

    go mod download
    
  3. 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
    
  4. Create the database

    CREATE DATABASE your_database;
    
  5. 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

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ‘₯ Authors

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
auth_controller.go
auth_controller.go
models/subscription.go
models/subscription.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL