This is a Python application that provides JWT authentication and integrates with the Dify API for chat functionality.
- JWT-based authentication
- Integration with Dify API for chat
- FastAPI backend
- Secure password hashing
- Environment variable configuration
- Install the required dependencies:
pip install -r requirements.txt-
Configure the application:
- Copy
.env.exampleto.env:cp .env.example .env
- Update the values in
.envfile with your settings:# JWT Settings SECRET_KEY=your-secret-key-here ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30 # Dify API Settings DIFY_API_URL=http://localhost/v1 DIFY_API_KEY=your-dify-api-key # Server Settings HOST=0.0.0.0 PORT=8000
- Copy
-
Set up Python virtual environment (recommended):
# Create virtual environment python -m venv venv # Activate virtual environment # On Windows: .\venv\Scripts\activate # On Unix or MacOS: source venv/bin/activate # Install dependencies pip install -r requirements.txt
- Start the server:
python main.py- The server will run on
http://localhost:8000
- Get a JWT token:
curl -X POST "http://localhost:8000/token" -H "Content-Type: application/x-www-form-urlencoded" -d "username=testuser&password=testpassword"- Use the token for authenticated requests:
curl -X POST "http://localhost:8000/chat" -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{"query": "Hello", "response_mode": "blocking"}'- Username: testuser
- Password: testpassword
POST /token: Get JWT tokenPOST /chat: Send message to Dify API
The application uses the following environment variables:
SECRET_KEY: JWT secret key for token generationALGORITHM: JWT algorithm (default: HS256)ACCESS_TOKEN_EXPIRE_MINUTES: Token expiration time in minutesDIFY_API_URL: Dify API endpoint URLDIFY_API_KEY: Dify API keyHOST: Server host addressPORT: Server port number
The application loads environment variables in the following order:
- System environment variables
.envfile- Default values in the code
- Never commit the
.envfile to version control - Use strong, unique values for
SECRET_KEYin production - Implement proper user management in production
- Keep your API keys secure