e-commerce-api

command module
v0.0.0-...-04b1328 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 9 Imported by: 0

README ΒΆ

E-Commerce API

This is a Go-based e-commerce backend API built with the Gin framework and GORM for ORM. It is designed to be backend-only, RESTful, and optionally Stripe-integrated. You can use it for web or mobile storefronts as a secure and modular API service.


πŸ”§ Tech Stack

  • Go (Gin + GORM)
  • PostgreSQL (Neon or local)
  • JWT Auth
  • Stripe Payments
  • Zod (for shared schema validation)
  • Redis (for rate limiting + caching)

πŸ“¦ Features

  • βœ… User authentication (signup, login, JWT)
  • πŸ›’ Cart and cart items per user
  • πŸ“¦ Product, category, and order management
  • πŸ’³ Stripe Checkout integration
  • πŸ” Auth middleware (with claims)
  • βœ… Zod schema validation (extra layer on frontend/backend if needed)
  • 🚦 Rate limiting middleware backed by Redis for enhanced security and scalability
  • 🧠 Optional reCAPTCHA validation for signup/login to prevent bot activity
  • πŸ“§ Email sending with Mailtrap (used for signup/order confirmations)
  • πŸš€ Redis caching integrated for product, category, order, and cart reads
  • πŸ–ΌοΈ Image/file upload via Cloudinary (used for product images)
  • πŸ“Š Admin dashboard-ready routes like /admin/orders/stats
  • πŸ”” Stripe webhook endpoint for async payment updates
  • πŸ‘₯ Role-based access control for admin-only features
  • 🧾 Invoice generation and email delivery (PDF attached or link)
  • πŸ—ƒοΈ Invoice record logging in PostgreSQL
  • πŸ“ˆ Admin invoice reporting via /admin/invoices and /admin/invoices/stats

πŸ—‚ Folder Structure

e-commerce-api/
β”œβ”€β”€ controllers/     # HTTP handlers for routes
β”œβ”€β”€ initializers/    # DB, Redis, and env config
β”œβ”€β”€ middleware/      # JWT, rate limiting, admin role checks
β”œβ”€β”€ models/          # GORM models
β”œβ”€β”€ utils/           # Reusable utilities (cache, mail, recaptcha, etc.)
β”œβ”€β”€ validators/      # Zod schemas (for optional validation)
β”œβ”€β”€ main.go          # Application entry point
β”œβ”€β”€ go.mod
β”œβ”€β”€ .env.example     # Sample env vars
└── README.md

πŸ“Œ Setup Instructions

1. Clone the repo
git clone https://github.com/loid-lab/e-commerce-api.git
cd e-commerce-api
2. Setup environment
cp .env.example .env

Update .env with your configuration values:

  • For Neon (cloud Postgres):

    DB_URL=postgres://user:password@ep-xxx.neon.tech:5432/dbname
    SECRET=your_jwt_secret
    STRIPE_SECRET_KEY=sk_test_...
    REDIS_URL=redis://redis:6379
    SMTP_HOST=smtp.mailtrap.io
    SMTP_PORT=587
    SMTP_USER=your_username
    SMTP_PASS=your_password
    MAIL_FROM=your@email.com
    MAIL_TO=receiver@email.com
    CLOUDINARY_CLOUD_NAME=your_cloud_name
    CLOUDINARY_API_KEY=your_api_key
    CLOUDINARY_API_SECRET=your_api_secret
    
  • For local Postgres (optional, see Docker Compose below):

    DB_URL=postgres://postgres:example@localhost:6379/ecommercedb?sslmode=disable
    SECRET=your_jwt_secret
    STRIPE_SECRET_KEY=sk_test_...
    REDIS_URL=redis://localhost:6379
    SMTP_HOST=smtp.mailtrap.io
    SMTP_PORT=587
    SMTP_USER=your_username
    SMTP_PASS=your_password
    MAIL_FROM=your@email.com
    MAIL_TO=receiver@email.com
    CLOUDINARY_CLOUD_NAME=your_cloud_name
    CLOUDINARY_API_KEY=your_api_key
    CLOUDINARY_API_SECRET=your_api_secret
    

Note:
This project uses Redis for rate limiting and caching. Set REDIS_URL and ensure Redis is running locally or remotely.


3. Running with Docker Compose
Using Neon (cloud DB)
version: '3.8'
services:
  api:
    build: .
    ports:
      - "8080:8080"
    environment:
      DB_URL: ${DB_URL}
      SECRET: ${SECRET}
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
      STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET}
      REDIS_URL: ${REDIS_URL}
      SMTP_HOST: ${SMTP_HOST}
      SMTP_PORT: ${SMTP_PORT}
      SMTP_USER: ${SMTP_USER}
      SMTP_PASS: ${SMTP_PASS}
      MAIL_FROM: ${MAIL_FROM}
      CLOUDINARY_CLOUD_NAME: ${CLOUDINARY_CLOUD_NAME}
      CLOUDINARY_API_KEY: ${CLOUDINARY_API_KEY}
      CLOUDINARY_API_SECRET: 
      ${CLOUDINARY_API_SECRET}
    restart: unless-stopped
docker-compose up --build
Using local Postgres + Redis

Add db and redis services in docker-compose.yaml:

  db:
    image: postgres:15-alpine
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: example
      POSTGRES_DB: ecommercedb
    ports:
      - "5432:5432"

  redis:
    image: redis:7-alpine
    restart: always
    ports:
      - "6379:6379"

πŸ” Authentication

  • POST /auth/signup β€” Register new user
  • POST /auth/login β€” Login and receive JWT
  • Authenticated routes require Authorization: Bearer <token>
  • Admin-only routes are protected using role-based middleware. A User must have a Role field set to "admin" to access them.

🧠 reCAPTCHA Support

Signup/login support reCAPTCHA v2/v3. Send recaptchaToken in form payload.


πŸ’³ Payments (Stripe)

  • POST /user/orders/:id/pay β€” Initiate Stripe checkout session
  • POST /webhooks/stripe β€” Stripe webhook endpoint to update order/payment statuses asynchronously

🧊 Redis Caching

Improves performance for heavy-read routes:

  • πŸ” Products: GET /products, GET /products/:id
  • πŸ” Orders: GET /orders, GET /orders/:id
  • πŸ” Categories: GET /categories
  • πŸ” Carts: GET /cart

Auto invalidation after create/update/delete where applicable.


πŸ›  Zod Validation

Used optionally for frontend/backend data agreement via validators/.

🧾 Invoicing & Reporting

This project includes built-in support for invoicing and admin reporting.

  • βœ… Automatically generate PDF invoices when an order is created
  • βœ… Send invoice via email using configured SMTP credentials
  • βœ… Store invoice records and line items in PostgreSQL
  • βœ… Admin dashboard endpoints:
    • GET /admin/invoices β€” view all invoices
    • GET /admin/invoices/stats β€” aggregated reporting
  • βœ… Optional support for Cloudinary or S3-based PDF storage

πŸ“˜ License

MIT β€” feel free to use, modify, or contribute.

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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