Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

๐Ÿš€ Modern TypeScript backend for web games with Fastify, tRPC & Prisma. Features magic link auth, type-safe APIs, and game-ready architecture.

License

Notifications You must be signed in to change notification settings

tobenot/Basic-Web-Game-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

121 Commits

Repository files navigation

Basic Web Game Backend

License: MIT TypeScript Fastify tRPC Prisma

A modern, type-safe backend for web games built with Fastify, tRPC, and Prisma. Features secure authentication with magic links and a robust API architecture.

โœจ Features

  • ๐Ÿ” Magic Link Authentication - Secure, passwordless login system
  • ๐Ÿš€ Fastify Server - High-performance Node.js web framework
  • ๐Ÿ”— tRPC Integration - End-to-end type safety
  • ๐Ÿ—„๏ธ Prisma ORM - Type-safe database operations
  • ๐Ÿ“ฑ CORS Support - Cross-origin resource sharing enabled
  • ๐ŸŽฎ Game-Ready API - Built specifically for web game backends
  • ๐Ÿ“Š User Management - Complete user lifecycle management
  • ๐Ÿ“ข Announcement System - Built-in announcement functionality

๐Ÿ› ๏ธ Tech Stack

  • Runtime: Node.js
  • Framework: Fastify
  • API: tRPC
  • Database: Prisma ORM (SQLite/PostgreSQL)
  • Language: TypeScript
  • Authentication: JWT + Magic Links
  • Email: Resend

๐Ÿš€ Quick Start

Prerequisites

  • Node.js 22+
  • npm or yarn
  • Database (SQLite for development, PostgreSQL for production)

Installation

  1. Clone the repository

    git clone https://github.com/tobenot/Basic-Web-Game-Backend.git
    cd Basic-Web-Game-Backend
  2. Install dependencies

    npm install
  3. Set up environment variables

    cp .env.example .env

    Configure your .env file:

    DATABASE_URL="file:./dev.db"
    JWT_SECRET="your-secret-key"
    RESEND_API_KEY="your-resend-api-key"
    EMAIL_FROM="noreply@sendmail.tobenot.top"
    EMAIL_FROM_NAME="YourApp"
  4. Generate Prisma schema and run migrations

    npm run migrate:dev
  5. Start the development server

    npm run dev

The server will be available at http://localhost:3000

๐Ÿ“š API Documentation

Authentication Endpoints

  • POST /api/trpc/auth.sendMagicLink - Send magic link to user email
  • POST /api/trpc/auth.verifyToken - Verify magic link token
  • GET /api/trpc/auth.me - Get current user info

Magic link generation:

  • Uses frontend URL (FRONTEND_LOCAL_URL / FRONTEND_PRODUCTION_URL) as base
  • Appends token as a query param
  • If the frontend URL contains a hash route, the query is inserted before the hash so the frontend can read it

User Management

  • GET /api/trpc/user.profile - Get user profile
  • PUT /api/trpc/user.update - Update user information

Announcements

  • GET /api/trpc.announcement.list - Get announcement list
  • POST /api/trpc.announcement.create - Create new announcement

๐Ÿ—„๏ธ Database Schema

The application uses Prisma with the following models:

  • User: Core user information and authentication
  • AuthToken: Magic link tokens for passwordless authentication

๐Ÿงช Development

Available Scripts

# Development
npm run dev # Start development server
npm run build # Build for production
npm run studio # Open Prisma Studio
# Database
npm run migrate:dev # Run migrations in development
npm run migrate:prod # Deploy migrations to production
# Utilities
npm run prisma:generate:schema # Generate Prisma schema
npm run copy-templates # Copy template files

ๅ‘ๅธƒๅˆฐ GitHub Packages

  1. ๆ‰‹ๅŠจ่งฆๅ‘ๅทฅไฝœๆต Update Version,้€‰ๆ‹ฉ patch|minor|major
  2. ๆตๆฐด็บฟๅฐ†ๆไบค็‰ˆๆœฌๆ›ดๆ–ฐ,้šๅŽ่‡ชๅŠจ่งฆๅ‘ Publish API Contract ๅฎŒๆˆๅ‘ๅธƒ
  3. ้ชŒ่ฏ็‰ˆๆœฌ:
    npm view @tobenot/basic-web-game-backend-contract version --registry=https://npm.pkg.github.com
  4. ๅ‰็ซฏๅฎ‰่ฃ…:
    yarn add @tobenot/basic-web-game-backend-contract@latest

ๅค‡็”จ:ๆœฌๅœฐ็›ดๆŽฅๅ‘ๅธƒ

npm login --scope=@tobenot --registry=https://npm.pkg.github.com
npm run build
npm version patch --no-git-tag-version
npm publish --registry=https://npm.pkg.github.com

Project Structure

src/
โ”œโ”€โ”€ ai/ # AI service & queue
โ”‚ โ”œโ”€โ”€ AiService.ts
โ”‚ โ”œโ”€โ”€ AiRequestQueueService.ts
โ”‚ โ””โ”€โ”€ types.ts
โ”œโ”€โ”€ routers/ # tRPC/Fastify routers
โ”‚ โ”œโ”€โ”€ auth.ts
โ”‚ โ”œโ”€โ”€ user.ts
โ”‚ โ”œโ”€โ”€ announcement.ts
โ”‚ โ””โ”€โ”€ llm-proxy.ts
โ”œโ”€โ”€ utils/ # Utilities (e.g. LLM upstream client)
โ”‚ โ””โ”€โ”€ llm-client.ts
โ”œโ”€โ”€ templates/ # Email templates
โ”œโ”€โ”€ server.ts # Fastify server setup
โ””โ”€โ”€ trpc.ts # tRPC configuration

๐Ÿš€ Deployment

Vercel Deployment

This project is configured for Vercel deployment. The vercel.json file contains the necessary configuration.

Environment Variables for Production

DATABASE_URL="postgresql://..."
JWT_SECRET="your-production-secret"
RESEND_API_KEY="your-resend-api-key"

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Guidelines

  1. Fork the repository
  2. Create a 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

๐Ÿ“„ License

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

๐Ÿ™ Acknowledgments

  • Built with Fastify for high-performance web applications
  • Powered by tRPC for end-to-end type safety
  • Database management with Prisma
  • Email delivery via Resend

๐Ÿ“ž Support

If you have any questions or need help, please:


Made with โค๏ธ by tobenot

This project is maintained as an independent open-source effort to provide a solid foundation for web game backends.

Template Updates

  • Use the bundled script to preview/apply template updates:
    • Preview latest: bash scripts/update-from-template.sh
    • Apply a specific tag with backup: APPLY=1 bash scripts/update-from-template.sh vX.Y.Z
  • The template source and version are recorded in template.lock.
  • Install a non-blocking pre-commit reminder:
    bash scripts/install-git-hooks.sh
  • Controlled areas (prefer updating via template): .github/, scripts/, src/framework/, template-marked blocks in src/server.ts, tsconfig.json, vercel.json, and docs listed in MIGRATION.md.

Releases

No releases published

Packages

Contributors 3

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /