A production-grade Express.js API server built with TypeScript, providing various API endpoints for different services including color palette generation, manga comparison, and general utilities.
- ๐จ Color Palette Generator API
- ๐ Manga Comparison Service
- ๐ Stack Connect Integration
- ๐ค General ChatGPT Integration
- ๐ CORS Protection
- ๐ TypeScript Support
- ๐งช Jest Testing Framework
- โ๏ธ AWS Lambda Support
- ๐ Swagger API Documentation
- Node.js (v22 or higher)
- npm (v6 or higher)
- AWS CLI (for deployment)
- Serverless Framework (for deployment)
- Clone the repository:
git clone https://github.com/yourusername/general-chatgpt-server.git
cd general-chatgpt-server
- Install dependencies:
npm install
- Create a
.env
file in the root directory:
NODE_ENV=development
PORT=3000
Start the development server:
npm run dev
Other useful commands:
npm run lint # Run ESLint
npm run lint:fix # Fix ESLint issues
npm run test # Run tests
npm run test:watch # Run tests in watch mode
npm run typecheck # Run TypeScript type checking
npm run build:production # Build for production
npm start # Start production server
This project uses environment variables for configuration. You can manage them in several ways:
For local development, create a .env
file in the root directory:
NODE_ENV=development
PORT=3000
API_KEY=your_api_key_here
BASE_URL=
For AWS Lambda deployment, you have several options:
-
Environment-specific files
Create environment-specific files like
.env.dev
,.env.staging
, and.env.production
:# Create environment-specific file cp .env.example .env.dev # Edit with your values nano .env.dev
The serverless-dotenv-plugin will load variables from these files based on the deployment stage.
-
Directly in serverless.yml
For non-sensitive configuration, define variables directly in the serverless.yml file:
provider: environment: NODE_ENV: ${opt:stage, 'dev'} PORT: 8080
-
AWS Parameter Store for Sensitive Values
For API keys, database connection strings, and other sensitive values, use AWS Parameter Store:
provider: environment: API_KEY: ${ssm:/my-app/${opt:stage, 'dev'}/api-key~true}
To store a value in Parameter Store:
# Store a secure parameter aws ssm put-parameter --name "/my-app/dev/api-key" --type "SecureString" --value "your-api-key-here"
Variable | Description | Default | Required |
---|---|---|---|
NODE_ENV | Environment (development, production) | development | Yes |
PORT | Port to run the server on | 3000 | No |
API_KEY | API key for external services | - | Yes |
This project is configured to run as an AWS Lambda function using the Serverless Framework.
To test the Lambda function locally before deployment:
npm run lambda:local
This will start a local server that simulates the AWS Lambda environment at http://localhost:3000.
To create a deployment package without deploying:
npm run lambda:package
This will create a .serverless
directory with the packaged Lambda function.
Ensure you have AWS credentials configured using AWS CLI:
aws configure
Then deploy to AWS:
npm run lambda:deploy
To deploy to a specific stage (e.g., production):
npm run lambda:deploy:prod
To deploy to a specific region:
npm run lambda:deploy -- --region ap-southeast-1
The Lambda function is configured in the serverless.yml
file. Key configuration options:
- Runtime: Node.js 22.x
- Memory: 1024MB
- Timeout: 30 seconds
- API Gateway: HTTP API with CORS enabled
- Environment Variables:
- Loaded from
.env.{stage}
files via serverless-dotenv-plugin - Directly defined in serverless.yml
- Securely stored in AWS Parameter Store for sensitive data
- Loaded from
For sensitive environment variables, use AWS Parameter Store:
# Store API key securely
aws ssm put-parameter \
--name "/my-app/dev/api-key" \
--type "SecureString" \
--value "your-api-key-here"
# Store database connection string
aws ssm put-parameter \
--name "/my-app/dev/database-url" \
--type "SecureString" \
--value "postgres://user:password@hostname:5432/dbname"
Then reference them in serverless.yml:
provider:
environment:
API_KEY: ${ssm:/my-app/${opt:stage, 'dev'}/api-key~true}
DATABASE_URL: ${ssm:/my-app/${opt:stage, 'dev'}/database-url~true}
Development: http://localhost:3000
Production: https://api.thanachon.me
Access the Swagger API documentation at:
Development: http://localhost:3000/docs
Production: https://api.thanachon.me/docs
GET /v1/colors/:input
POST /v1/colors/chat
Parameter | Type | Required | Description |
---|---|---|---|
input |
string |
Yes | Description or theme for the color palette |
{
colors: Array<{
code: string, // Hex color code
name: string // Color name
}>
}
# GET request
curl -X GET 'https://api.thanachon.me/v1/colors/sunset'
# POST request
curl -X POST 'https://api.thanachon.me/v1/colors/chat' \
-H 'Content-Type: application/json' \
-d '{"input": "sunset colors"}'
Endpoint | Method | Description |
---|---|---|
/v1/general/fix-grammar |
POST | Fix spelling and grammar |
/v1/general/summarize |
POST | Summarize text |
/v1/general/rewrite |
POST | Rewrite text |
/v1/general/explain |
POST | Explain text |
/v1/general/translate |
POST | Translate text |
/v1/general/brainstorm |
POST | Generate ideas |
/v1/general/outline |
POST | Create outline |
/v1/general/write-blog |
POST | Generate blog content |
/v1/general/shorter |
POST | Make text shorter |
/v1/general/longer |
POST | Make text longer |
{
"input": "string", // Required: Text to process
"lang": "string" // Optional: Target language (default: "en")
}
Note: lang
parameter is only used for translate, summarize, explain, brainstorm, outline, write-blog, shorter, and longer endpoints.
POST /v1/stack-connect/feelinks
Parameter | Type | Required | Description |
---|---|---|---|
category |
string |
Yes | Category for scenario generation |
{
"scenario": "string",
"audio": "base64" // Audio version of the scenario
}
POST /v1/stack-connect/ito
Parameter | Type | Required | Description |
---|---|---|---|
category |
string |
Yes | Category for question generation |
lang |
string |
No | Language (default: "en") |
{
"data": {
"question": "string",
"least": "string",
"most": "string"
},
"audio": "base64" // Audio version of the question
}
POST /v1/manga
Parameter | Type | Required | Description |
---|---|---|---|
char1 |
string |
Yes | First character name |
char2 |
string |
Yes | Second character name |
manga1 |
string |
Yes | First manga title |
manga2 |
string |
Yes | Second manga title |
curl -X POST 'https://api.thanachon.me/v1/manga' \
-H 'Content-Type: application/json' \
-d '{
"char1": "Luffy",
"char2": "Naruto",
"manga1": "One Piece",
"manga2": "Naruto"
}'
All endpoints follow this error format:
{
"error": {
"message": "Error description"
}
}
Common HTTP Status Codes:
- 200: Success
- 400: Bad Request (missing parameters)
- 500: Internal Server Error
The server implements rate limiting based on the endpoint usage. Contact the administrator for specific limits.
The server implements CORS protection with the following configurations:
- Development: All origins allowed
- Production: Whitelist of specific domains
- color-palette-generator-v0ah.onrender.com
- thanachon.me and subdomains
- Other configured domains
general-chatgpt-server/
โโโ config/ # Configuration files
โโโ controllers/ # API controllers
โโโ middlewares/ # Express middlewares
โโโ models/ # Data models
โโโ routes/ # API routes
โโโ types/ # TypeScript type definitions
โโโ utilities/ # Utility functions
โโโ server.ts # Main server file
โโโ dist/ # Compiled JavaScript
โโโ tests/ # Test files
โโโ .env # Environment variables
โโโ package.json # Project dependencies
Run the test suite:
npm test # Run all tests
npm run test:watch # Run tests in watch mode