Home
Softono
MiniWebServer

MiniWebServer

Open source MIT Pascal
16
Stars
6
Forks
0
Issues
2
Watchers
2 years
Last Commit

About MiniWebServer

MiniWebServer is a lightweight HTTP server library for Delphi and Pascal that enables developers to quickly create web applications with minimal code. It supports standard request handling through route mappings, allowing developers to define URL endpoints that respond with custom logic including JSON objects, plain text, or file contents. The library features attribute-based routing where methods can be decorated with RouteMethod annotations to automatically handle specific HTTP verbs like GET and HEAD. It also supports inline anonymous route definitions for rapid prototyping. The server can bind to multiple ports simultaneously and includes an AutoFileServer option for serving static files directly from disk. Response objects provide convenient methods for sending various data types, including auto-serialization of objects to JSON and returning files by path. Response status codes can be customized for each route. Typical use cases include building REST APIs, microservice backends, weather servers, file-sha

Platforms

Web Self-hosted

Languages

Pascal

Links

MiniWebServer

program MiniWebServer;

uses
  HTTP.Server in 'HTTP.Server.pas',
  WMS.OWM in 'Sample\WMS.OWM.pas';

begin
  var Server := THTTPServer.Create;
  try
    Server.Route('/weather', GetWeather);
    Server.Run([80, 8080, 9090]);
  finally
    Server.Free;
  end;
end.
program MiniWebServer_Attrib;

uses
  HTTP.Server in 'HTTP.Server.pas',
  WMS.OWM in 'Sample\WMS.OWM.pas';

type
  TServer = class(THTTPServer)
    //auto class routes
    [RouteMethod('/test', [GET, HEAD])]
    procedure Test(Request: TRequest; Response: TResponse);
    [RouteMethod('/check', [GET])]
    procedure Check(Request: TRequest; Response: TResponse);
  end;

{ TServer }

procedure TServer.Check;
begin
  //send json text
  Response.Json('{ "value": "test_text" }', 401);
  //send file
  Response.AsFile('C:\file.ext');
  //send json object
  var JObject := TJsonObject.Create;
  Response.Json(JObject, 200); // auto clear
end;

procedure TServer.Test;
begin
  //send object
  var Obj := TObject.Create;
  Response.Json(Obj);
  Obj.Free;
end;

begin
  var Server := TServer.Create;
  try
    //GET files
    Server.AutoFileServer := True;
    //inline route
    Server.Route('/run',
      procedure(Request: TRequest; Response: TResponse)
      begin
        Response.Json('{ "text": "done" }', 200);
      end);
    //add route
    Server.Route('/weather', GetWeather);
    Server.Run([80, 8080, 9090]);
  finally
    Server.Free;
  end;
end.