A modern, containerized Dart web application featuring three REST API endpoints and an interactive HTML interface. Built with the Shelf framework and optimized for Docker deployment.
-
Three REST API Endpoints:
GET /api/time- Returns current server time with multiple formatsGET /api/random- Generates random values (integer, decimal, boolean)POST /api/echo- Echoes back JSON payloads with metadata
-
Interactive Web Interface: Beautiful, gradient-styled HTML page with real-time API testing
-
Docker Ready: Multi-stage Dockerfile for optimized production builds
-
CORS Enabled: Cross-Origin Resource Sharing configured for browser compatibility
-
Port 9000: Configured to run on port 9000 as specified
-
For Local Development:
- Dart SDK 3.0 or higher
-
For Docker Deployment:
- Docker installed on your system
dart-docker/
βββ bin/
β βββ server.dart # Main server application
βββ public/
β βββ index.html # Interactive web interface
βββ pubspec.yaml # Dart dependencies
βββ Dockerfile # Docker configuration
βββ .dockerignore # Docker build exclusions
βββ README.md # This file
-
Install Dependencies:
cd /Users/renga-16116/Documents/dart-docker dart pub get -
Run the Server:
dart run bin/server.dart
-
Access the Application:
- Open your browser to:
http://localhost:9000 - The interactive interface will load automatically
- Open your browser to:
cd /Users/renga-16116/Documents/dart-docker
docker build --platform=linux/amd64 -t dart-docker-app .The build process uses a multi-stage approach:
- Stage 1: Compiles Dart code to a native executable
- Stage 2: Creates a minimal runtime image with only necessary components
docker run -p 9000:9000 dart-web-appThe application will be available at http://localhost:9000
docker run -d -p 9000:9000 --name dart-api dart-web-appdocker stop dart-api
docker rm dart-apiEndpoint: GET /api/time
Description: Returns the current server time in multiple formats.
Response Example:
{
"timestamp": "2025-12-19T16:25:30.123Z",
"unix": 1734627930123,
"formatted": "2025-12-19 16:25:30.123",
"timezone": "IST"
}cURL Example:
curl http://localhost:9000/api/timeEndpoint: GET /api/random
Description: Generates random integer, decimal, and boolean values.
Response Example:
{
"number": 742,
"decimal": 0.8374629,
"boolean": true
}cURL Example:
curl http://localhost:9000/api/randomEndpoint: POST /api/echo
Description: Echoes back the JSON payload with request metadata.
Request Body:
{
"message": "Hello, Dart!"
}Response Example:
{
"echo": {
"message": "Hello, Dart!"
},
"contentType": "application/json",
"method": "POST",
"receivedAt": "2025-12-19T16:25:30.456Z"
}cURL Example:
curl -X POST http://localhost:9000/api/echo \
-H "Content-Type: application/json" \
-d '{"message": "Hello, Dart!"}'The application includes a beautiful, interactive web interface accessible at the root URL (http://localhost:9000). Features include:
- Modern Design: Gradient backgrounds with glassmorphism effects
- Real-time Testing: Interactive buttons to test each API endpoint
- Response Visualization: Formatted JSON responses with status indicators
- Smooth Animations: Fade-in effects and hover transitions
- Responsive Layout: Works on desktop and mobile devices
- shelf (^1.4.0): HTTP server framework
- shelf_router (^1.1.0): Routing middleware
- shelf_static (^1.1.0): Static file serving
- Base Image:
dart:stable(build stage),debian:bullseye-slim(runtime) - Compiled: Native executable for optimal performance
- Image Size: Optimized with multi-stage build
- Port: Exposes port 9000
The application includes CORS middleware allowing:
- All origins (
*) - Methods: GET, POST, PUT, DELETE, OPTIONS
- Headers: Origin, Content-Type, Accept
- Navigate to
http://localhost:9000 - Click the buttons for each endpoint
- View the formatted JSON responses
Test all endpoints with the provided cURL commands in the API Documentation section.
Import the following base URL: http://localhost:9000
Then test each endpoint as documented above.
- The server logs all requests to the console
- Static files are served from the
public/directory - The application uses async/await for non-blocking I/O
- Error handling is implemented for the echo endpoint
Feel free to fork this project and submit pull requests for any improvements.
This project is open source and available for educational and commercial use.
Port Already in Use:
# Find process using port 9000
lsof -i :9000
# Kill the process or use a different portDocker Build Fails:
- Ensure Docker is running
- Check that all files are present in the directory
- Verify Dart SDK version compatibility
Cannot Access Application:
- Verify the container is running:
docker ps - Check port mapping is correct:
-p 9000:9000 - Ensure no firewall is blocking port 9000
Built with β€οΈ using Dart and Shelf Framework