Skip to content

Repository files navigation

πŸŽ“ Motzklist Backend

The backend service for the Motzklist project β€” a school equipment shopping platform built with Go. This API gateway manages schools, grades, equipment catalogs, user authentication, shopping carts, and payment processing through Stripe integration.

Stack: Go 1.25.4 | PostgreSQL | Stripe API | Docker


πŸ“‹ Table of Contents


🎯 Overview

Motzklist Backend is a RESTful API service that powers the Motzklist web platform. It provides endpoints for:

  • School & Grade Management: Retrieve and manage educational institutions and grade levels
  • Equipment Catalogs: Browse required equipment for different schools and grades
  • User Authentication: Session-based authentication with secure login/logout
  • Shopping Cart: Save and manage shopping lists
  • Payment Processing: Stripe integration for secure checkout and order management
  • Admin Panel: Manage schools, grades, equipment, and view payment history

All responses support multi-language localization (English & Hebrew) via the lang query parameter.


✨ Features

  • βœ… RESTful API with JSON request/response format
  • βœ… Multi-language Support β€” Localized content for English and Hebrew
  • βœ… User Authentication β€” Session-based authentication with secure cookies
  • βœ… Shopping Cart Management β€” Persistent cart storage per user
  • βœ… Stripe Integration β€” Secure payment processing and refund management
  • βœ… Admin Endpoints β€” Full CRUD operations for schools, grades, and equipment
  • βœ… CORS Support β€” Configurable cross-origin requests
  • βœ… Docker Containerization β€” Easy deployment with Docker & Docker Compose
  • βœ… PostgreSQL Database β€” Relational database with multi-language support
  • βœ… Unit Tests β€” Comprehensive test coverage for core functionality

πŸ“ Project Structure

.
β”œβ”€β”€ main.go                  # Application entry point, HTTP routing
β”œβ”€β”€ main_test.go            # Unit tests for all handlers
β”œβ”€β”€ db_api.go               # Database operations (schools, grades, equipment)
β”œβ”€β”€ user_handlers.go        # Authentication endpoints (login, logout, auth status)
β”œβ”€β”€ class_handlers.go       # School/Grade/Equipment endpoints
β”œβ”€β”€ cart_handlers.go        # Shopping cart endpoints
β”œβ”€β”€ payment_handlers.go     # Stripe checkout session creation
β”œβ”€β”€ stripe_handlers.go      # Stripe webhook handling and admin payment endpoints
β”œβ”€β”€ admin_handlers.go       # Admin CRUD operations for schools, grades, equipment
β”œβ”€β”€ mock_db.go              # Mock data for development/testing
β”œβ”€β”€ go.mod                  # Go module dependencies
β”œβ”€β”€ go.sum                  # Go module checksums
β”œβ”€β”€ Makefile                # Build and test commands
β”œβ”€β”€ Dockerfile              # Docker image configuration
β”œβ”€β”€ docker-compose.yml      # PostgreSQL + Backend orchestration
β”œβ”€β”€ SCHEMA.md               # Complete API schema documentation
└── LICENSE                 # License file

πŸš€ Getting Started

Prerequisites

  • Go 1.25.4+ β€” Download
  • PostgreSQL 13+ β€” For database
  • Stripe Account β€” For payment processing (optional for development)

Local Development

  1. Clone the repository:

    git clone https://github.com/Motzklist/Back-End.git
    cd Back-End
  2. Download dependencies:

    make deps
  3. Set up environment variables: Create a .env file in the project root (or export these variables):

    # Database
    DATABASE_URL=postgres://user:password@localhost:5432/motzklist
    
    # Frontend
    CLIENT_ORIGIN=http://localhost:3000
    
    # Stripe (optional)
    STRIPE_SECRET_KEY=sk_test_...
    STRIPE_WEBHOOK_SECRET=whsec_...
    FRONTEND_URL=http://localhost:3000
  4. Set up the PostgreSQL database:

    CREATE DATABASE motzklist;
    -- Run schema migrations (see SCHEMA.md for table definitions)
  5. Run the server:

    make run

    The API will be available at http://localhost:8080

Docker Setup

  1. Start services with Docker Compose:

    docker-compose up --build

    This starts:

    • PostgreSQL database (port 5432)
    • Go backend service (port 8080)
  2. Verify the service:

    curl http://localhost:8080/api/schools

πŸ“š API Documentation

See SCHEMA.md for the complete API specification.

Example Endpoints

Get Schools:

GET /api/schools?lang=en

Get Grades for a School:

GET /api/grades?school_id=1&lang=en

Get Equipment List:

GET /api/equipment?school_id=1&grade_id=9&lang=en

User Login:

POST /api/login
Content-Type: application/json

{
  "username": "avner",
  "password": "2004"
}

Get Shopping Cart:

GET /api/cart?userid=1&lang=en

Create Stripe Checkout Session:

POST /api/checkout
Content-Type: application/json

{
  "userId": "1",
  "gradeId": "9",
  "items": [
    {
      "equipmentId": "101",
      "name": "Notebook",
      "quantity": 5,
      "amount": 250
    }
  ]
}

Admin: List Schools (requires authentication):

GET /api/admin/schools
Cookie: sessionid=...

πŸ—„οΈ Database Schema

The database includes the following tables:

  • school β€” Educational institutions with multi-language names
  • grade β€” Grade levels (9-12) with multi-language names
  • equipment β€” Product catalog with pricing and descriptions
  • requirement β€” Links equipment to specific grade/school combinations
  • users β€” User credentials (username, password, user ID)
  • cart β€” Persistent shopping carts per user
  • orders β€” Purchase history with order details
  • order_item β€” Line items within orders (from Stripe webhooks)

See SCHEMA.md for detailed table definitions and relationships.


βš™οΈ Configuration

Environment Variables

Variable Required Description
DATABASE_URL βœ… PostgreSQL connection string
CLIENT_ORIGIN βœ… Frontend origin for CORS (e.g., http://localhost:3000)
STRIPE_SECRET_KEY ❌ Stripe API secret key
STRIPE_WEBHOOK_SECRET ❌ Stripe webhook signing secret
FRONTEND_URL ❌ Frontend URL for Stripe success/cancel redirects

Database Connection

Update DATABASE_URL in your .env:

DATABASE_URL=postgres://username:password@localhost:5432/motzklist

CORS Configuration

The CLIENT_ORIGIN environment variable accepts comma-separated origins:

CLIENT_ORIGIN=http://localhost:3000,https://motzklist.com

πŸ§ͺ Testing

Run the test suite:

make test

To run tests with verbose output:

go test -v ./...

Test Coverage:

  • Authentication (login, logout, session management)
  • Shopping cart operations (GET, POST, validation)
  • Equipment and grade retrieval
  • Error handling and input validation

Some tests require a seeded test database and are marked with t.Skip(). To enable them, set up a test database and remove the skip statements.


🐳 Deployment

Docker Build

Build the image:

docker build -t motzklist-backend:latest .

Run the container:

docker run -p 8080:8080 \
  -e DATABASE_URL="postgres://..." \
  -e CLIENT_ORIGIN="https://motzklist.com" \
  -e STRIPE_SECRET_KEY="sk_..." \
  motzklist-backend:latest

Docker Compose

Deploy the full stack:

docker-compose up -d

View logs:

docker-compose logs -f backend

πŸ› οΈ Build Commands

Command Description
make run Start the development server
make build Compile the binary to ./build/motzklist-backend
make clean Remove the build directory
make deps Download Go dependencies
make test Run the test suite

πŸ“ Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/your-feature)
  3. Make changes and add tests
  4. Run tests (make test) to ensure everything passes
  5. Commit with clear messages (git commit -am 'Add feature')
  6. Push to your fork and create a Pull Request

Code Standards

  • Follow Go naming conventions and idioms
  • Add tests for new functionality
  • Update SCHEMA.md if API changes are made
  • Ensure CORS and multi-language support for new endpoints

πŸ“„ License

This project is licensed under the LICENSE file in this repository.


πŸ™‹ Support

For issues, questions, or suggestions:

  • Open an issue on GitHub
  • Check SCHEMA.md for API documentation
  • Review test files for usage examples

Happy coding! πŸš€

About

The backend of the Motzklist project

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages