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.
- ๐ 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
- Runtime: Node.js
- Framework: Fastify
- API: tRPC
- Database: Prisma ORM (SQLite/PostgreSQL)
- Language: TypeScript
- Authentication: JWT + Magic Links
- Email: Resend
- Node.js 18+
- npm or yarn
- Database (SQLite for development, PostgreSQL for production)
-
Clone the repository
git clone https://github.com/tobenot/Basic-Web-Game-Backend.git cd Basic-Web-Game-Backend
-
Install dependencies
npm install
-
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"
-
Generate Prisma schema and run migrations
npm run migrate:dev
-
Start the development server
npm run dev
The server will be available at http://localhost:3000
POST /api/trpc/auth.sendMagicLink
- Send magic link to user emailPOST /api/trpc/auth.verifyToken
- Verify magic link tokenGET /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
GET /api/trpc/user.profile
- Get user profilePUT /api/trpc/user.update
- Update user information
GET /api/trpc.announcement.list
- Get announcement listPOST /api/trpc.announcement.create
- Create new announcement
The application uses Prisma with the following models:
- User: Core user information and authentication
- AuthToken: Magic link tokens for passwordless authentication
# 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
- ๆๅจ่งฆๅๅทฅไฝๆต
Update Version
๏ผ้ๆฉpatch|minor|major
- ๆตๆฐด็บฟๅฐๆไบค็ๆฌๆดๆฐ๏ผ้ๅ่ชๅจ่งฆๅ
Publish API Contract
ๅฎๆๅๅธ - ้ช่ฏ็ๆฌ๏ผ
npm view @tobenot/basic-web-game-backend-contract version --registry=https://npm.pkg.github.com
- ๅ็ซฏๅฎ่ฃ
๏ผ
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
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
This project is configured for Vercel deployment. The vercel.json
file contains the necessary configuration.
DATABASE_URL="postgresql://..."
JWT_SECRET="your-production-secret"
RESEND_API_KEY="your-resend-api-key"
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.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- 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
If you have any questions or need help, please:
- Open an issue
- Check the documentation
Made with โค๏ธ by tobenot
This project is maintained as an independent open-source effort to provide a solid foundation for web game backends.
- 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
- Preview latest:
- 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 insrc/server.ts
,tsconfig.json
,vercel.json
, and docs listed inMIGRATION.md
.