Catalog · How the catalog works
pascal-web-server
- ★ 1 on GitHub
Anyone may press Interesting, signed in or not. It helps others find 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
brew install fpcUbuntu/Debian
sudo apt-get update
sudo apt-get install fpcWindows
Download the installer from Free Pascal website
Building the Server
Using Make
makeManual Build
fpc -O2 -Xi -Xs webserver.pasRunning the Server
Using Make
make runDirect Execution
./webserverThe 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).
# Open in browser
open http://localhost:80802. API Info
GET /api
Returns API information and available endpoints.
curl http://localhost:8080/apiResponse:
{
"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.
curl http://localhost:8080/api/healthResponse:
{
"status": "healthy",
"timestamp": "2025-11-20 10:30:45"
}4. List All Users
GET /api/users
Returns all users.
curl http://localhost:8080/api/usersResponse:
{
"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.
curl http://localhost:8080/api/users/1Response:
{
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}404 Response:
{
"error": "User not found",
"id": 999
}6. Create New User
POST /api/users
Creates a new user.
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice Wonder", "email": "alice@example.com"}'Response (201):
{
"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.
curl -X POST http://localhost:8080/api/echo \
-H "Content-Type: application/json" \
-d '{"message": "Hello, Pascal!", "value": 42}'Response:
{
"echo": {
"message": "Hello, Pascal!",
"value": 42
},
"timestamp": "2025-11-20 10:30:45"
}Project Structure
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 JavaScriptDevelopment
Cleaning Build Artifacts
make cleanRebuild
make rebuildAvailable Make Targets
makeormake all- Build the servermake run- Build and run the servermake clean- Remove build artifactsmake rebuild- Clean and rebuildmake 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
- Create a procedure to handle the request:
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;- Register the route in the main program:
HTTPRouter.RegisterRoute('/api/myendpoint', rmGet, @HandleMyEndpoint);- Rebuild and run:
make rebuild && make runAdding 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+Cto stop the server
License
This is a sample project for educational purposes.
Author
Created with Free Pascal and fphttpapp framework.

