Skip to content

catalystbyzoho/example-dart-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Dart REST API Web Application

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.

πŸš€ Features

  • Three REST API Endpoints:

    • GET /api/time - Returns current server time with multiple formats
    • GET /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

πŸ“‹ Prerequisites

  • For Local Development:

    • Dart SDK 3.0 or higher
  • For Docker Deployment:

    • Docker installed on your system

πŸ› οΈ Project Structure

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

πŸƒ Running Locally (Without Docker)

  1. Install Dependencies:

    cd /Users/renga-16116/Documents/dart-docker
    dart pub get
  2. Run the Server:

    dart run bin/server.dart
  3. Access the Application:

    • Open your browser to: http://localhost:9000
    • The interactive interface will load automatically

🐳 Running with Docker

Build the Docker Image

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

Run the Docker Container

docker run -p 9000:9000 dart-web-app

The application will be available at http://localhost:9000

Run in Detached Mode

docker run -d -p 9000:9000 --name dart-api dart-web-app

Stop the Container

docker stop dart-api
docker rm dart-api

πŸ“‘ API Documentation

1. Get Server Time

Endpoint: 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/time

2. Generate Random Values

Endpoint: 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/random

3. Echo Request

Endpoint: 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!"}'

🎨 Web Interface

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

πŸ”§ Technical Details

Dependencies

  • shelf (^1.4.0): HTTP server framework
  • shelf_router (^1.1.0): Routing middleware
  • shelf_static (^1.1.0): Static file serving

Docker Image Details

  • 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

CORS Configuration

The application includes CORS middleware allowing:

  • All origins (*)
  • Methods: GET, POST, PUT, DELETE, OPTIONS
  • Headers: Origin, Content-Type, Accept

πŸ§ͺ Testing the Application

Using the Web Interface

  1. Navigate to http://localhost:9000
  2. Click the buttons for each endpoint
  3. View the formatted JSON responses

Using cURL

Test all endpoints with the provided cURL commands in the API Documentation section.

Using Postman or Similar Tools

Import the following base URL: http://localhost:9000

Then test each endpoint as documented above.

πŸ“ Development Notes

  • 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

🀝 Contributing

Feel free to fork this project and submit pull requests for any improvements.

πŸ“„ License

This project is open source and available for educational and commercial use.

πŸ†˜ Troubleshooting

Port Already in Use:

# Find process using port 9000
lsof -i :9000

# Kill the process or use a different port

Docker 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

About

Demo dart app deployed on Catalyst Appsail

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published