Skip to content

πŸš€ Next.js microservices in a monorepo w/ Turborepo, demonstrating best practices and modern tech.

License

Notifications You must be signed in to change notification settings

aryanprince/campus-hub

Repository files navigation

304_1x_shots_so

πŸŽ“ Campus Hub

A showcase monorepo of 3 interconnected microservices in a university setting - demonstrating real-world technology and best practices.

Features   ✦   Directory Structure   ✦   Features   ✦   Tech Stack   ✦   Getting Started

Next JS Turborepo TailwindCSS shadcn/ui Drizzle Vercel Railway

CI GitHub Issues GitHub License

πŸ“– Introduction

Campus Hub is a showcase application featuring three Next.js microservices, fully deployed to the cloud for live use. This project includes a Student Portal, a Library Portal, and a Finance Portal, all integrated within a monorepo using Turborepo and communicating via RESTful APIs.

Built to demonstrate my expertise in modern software development, Campus Hub employs a microservices architecture with independent functionality and separate databases, while leveraging a monorepo for shared code and improved DX. It also implements industry best practices, including CI/CD pipelines, design patterns, and robust architecture.

πŸ“¦ Directory Structure

Campus Hub is a monorepo managed by Turborepo. The monorepo contains an apps, packages, and tooling directory, each serving a specific purpose as shown below:

.
β”œβ”€β”€ .github                   # GitHub Actions CI/CD workflows
β”‚    └── workflows            # CI with pnpm cache + shared GH Actions composite workflow step for all jobs
β”‚        β”œβ”€β”€ build            # Build monorepo w/ Turborepo's Remote Cache for fast builds
β”‚        β”œβ”€β”€ e2e-test         # Run E2E tests w/ Playwright & Docker Compose for local DBs
β”‚        β”œβ”€β”€ vercel (deploy)  # Automatically deploy Next.js apps to Vercel on push
β”‚        β”œβ”€β”€ commitlint       # Lint commit messages to Gitmoji standard using commitlint
β”‚        β”œβ”€β”€ lint             # Lints entire monorepo using shared ESLint config
β”‚        β”œβ”€β”€ format           # Formats entire monorepo using Prettier
β”‚        └── typecheck        # Typechecks entire monorepo using shared TS config
β”‚
β”œβ”€β”€ .husky                    # Git pre-commit hooks for commit messages and linting
β”œβ”€β”€ .vscode                   # Recommended extensions and settings for VSCode
β”œβ”€β”€ postman                   # Postman collection for API testing and documentation
β”‚
β”œβ”€β”€ apps                      # Main applications under the monorepo
β”‚    β”œβ”€β”€ student              # Next.js app for the Student Portal
β”‚    β”œβ”€β”€ library              # Next.js app for the Library Portal
β”‚    └── finance              # Next.js app for the Finance Portal
β”‚
β”œβ”€β”€ packages                  # Shared packages used by the apps
β”‚    └── ui                   # Shared UI components (using shadcn/ui)
β”‚
β”œβ”€β”€ tooling                   # Shared configuration setup for the apps and packages
β”‚    β”œβ”€β”€ config-eslint        # Shared, fine-grained, ESLint preset
β”‚    β”œβ”€β”€ config-playwright    # Shared Playwright configuration for E2E tests
β”‚    β”œβ”€β”€ config-prettier      # Shared Prettier configuration
β”‚    β”œβ”€β”€ config-tailwind      # Shared Tailwind CSS configuration
β”‚    β”œβ”€β”€ config-typescript    # Shared TS config all apps under the monorepo
β”‚    └── github-actions       # Shared GitHub Actions composite workflow step for all CI jobs
β”‚
β”œβ”€β”€ docker-compose.yml        # Docker Compose file for local development databases
β”œβ”€β”€ .npmrc                    # NPM configuration file for pnpm workspaces
β”œβ”€β”€ .nvmrc                    # Node Version Manager (nvm/fnm) file for setting a specific Node version in the monorepo
β”œβ”€β”€ commitlint.config.ts      # Commitlint configuration for Gitmoji commit messages
β”œβ”€β”€ pnpm-workspace.yaml       # pnpm workspace config using catalogs for unified dependencies across monorepo
β”œβ”€β”€ renovate.json             # Renovate configuration for automated dependency updates
└── turbo.json                # Turborepo configuration file (includes all tasks)

✨ Features

Student Portal (Live Demo)

  • Student Registration and Authentication: Secure sign-up and log-in for students.
  • Course Enrollment and Management:
    • Browse and enroll in available courses.
    • View enrolled courses and their respective fees.
  • Graduation Status Tracking:
    • Monitor readiness for graduation.
    • Verify that all course fees are paid for eligibility.
  • Profile Management: Edit and update personal details.

Library Portal (Live Demo)

  • Student Verification: Only accessible to students enrolled in at least one course.
  • Library Book Management:
    • View and search for available books with cover art.
    • Borrow books with a default return date of 14 days.
    • Create and manage a saved collection of books for future borrowing.
  • Overdue Fee Calculation:
    • Automatic calculation of fees for overdue books.
    • Display and manage overdue payments.

