🇺🇸
ModularBr Framework for Delphi
Portugues(PT)TelegramGitHub
  • Welcome to ModularBr
  • Getting to know
    • Introduction
      • Benefits
      • Requirements
      • Installing
  • Module
    • Exporting Binds
    • Importing Binds
  • Routes
    • Route Guard
    • Middlewares
  • Pipes
  • Dependency injection
  • Route Handlers
  • Usability
    • Horse and ModularBr
    • Desktop and ModularBr
  • CLI
  • Frequently Asked Questions
    • FAQs
      • How to contribute?
      • How to download and install?
      • What is InjectorBr?
      • What is ResultPairBr?
      • What is ECLBr?
  • Usage policy
    • License
Powered by GitBook
On this page

Route Handlers

With ModularBr, the routes of your API can be defined in a clear and organized manner using the Route Handlers feature. With this feature, you can divide the implementations into separate classes that inherit from the TRouteHandler class, where each class is responsible for defining the corresponding routes and their handlers. Then, the list of classes that define the routes is registered in a single place - the RouteHandlers method of the AppModule module - making development and maintenance easier and more efficient.

By using Route Handlers, the code becomes more modular and easier to understand, allowing you to quickly find the implementations for each route. This means that you can have a more organized structure for handling the routes of your application, making maintenance and the addition of new features easier. With ModularBr, you have a powerful feature that helps make your API more efficient, organized, and easy to manage.

Example of usage with Horse:

Creating the RouteHandler for PDF retrieval:

unit pdf.route.handler;
...
type
  TPDFRouteHandler = class(TRouteHandler)
  protected
    procedure RegisterRoutes; override;
  public
    procedure NFePDF(Req: THorseRequest; Res: THorseResponse);
    procedure NFeCancelamentoPDF(Req: THorseRequest; Res: THorseResponse);
    procedure NFeInutilizacaoPDF(Req: THorseRequest; Res: THorseResponse);
    procedure NFeCartaCorrecaoPDF(Req: THorseRequest; Res: THorseResponse);
  end;

implementation
...
{ TPDFRouteHandler }
procedure TPDFRouteHandler.RegisterRoutes;
begin
  inherited;
  THorse.Get(Rota.NFePDF, NFePDF);
  THorse.Get(Rota.NFeCancelamentoPDF, NFeCancelamentoPDF);
  THorse.Get(Rota.NFeInutilizacaoPDF, NFeInutilizacaoPDF);
  THorse.Get(Rota.NFeCartaCorrecaoPDF, NFeCartaCorrecaoPDF);
end;
...

Registering TPDFHouteHandler in AppModule:

...
type
  TAppModule = class(TModule)
  public
    function Routes: TRoutes; override;
    function RouteHandlers: TRouteHandlers; override;
  end;

implementation
...
function TAppModule.RouteHandlers: TRouteHandlers;
begin
  Result := [TPDFRouteHandler];
end;

function TAppModule.Routes: TRoutes;
begin
  Result := [RouteModule(Rota.Empresa, TEmpresaModule),
             RouteModule(Rota.NFe, TNFeModule)];
end;
...

Initializing the Modular and the AppModule:

procedure TFormServer.StartRoutes;
begin
  ReportMemoryLeaksOnShutdown := True;
  // Registering to AppModule/Route Guardian
  THorse.Use(HorseModular(TAppModule.Create));
  // Putting the server online.
  StartServer;
end;

procedure TFormServer.StartServer;
begin
  THorse.Listen(9000);
end;
PreviousDependency injectionNextUsability

Last updated 1 year ago