An open API service indexing awesome lists of open source software.

https://github.com/homuchen/meno-api

An api server for meno
https://github.com/homuchen/meno-api

Last synced: 19 days ago
JSON representation

An api server for meno

Awesome Lists containing this project

README

          

# Meno API

RESTful API built with Express, MongoDB, and clean architecture principles.

## ๐Ÿ—๏ธ Architecture

This project follows **Clean Architecture** (Hexagonal/Onion Architecture) with clear separation of concerns:

```
src/
โ”œโ”€โ”€ core/ # Business logic layer
โ”œโ”€โ”€ components/ # Infrastructure layer (database, storage, logging)
โ”œโ”€โ”€ api/ # HTTP interface layer
โ”œโ”€โ”€ models/ # Data models
โ””โ”€โ”€ utils/ # Utilities
```

### Dependency Flow

```
Controllers โ†’ Core Services โ†’ Components
```

- **Controllers** handle HTTP concerns only
- **Core Services** contain business logic
- **Components** handle infrastructure (database, storage, logging)

## ๐Ÿš€ Features

- โœ… **Clean Architecture** - Separation of concerns, testable, maintainable
- โœ… **Pluggable Storage** - Interface-based storage (local filesystem, GCS, S3)
- โœ… **Audio File Support** - Full MP3, M4A, WAV, WebM, OGG, FLAC support with automatic duration extraction
- โœ… **MongoDB Database** - Mongoose ODM with schema validation
- โœ… **Winston Logging** - Structured logging with file rotation
- โœ… **Swagger Documentation** - Auto-generated API docs from code
- โœ… **Error Handling** - Global error handler with custom error classes
- โœ… **Request Validation** - Joi schema validation
- โœ… **Health Checks** - Readiness and liveness endpoints

## ๐Ÿ“ฆ Installation

```bash
# Install dependencies
npm install

# Copy environment file
cp .env.example .env

# Update .env with your configuration
```

## ๐Ÿ”ง Configuration

Edit `.env` file:

```bash
NODE_ENV=development
PORT=3000
MONGODB_URI=mongodb://localhost:27017/meno-api
STORAGE_PROVIDER=local
LOCAL_STORAGE_PATH=./storage
LOG_LEVEL=info
```

## ๐Ÿƒ Running the Server

```bash
# Development (with auto-reload)
npm run dev

# Production
npm start
```

Server will start on `http://localhost:3000`

## ๐Ÿ“š API Documentation

### Interactive Documentation

Once the server is running, visit:

- **Swagger UI**: http://localhost:6001/api-docs
- **Health Check**: http://localhost:6001/api/health

### OpenAPI Specification

For frontend development or code generation:

- **OpenAPI YAML**: `docs/openapi.yaml` (1600+ lines, 23 endpoints)
- **Documentation Guide**: See `docs/README.md` for usage instructions

To regenerate the OpenAPI spec after making changes:

```bash
npm run generate:openapi
```

## ๐Ÿ›ฃ๏ธ API Endpoints

### Health
- `GET /api/health` - Health check
- `GET /api/health/ready` - Readiness check

### Users
- `GET /api/users` - Get all users (paginated)
- `GET /api/users/:id` - Get user by ID
- `POST /api/users` - Create new user
- `PUT /api/users/:id` - Update user
- `DELETE /api/users/:id` - Delete user
- `POST /api/users/:id/avatar` - Upload user avatar

### Files
- `POST /api/files` - Upload file
- `GET /api/files` - Get all files (paginated)
- `GET /api/files/:id` - Get file by ID
- `GET /api/files/:id/download` - Download file
- `GET /api/files/:id/url` - Get file URL
- `DELETE /api/files/:id` - Delete file

## ๐Ÿ”Œ Storage Providers

### Local Filesystem (Default)

```bash
STORAGE_PROVIDER=local
LOCAL_STORAGE_PATH=./storage
```

### Google Cloud Storage

```bash
STORAGE_PROVIDER=gcs
GCS_BUCKET_NAME=your-bucket-name
GCS_KEY_FILE=./path/to/service-account-key.json
```

**Note**: GCS provider requires implementation. See `src/components/storage/providers/gcs.provider.js`

### Adding New Storage Providers

1. Create new provider class implementing `IStorageProvider` interface
2. Add to `StorageFactory` in `src/components/storage/storage.factory.js`
3. Update environment configuration

## ๐Ÿงช Testing

```bash
# Run tests
npm test

# Run tests with coverage
npm test -- --coverage

# Watch mode
npm run test:watch
```

## ๐Ÿ“ Code Quality

```bash
# Lint code
npm run lint

# Fix linting issues
npm run lint:fix
```

## ๐Ÿ—‚๏ธ Project Structure

```
meno-api/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ api/ # HTTP layer
โ”‚ โ”‚ โ”œโ”€โ”€ controllers/ # Request handlers
โ”‚ โ”‚ โ”œโ”€โ”€ middleware/ # Express middleware
โ”‚ โ”‚ โ”œโ”€โ”€ routes/ # Route definitions
โ”‚ โ”‚ โ””โ”€โ”€ validators/ # Request validation
โ”‚ โ”œโ”€โ”€ components/ # Infrastructure
โ”‚ โ”‚ โ”œโ”€โ”€ config/ # Configuration
โ”‚ โ”‚ โ”œโ”€โ”€ database/ # MongoDB connection
โ”‚ โ”‚ โ”œโ”€โ”€ logging/ # Winston logger
โ”‚ โ”‚ โ””โ”€โ”€ storage/ # Storage abstraction
โ”‚ โ”œโ”€โ”€ core/ # Business logic
โ”‚ โ”‚ โ”œโ”€โ”€ interfaces/ # Contracts
โ”‚ โ”‚ โ””โ”€โ”€ services/ # Domain services
โ”‚ โ”œโ”€โ”€ models/ # MongoDB schemas
โ”‚ โ”œโ”€โ”€ utils/ # Utilities
โ”‚ โ”œโ”€โ”€ app.js # Express app setup
โ”‚ โ””โ”€โ”€ server.js # Entry point
โ”œโ”€โ”€ storage/ # Local file storage
โ”œโ”€โ”€ logs/ # Log files
โ”œโ”€โ”€ tests/ # Test files
โ”œโ”€โ”€ .env.example # Environment template
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ README.md
```

## ๐Ÿ” Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `NODE_ENV` | Environment | `development` |
| `PORT` | Server port | `3000` |
| `MONGODB_URI` | MongoDB connection string | `mongodb://localhost:27017/meno-api` |
| `STORAGE_PROVIDER` | Storage provider (local, gcs, s3) | `local` |
| `LOCAL_STORAGE_PATH` | Local storage path | `./storage` |
| `LOG_LEVEL` | Logging level | `info` |
| `LOG_DIR` | Log directory | `./logs` |

## ๐Ÿค Contributing

1. Follow clean architecture principles
2. Controllers only call core services
3. Core services call components via interfaces
4. Add tests for new features
5. Update Swagger documentation

## ๐Ÿ“„ License

ISC