Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

pascal-web-server

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

Pascal Web Server

A simple HTTP web server with RESTful API endpoints built using Free Pascal and the fphttpapp framework.

Features

  • 🚀 Lightweight HTTP server
  • 🌐 Static file serving (HTML, CSS, JS)
  • 🎨 Modern web interface
  • 🔄 RESTful API endpoints
  • 📝 JSON request/response handling
  • 🛣️ Route parameters support
  • 💾 In-memory data storage (demo)
  • 🧵 Multithreaded request handling

Prerequisites

  • Free Pascal Compiler (FPC) version 3.0.0 or higher

Installing Free Pascal

macOS

bash
brew install fpc

Ubuntu/Debian

bash
sudo apt-get update
sudo apt-get install fpc

Windows

Download the installer from Free Pascal website

Building the Server

Using Make

bash
make

Manual Build

bash
fpc -O2 -Xi -Xs webserver.pas

Running the Server

Using Make

bash
make run

Direct Execution

bash
./webserver

The server will start on http://localhost:8080

Web Interface

Open your browser and navigate to http://localhost:8080 to access the modern web interface!

The web interface provides:

  • 📊 Real-time user list display
  • ➕ Add new users through a form
  • 🧪 Interactive API testing tool
  • 📈 Server health monitoring
  • 💅 Beautiful, responsive design

API Endpoints

1. Root Endpoint

GET /

Serves the main web interface (HTML page).

bash
# Open in browser
open http://localhost:8080

2. API Info

GET /api

Returns API information and available endpoints.

bash
curl http://localhost:8080/api

Response:

json
{
  "message": "Welcome to Pascal Web Server API!",
  "version": "1.0.0",
  "endpoints": ["/api", "/api/health", "/api/users", "/api/users/:id", "/api/echo"]
}

3. Health Check

GET /api/health

Returns server health status.

bash
curl http://localhost:8080/api/health

Response:

json
{
  "status": "healthy",
  "timestamp": "2025-11-20 10:30:45"
}

4. List All Users

GET /api/users

Returns all users.

bash
curl http://localhost:8080/api/users

Response:

json
{
  "count": 3,
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "email": "john@example.com"
    },
    {
      "id": 2,
      "name": "Jane Smith",
      "email": "jane@example.com"
    },
    {
      "id": 3,
      "name": "Bob Johnson",
      "email": "bob@example.com"
    }
  ]
}

5. Get User by ID

GET /api/users/:id

Returns a specific user by ID.

bash
curl http://localhost:8080/api/users/1

Response:

json
{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com"
}

404 Response:

json
{
  "error": "User not found",
  "id": 999
}

6. Create New User

POST /api/users

Creates a new user.

bash
curl -X POST http://localhost:8080/api/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice Wonder", "email": "alice@example.com"}'

Response (201):

json
{
  "id": 4,
  "name": "Alice Wonder",
  "email": "alice@example.com",
  "message": "User created successfully"
}

7. Echo Endpoint

POST /api/echo

Echoes back the JSON request body.

bash
curl -X POST http://localhost:8080/api/echo \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello, Pascal!", "value": 42}'

Response:

json
{
  "echo": {
    "message": "Hello, Pascal!",
    "value": 42
  },
  "timestamp": "2025-11-20 10:30:45"
}

Project Structure

pascal
pascal-web-server/
????????? webserver.pas    # Main server source code
????????? Makefile         # Build configuration
????????? README.md        # This file
????????? QUICKSTART.md    # Quick start guide
????????? test-api.sh      # API testing script
????????? .gitignore       # Git ignore rules
????????? public/          # Static files directory
    ????????? index.html   # Main web interface
    ????????? css/
    ???   ????????? style.css    # Stylesheet
    ????????? js/
        ????????? app.js       # Client-side JavaScript

Development

Cleaning Build Artifacts

bash
make clean

Rebuild

bash
make rebuild

Available Make Targets

  • make or make all - Build the server
  • make run - Build and run the server
  • make clean - Remove build artifacts
  • make rebuild - Clean and rebuild
  • make help - Show available targets

Architecture

The server uses:

  • fphttpapp - Free Pascal HTTP application framework
  • httproute - HTTP routing system
  • fpjson - JSON parsing and generation
  • httpdefs - HTTP definitions and utilities

Key Components

  • Static File Serving: Serves HTML, CSS, JS, and other static files from the public/ directory
  • MIME Type Detection: Automatically determines content type based on file extension
  • Route Registration: Routes are registered with HTTP methods (GET, POST, etc.)
  • JSON Handling: Automatic JSON parsing and generation for requests/responses
  • Route Parameters: Support for dynamic route parameters (e.g., :id)
  • Error Handling: Proper HTTP status codes and error responses
  • Threading: Multithreaded request handling for better performance

Extending the Server

Adding a New API Endpoint

  1. Create a procedure to handle the request:
pascal
procedure HandleMyEndpoint(ARequest: TRequest; AResponse: TResponse);
var
  ResponseJSON: TJSONObject;
begin
  AResponse.ContentType := 'application/json';
  ResponseJSON := TJSONObject.Create;
  try
    ResponseJSON.Add('message', 'My custom endpoint');
    AResponse.Content := ResponseJSON.AsJSON;
  finally
    ResponseJSON.Free;
  end;
end;
  1. Register the route in the main program:
pascal
HTTPRouter.RegisterRoute('/api/myendpoint', rmGet, @HandleMyEndpoint);
  1. Rebuild and run:
bash
make rebuild && make run

Adding Static Files

Simply place your HTML, CSS, JS, or other static files in the appropriate directories:

  • HTML files: public/
  • CSS files: public/css/
  • JavaScript files: public/js/
  • Images: public/images/

The server will automatically serve them with the correct MIME types.

Notes

  • The server stores data in memory, so all data is lost when the server stops
  • For production use, consider adding database integration
  • Default port is 8080 (can be changed in the source code)
  • Press Ctrl+C to stop the server

License

This is a sample project for educational purposes.

Author

Created with Free Pascal and fphttpapp framework.