DelphiMVCFramework

repository·master·Indexed 23 days ago

https://github.com/danieleteti/delphimvcframework

An open-source framework for building RESTful services, JSON-RPC APIs, and web applications using Object Pascal. It features a complete MVC architecture with a built-in ORM, authentication, and middleware support. The repository also includes LoggerPro for asynchronous structured logging and SwagDoc for generating Swagger 2.0 specification files.

Tokens
52.9K
Snippets
114
Records
243
Agent score
73%

What's inside DelphiMVCFramework

  1. What is SwagDoc

    master

    SwagDoc is a Delphi library designed specifically to generate a swagger.json file following the Swagger Specification version 2.0.

    Its primary responsibility is the generation of the swagger.json file, which contains the complete documentation for your REST API. To visualize this documentation as an interactive web page, the generated swagger.json file must be attached to Swagger UI distribution files.

  2. Explore comprehensive streaming mechanisms and guides

    master

    For more advanced or different streaming implementations, refer to these resources:

    • Comprehensive Showcase: See samples/streamed_array_writer/ for a side-by-side comparison of JSON-array writers, SSE, JSONL, CSV, and the declarative TMVCStreamedResponse chunked path.
    • Full Documentation: See docs/incremental-streaming.md for a complete guide on when to use each mechanism, how to activate it, framing, backends, and handling disconnections.
  3. Identify key files in the Minimal API WebApp Showcase

    master

    The following files contain the core logic and templates for the showcase:

    • RoutesU.pas: Contains all handlers, annotated with the binding mode they illustrate.
    • ShowcaseModelsU.pas: Defines the records used for data binding (TSignupForm, TContextInfo, TSearchQuery), demonstrating different attribute sources.
    • templates/baselayout.html and templates/pages/*.html: Bootstrap 5.3 views (defaulting to dark mode via data-bs-theme).
    • ServicesU.pas: Handles the registration of IPeopleService used by the routes.
  4. Create an automatic installer for Delphi components using InnoSetup

    master

    This collection of InnoSetup scripts allows you to build an automated installer for Delphi packages and libraries. The resulting setup can:

    • Install the project into a user-selected folder.
    • Copy all project files (Sources, resources, Packages, help, etc.).
    • Detect installed Delphi versions on the machine.
    • Compile packages (.dcp) to create 32-bit and 64-bit .dcu files.
    • Install packages (.bpl) into the Delphi CommonBplFolder.
    • Create environment variables and add search paths using those variables.

    During updates, the installer can uninstall previous versions (including those from Get-It), remove old sources, and clean up old .dcp and .bpl files from CommonDcpFolder and CommonBplFolder before proceeding with the new installation.

  5. What is Resource Query Language (RQL)?

    master
    Resource Query Language (RQL) is a query language designed for use in URIs with object-style data structures. It uses a set of nestable named operators with arguments, providing an extensible grammar that is URL-friendly. DelphiMVCFramework supports RQL natively, and the MVCActiveRecord framework implements a large subset of the RQL specifications for database querying.
  6. Understand Redis Key Formats for Rate Limiting

    master

    The rate limit middleware stores state in Redis using a specific key pattern: {prefix}:{keytype}:{identifier}. This allows for different types of rate limiting (e.g., by IP, User ID, or API Key) to coexist under the same prefix.

    Key types include:

    • rlkIPAddress (Type 0)
    • rlkUserID (Type 1)
    • rlkAPIKey (Type 2)
    • rlkCustomHeader (Type 3)

    The key lifecycle follows these steps:

    1. First Request: Key is created with value 1 and a TTL (Time To Live) is set to the window duration.
    2. Subsequent Requests: The key is incremented atomically using the Redis INCR command.
    3. After Window: The key expires automatically via Redis TTL.
    4. Next Window: A new key is created.
  7. Configure Auto-Validation Attributes

    master

    The generator automatically infers validation attributes from the database schema. In version 3.5.0-silicon and later, these are ON by default.

    • AUTO_REQUIRED: Emits [MVCRequired] on every NOT NULL column except auto-generated primary keys.
    • AUTO_MAXLENGTH: Emits [MVCMaxLength(N)] on bounded VARCHAR / NVARCHAR columns. TEXT / CLOB are skipped.

    To disable these, set the corresponding key to false in your .env file or use the CLI flags --no-auto-required or --no-auto-maxlength.

  8. Handle host-incompatible tests in DMVCFramework

    master

    Some tests exercise behaviors that front-end web servers (like Apache or IIS) might override or filter, such as custom status reason phrases, response compression, or permissive URL parsing. To prevent these from being marked as failures, they are categorized using DUnitX tags.

    Test Categories

    • [Category('NotOnApache')]: Skipped during tests-apache runs.
    • [Category('NotOnIIS')]: Skipped during tests-isapi runs.
    • [Category('NotOnApache,NotOnIIS')]: Skipped during both Apache and ISAPI runs.

    The invoke tasks automatically pass the --exclude flag to the DUnitX runner based on these categories so that a successful run reports 0 failed, 0 errored on every host.

  9. Configure Fail-Open vs Fail-Closed Behavior

    master

    By default, the Redis rate limit middleware is configured to fail-open. This means if the Redis server is unavailable, the middleware allows requests to proceed rather than blocking them. This prevents a Redis outage from causing a complete API outage.

    If you require a fail-closed approach (where requests are blocked if Redis cannot be reached), you must customize the CheckRateLimit method to raise an exception on Redis errors.

  10. Supported Parameter-Binding Modes in MinimalAPI

    master

    The MVCFramework.MinimalAPI surface supports several ways to bind incoming request data to handler arguments. The following modes are demonstrated in the showcase:

    • Dependency Injection (DI): Resolving services (e.g., IPeopleService) directly from the service container.
    • Primitive Binding: Binding simple types (like Integer) from route segments (e.g., /people/(id:int)).
    • Class Body JSON: Binding a JSON request body to a class. This supports auto-validation using TMVCValidatable and attributes like [MVCRequired], [MVCMinLength], and [MVCEmail].
    • Record Binding: Binding query string parameters to a record using the [MVCFromQueryString] attribute, allowing for per-field default values.
    • File Upload: Binding multipart form data to a TMVCFormFile argument, which provides access to FileName, Size, and ContentType.
    • Typed Array from Query: Binding repeated query string keys (e.g., ?tag=a&tag=b) to a TArray<string>.
  11. Configure dotEnv priority strategies

    master

    You can control whether values from .env files or System Environment variables take precedence by using UseStrategy with TMVCDotEnvPriority during the dotEnv build process.

    • TMVCDotEnvPriority.FileThenEnv: Values in .env files take precedence over System Environment variables.
    • TMVCDotEnvPriority.EnvThenFile: System Environment variables take precedence over values in .env files.
    // Strategy affects dotEnv.Env() behavior only:
    var dotEnv := NewDotEnv.UseStrategy(TMVCDotEnvPriority.FileThenEnv).Build();
    
    // If 'PATH' is in both the file and the OS, this returns the file value:
    var Setting2 := dotEnv.Env('PATH'); 
    
    // The OS environment remains unchanged:
    var SystemPath := GetEnvironmentVariable('PATH');