Finance Portal (Live Demo)

  • Invoice Management:
    • Input and verify invoice numbers for courses or overdue book payments.
    • Display detailed invoice information.
  • Payment Processing: Facilitate payments to clear outstanding dues.

Best Practices Implemented

  • CI/CD Pipelines: Automated testing and deployment processes using GitHub Actions and Vercel.
  • Design Patterns: Applied relevant design patterns for maintainable and scalable code.
  • Monorepo Structure: Turborepo allowed shared code and dependencies across the microservices.
  • RESTful APIs: Seamless communication between microservices.

πŸ› οΈ Tech Stack

Frontend:

  • Next.js (App Router): A React framework for building server-rendered applications.
  • TypeScript: JavaScript with syntax for types. Used across the entire stack for type safety and improved DX.
  • Tailwind CSS + shadcn/ui + Radix UI: Design system for styling the apps. Used for consistent UI components across the microservices.
  • React Query: A data-fetching library for React that provides async data state management and caching.
  • Lucide: Icon pack used for the app icons across the microservices.
  • Geist: Nice fonts from Vercel. Used for all the microservices.

Backend:

  • Next.js (API Routes): Used to build REST API endpoints directly in Next.js. Used to communicate between the microservices.
  • TypeScript: JavaScript with syntax for types. Used across the entire stack for type safety and improved DX.
  • PostgreSQL: Powerful database system. Used to store all the data for the 3 microservices.
  • Drizzle ORM: Fast and type-safe database ORM for TypeScript & Node.js.
  • Lucia: Authentication library for Next.js. Used to handle authentication for the microservices.

Tooling:

  • Turborepo: A monorepo tool that's fast, caches builds (locally, remotely and even across CI), and has great DX.
  • pnpm: Fast, disk space efficient package manager. Works well with Turborepo.
    • pnpm Workspaces: pnpm feature allowing you to manage multiple packages in a monorepo. Keeps a single lockfile for all packages.
  • Docker: Used to containerize the apps and the databases for local development and deployment.
  • Prettier: An opinionated code formatter. Ensures that all code conforms to a consistent style.
    • Prettier Tailwind: A Prettier plugin to format Tailwind CSS classes to a consistent order.
  • ESLint: A tool to identify bad code practices and patterns across the codebase.

Deployment:

  • Vercel: Used for the deployment of the Next.js apps, including all the REST API endpoints as serverless functions.
  • Railway: Preferred database hosting platform for the production databases.
  • GitHub Actions: Used to automate the CI/CD pipeline for the monorepo along with Vercel deployments.

πŸš€ Getting Started

Prerequisites

Ensure you have the following installed first:

  • Node.js (LTS version recommended)
brew install node # macOS or Linux (using Homebrew)
winget install -e --id OpenJS.NodeJS # Windows
brew install pnpm # macOS or Linux (using Homebrew)
winget install -e --id pnpm.pnpm # Windows
# or using npm
npm install -g pnpm # Install pnpm globally
  • Docker (only for local DBs during development)
brew install --cask docker # macOS or Linux (using Homebrew)
winget install -e --id Docker.DockerDesktop # Windows
npm install -g turbo # Either install Turbo globally using npm
pnpm install -g turbo # or using pnpm (recommended)

Installation

  1. Clone the repository:
git clone https://github.com/aryanprince/campus-hub.git
  1. Install the dependencies:
pnpm install
  1. Create the .env file for each app:
cp apps/student/.env.example apps/student/.env
cp apps/library/.env.example apps/library/.env
cp apps/finance/.env.example apps/finance/.env
  1. Configure environment variables for each app. Stick to the defaults for the most part.

  2. Start the local databases using Docker:

docker-compose up -d
  1. Run script to setup the databases with existing migrations and seed data:
pnpm db:setup
  1. Start the development server using Turborepo:
pnpm dev

and open http://localhost:3001 (Student), http://localhost:3002 (Library), and http://localhost:3003 (Finance) with your browser πŸš€

  1. (Optional) Run the following command to open Drizzle Studio to view the database content from the browser:
pnpm db:studio

and open https://local.drizzle.studio?port=3011 (Student DB), https://local.drizzle.studio?port=3012 (Library DB), and https://local.drizzle.studio?port=3013 (Finance DB) with your browser πŸ—ƒοΈ

πŸ“œ Scripts

Database Scripts

Run the following scripts to manage the database schema and data:

db:studio # Opens Drizzle Studio to view stuff in the database. So cool!
db:push # Pushes database schema directly w/o migrations (prefer db:migrate)
db:migrate:generate # Generates the migration files based on the schema changes
db:migrate:run # Migrates the database using the generated migration files (from db:generate)
db:migrate:push # Generates and runs the migration files in one go (db:generate + db:migrate)
db:seed # Seeds the database with initial data
db:setup # Sets up the database by running db:migrate and db:seed

πŸ“ Note

Developed as part of a university project, Campus Hub highlights my ability to create production-ready applications using the latest technologies and standards. The project is a testament to my skills in software development, showcasing my expertise in building scalable, maintainable, and secure applications. I hope you enjoy exploring Campus Hub and appreciate the effort that went into creating it.