migration to typescript
This commit is contained in:
parent
924000b084
commit
9fde77468a
41 changed files with 11952 additions and 10164 deletions
|
|
@ -1,191 +1,200 @@
|
|||
# E-Learning Backend
|
||||
# E-Learning Platform Backend
|
||||
|
||||
Backend API for the E-Learning Platform built with Node.js, Express, Prisma, and PostgreSQL.
|
||||
Backend API for E-Learning Platform built with TypeScript, Express, TSOA, and Prisma.
|
||||
|
||||
## 🚀 Features
|
||||
|
||||
- **Authentication & Authorization**: JWT-based authentication with role-based access control
|
||||
- **Course Management**: Create, manage, and publish courses with chapters and lessons
|
||||
- **Multi-Language Support**: Thai and English content support
|
||||
- **Quiz System**: Interactive quizzes with multiple attempts and score policies
|
||||
- **Progress Tracking**: Track student progress and issue certificates
|
||||
- **File Upload**: Support for video lessons and attachments (MinIO/S3)
|
||||
- **Caching**: Redis integration for improved performance
|
||||
- **Security**: Helmet, CORS, rate limiting, and input validation
|
||||
- **TypeScript** - Type-safe development
|
||||
- **TSOA** - Automatic OpenAPI/Swagger documentation
|
||||
- **Prisma** - Type-safe database ORM
|
||||
- **JWT Authentication** - Secure user authentication
|
||||
- **Role-based Authorization** - Admin, Instructor, Student roles
|
||||
- **Multi-language Support** - Thai and English
|
||||
- **File Upload** - Video and attachment support with MinIO/S3
|
||||
- **Redis Caching** - Performance optimization
|
||||
- **Rate Limiting** - API protection
|
||||
- **Comprehensive Error Handling** - Structured error responses
|
||||
|
||||
## 📋 Prerequisites
|
||||
|
||||
- Node.js >= 18.0.0
|
||||
- PostgreSQL >= 14
|
||||
- Redis (optional, for caching)
|
||||
- MinIO or S3 (for file storage)
|
||||
- Node.js >= 18
|
||||
- Docker & Docker Compose
|
||||
- PostgreSQL (via Docker)
|
||||
- Redis (via Docker)
|
||||
- MinIO (via Docker)
|
||||
|
||||
## 🛠️ Installation
|
||||
## 🛠️ Setup
|
||||
|
||||
1. **Clone the repository**
|
||||
```bash
|
||||
git clone <repository-url>
|
||||
cd e-learning/Backend
|
||||
```
|
||||
### 1. Install Dependencies
|
||||
|
||||
2. **Install dependencies**
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
3. **Setup environment variables**
|
||||
### 2. Environment Configuration
|
||||
|
||||
```bash
|
||||
cp .env.example .env
|
||||
# Edit .env with your configuration
|
||||
```
|
||||
|
||||
4. **Start Docker services** (PostgreSQL, Redis, MinIO)
|
||||
### 3. Start Docker Services
|
||||
|
||||
```bash
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
5. **Run database migrations**
|
||||
### 4. Database Setup
|
||||
|
||||
```bash
|
||||
# Generate Prisma client
|
||||
npx prisma generate
|
||||
|
||||
# Run migrations
|
||||
npx prisma migrate dev
|
||||
|
||||
# Seed database
|
||||
npx prisma db seed
|
||||
```
|
||||
|
||||
6. **Seed the database**
|
||||
### 5. Generate TSOA Routes & Swagger
|
||||
|
||||
```bash
|
||||
npm run prisma:seed
|
||||
npm run tsoa:gen
|
||||
```
|
||||
|
||||
7. **Start development server**
|
||||
### 6. Start Development Server
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
The server will start on `http://localhost:4000`
|
||||
The server will start at `http://localhost:4000`
|
||||
|
||||
## 📁 Project Structure
|
||||
## 📚 API Documentation
|
||||
|
||||
Swagger documentation is available at: `http://localhost:4000/api-docs`
|
||||
|
||||
## 🏗️ Project Structure
|
||||
|
||||
```
|
||||
Backend/
|
||||
├── prisma/
|
||||
│ ├── migrations/ # Database migrations
|
||||
│ ├── schema.prisma # Database schema
|
||||
│ └── seed.js # Database seeding
|
||||
│ ├── migrations/ # Database migrations
|
||||
│ ├── schema.prisma # Database schema
|
||||
│ └── seed.js # Database seeder
|
||||
├── src/
|
||||
│ ├── config/ # Configuration files
|
||||
│ │ ├── database.js # Prisma client
|
||||
│ │ ├── logger.js # Winston logger
|
||||
│ │ └── redis.js # Redis client
|
||||
│ ├── controllers/ # Request handlers
|
||||
│ ├── middleware/ # Express middleware
|
||||
│ │ ├── auth.js # Authentication & authorization
|
||||
│ │ └── errorHandler.js
|
||||
│ ├── routes/ # Route definitions
|
||||
│ ├── services/ # Business logic
|
||||
│ ├── utils/ # Utility functions
|
||||
│ │ └── jwt.js # JWT utilities
|
||||
│ ├── validators/ # Input validation
|
||||
│ ├── app.js # Express app setup
|
||||
│ └── server.js # Server entry point
|
||||
├── tests/ # Test files
|
||||
├── logs/ # Log files
|
||||
├── .env.example # Environment variables template
|
||||
├── package.json
|
||||
└── README.md
|
||||
│ ├── config/ # Configuration files
|
||||
│ │ ├── index.ts # Main config
|
||||
│ │ ├── logger.ts # Winston logger
|
||||
│ │ └── database.ts # Prisma client
|
||||
│ ├── controllers/ # TSOA controllers
|
||||
│ │ └── HealthController.ts
|
||||
│ ├── middleware/ # Express middleware
|
||||
│ │ ├── authentication.ts
|
||||
│ │ └── errorHandler.ts
|
||||
│ ├── services/ # Business logic
|
||||
│ ├── types/ # TypeScript types
|
||||
│ │ └── index.ts
|
||||
│ ├── utils/ # Utility functions
|
||||
│ ├── validators/ # Input validation
|
||||
│ ├── app.ts # Express app setup
|
||||
│ └── server.ts # Server entry point
|
||||
├── public/ # Generated Swagger docs
|
||||
├── .env.example # Environment template
|
||||
├── tsconfig.json # TypeScript config
|
||||
├── tsoa.json # TSOA config
|
||||
├── nodemon.json # Nodemon config
|
||||
└── package.json
|
||||
```
|
||||
|
||||
## 🔑 Environment Variables
|
||||
## 🔧 Available Scripts
|
||||
|
||||
See `.env.example` for all available environment variables.
|
||||
```bash
|
||||
npm run dev # Start dev server with hot reload
|
||||
npm run build # Build TypeScript + generate TSOA routes
|
||||
npm start # Start production server
|
||||
npm run tsoa:gen # Generate TSOA routes & Swagger
|
||||
npm test # Run tests
|
||||
npm run lint # Run ESLint
|
||||
npm run format # Format code with Prettier
|
||||
```
|
||||
|
||||
Key variables:
|
||||
- `DATABASE_URL`: PostgreSQL connection string
|
||||
- `JWT_SECRET`: Secret key for JWT tokens
|
||||
- `REDIS_URL`: Redis connection string
|
||||
- `S3_ENDPOINT`: MinIO/S3 endpoint
|
||||
- `CORS_ORIGIN`: Allowed CORS origins
|
||||
## 🗄️ Database Commands
|
||||
|
||||
```bash
|
||||
npx prisma studio # Open Prisma Studio (GUI)
|
||||
npx prisma migrate dev # Create and apply migration
|
||||
npx prisma db seed # Seed database
|
||||
npx prisma generate # Generate Prisma client
|
||||
```
|
||||
|
||||
## 🐳 Docker Commands
|
||||
|
||||
```bash
|
||||
docker compose up -d # Start all services
|
||||
docker compose down # Stop all services
|
||||
docker compose logs -f # View logs
|
||||
docker compose ps # List running services
|
||||
```
|
||||
|
||||
## 🔐 Default Credentials
|
||||
|
||||
After seeding, you can login with:
|
||||
|
||||
- **Admin**: `admin` / `admin123`
|
||||
- **Instructor**: `instructor` / `instructor123`
|
||||
- **Student**: `student` / `student123`
|
||||
|
||||
## 📝 Development Workflow
|
||||
|
||||
1. Create a new controller in `src/controllers/`
|
||||
2. Add TSOA decorators (`@Route`, `@Get`, `@Post`, etc.)
|
||||
3. Run `npm run tsoa:gen` to generate routes
|
||||
4. Implement business logic in `src/services/`
|
||||
5. Test your endpoints
|
||||
|
||||
## 🧪 Testing
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
npm test
|
||||
|
||||
# Run tests in watch mode
|
||||
npm run test:watch
|
||||
|
||||
# Run tests with coverage
|
||||
npm test -- --coverage
|
||||
npm test # Run all tests
|
||||
npm run test:watch # Run tests in watch mode
|
||||
npm run test:coverage # Generate coverage report
|
||||
```
|
||||
|
||||
## 📝 API Documentation
|
||||
## 🚀 Deployment
|
||||
|
||||
### Authentication
|
||||
See [Deployment Workflow](./.agent/workflows/deployment.md) for production deployment instructions.
|
||||
|
||||
- `POST /api/auth/register` - Register new user
|
||||
- `POST /api/auth/login` - Login user
|
||||
- `GET /api/auth/me` - Get current user profile
|
||||
- `POST /api/auth/logout` - Logout user
|
||||
## 📖 Documentation
|
||||
|
||||
### Health Check
|
||||
- [Backend Development Rules](./.agent/rules/rules.md)
|
||||
- [Agent Skills Backend](./agent_skills_backend.md)
|
||||
- [API Workflows](./.agent/workflows/)
|
||||
|
||||
- `GET /health` - Server health status
|
||||
|
||||
## 🔐 Default Credentials
|
||||
|
||||
After seeding the database, you can use these credentials:
|
||||
|
||||
- **Admin**: `admin` / `admin123`
|
||||
- **Instructor**: `instructor` / `admin123`
|
||||
- **Student**: `student` / `admin123`
|
||||
|
||||
## 🛠️ Development
|
||||
## 🔍 Troubleshooting
|
||||
|
||||
### Port already in use
|
||||
```bash
|
||||
# Start dev server with auto-reload
|
||||
npm run dev
|
||||
|
||||
# Run linter
|
||||
npm run lint
|
||||
|
||||
# Format code
|
||||
npm run format
|
||||
|
||||
# Open Prisma Studio (database GUI)
|
||||
npm run prisma:studio
|
||||
lsof -i :4000
|
||||
kill -9 <PID>
|
||||
```
|
||||
|
||||
## 📦 Database Commands
|
||||
|
||||
### Database connection error
|
||||
```bash
|
||||
# Generate Prisma Client
|
||||
npm run prisma:generate
|
||||
|
||||
# Create migration
|
||||
npx prisma migrate dev --name migration_name
|
||||
|
||||
# Apply migrations
|
||||
npx prisma migrate deploy
|
||||
|
||||
# Reset database (development only)
|
||||
npx prisma migrate reset
|
||||
|
||||
# Seed database
|
||||
npm run prisma:seed
|
||||
docker compose logs postgres
|
||||
npx prisma db pull
|
||||
```
|
||||
|
||||
## 🚀 Production Deployment
|
||||
|
||||
1. Set `NODE_ENV=production`
|
||||
2. Set strong `JWT_SECRET`
|
||||
3. Configure production database
|
||||
4. Run migrations: `npx prisma migrate deploy`
|
||||
5. Start with PM2: `pm2 start src/server.js`
|
||||
|
||||
## 📚 Resources
|
||||
|
||||
- [Prisma Documentation](https://www.prisma.io/docs)
|
||||
- [Express Documentation](https://expressjs.com/)
|
||||
- [JWT Documentation](https://jwt.io/)
|
||||
### Prisma client error
|
||||
```bash
|
||||
npx prisma generate
|
||||
```
|
||||
|
||||
## 📄 License
|
||||
|
||||
MIT
|
||||
ISC
|
||||
|
||||
## 👥 Team
|
||||
|
||||
E-Learning Development Team
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue