104 lines
2.9 KiB
Markdown
104 lines
2.9 KiB
Markdown
# Log Microservice
|
|
|
|
A Node.js microservice that receives log data via POST requests with basic authentication and stores them in a PostgreSQL database via Supabase.
|
|
|
|
## Features
|
|
|
|
- **HTTP API**: RESTful endpoints for receiving log data
|
|
- **Basic Authentication**: Secure endpoints with username/password authentication
|
|
- **PostgreSQL Storage**: Stores logs in Supabase PostgreSQL database
|
|
- **Input Validation**: Validates incoming data and provides meaningful error messages
|
|
- **Default Values**: Automatically applies default values for optional fields
|
|
- **Health Check**: Built-in health check endpoint
|
|
|
|
## API Endpoints
|
|
|
|
### POST /logs
|
|
Receives log data and stores it in the database.
|
|
|
|
**Authentication**: Basic Auth required
|
|
|
|
**Request Body**:
|
|
```json
|
|
{
|
|
"message": "This is a log message (required)",
|
|
"project": "My Project (optional, defaults to 'Project 1')",
|
|
"type": "Error (optional, defaults to 'Info')",
|
|
"owner": "john.doe (optional, defaults to 'N/A')",
|
|
"avatar_src": "/custom-avatar.png (optional, defaults to '/rectangle-15.png')",
|
|
"status": "Active (optional, defaults to 'Pending')"
|
|
}
|
|
```
|
|
|
|
**Response**:
|
|
```json
|
|
{
|
|
"success": true,
|
|
"message": "Log entry created successfully",
|
|
"id": 1703123456
|
|
}
|
|
```
|
|
|
|
### GET /health
|
|
Health check endpoint to verify service status.
|
|
|
|
**Response**:
|
|
```json
|
|
{
|
|
"status": "healthy",
|
|
"timestamp": "2024-01-01T12:00:00.000Z"
|
|
}
|
|
```
|
|
|
|
## Setup
|
|
|
|
1. **Set up Supabase**: Click the "Connect to Supabase" button to configure your database
|
|
2. **Configure Environment**: Copy `.env.example` to `.env` and update the values
|
|
3. **Run the Service**: Use `npm start` to start the microservice
|
|
|
|
## Database Schema
|
|
|
|
The service automatically creates a `logs` table with the following structure:
|
|
|
|
- `id` (bigint) - Epoch time identifier
|
|
- `body` (text) - First 200 characters of the message
|
|
- `project` (text) - Project name
|
|
- `type` (text) - Log type (Info, Error, Warning, etc.)
|
|
- `date` (date) - Date of the log entry
|
|
- `avatar_src` (text) - Avatar image source
|
|
- `owner` (text) - Owner of the log entry
|
|
- `description` (text) - Full log message
|
|
- `created_at` (timestamptz) - Creation timestamp
|
|
- `status` (text) - Status of the log entry
|
|
|
|
## Authentication
|
|
|
|
The service uses HTTP Basic Authentication. Default credentials:
|
|
- Username: `admin`
|
|
- Password: `password123`
|
|
|
|
Update these in your `.env` file for production use.
|
|
|
|
## Usage Example
|
|
|
|
```bash
|
|
# Using curl to send a log entry
|
|
curl -X POST http://localhost:3000/logs \
|
|
-H "Content-Type: application/json" \
|
|
-H "Authorization: Basic YWRtaW46cGFzc3dvcmQxMjM=" \
|
|
-d '{
|
|
"message": "User login successful",
|
|
"project": "Authentication Service",
|
|
"type": "Info",
|
|
"owner": "auth-service"
|
|
}'
|
|
```
|
|
|
|
## Error Handling
|
|
|
|
The service provides detailed error responses:
|
|
|
|
- `400 Bad Request`: Invalid input data
|
|
- `401 Unauthorized`: Missing or invalid authentication
|
|
- `404 Not Found`: Unknown endpoint
|
|
- `500 Internal Server Error`: Database or server errors |