Skip to content

EngageLabsIt/MinimalApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MinimalApi

A way to pack minimal APIs into configurable modules.

Continuous Integration

Continuous Deployment

Table of Contents

Description

This project aims to provide a minimal API framework that allows you to easily configure and organize your API endpoints into modules. It provides a simple and lightweight solution for building APIs with minimal overhead.

Usage

Create your module

To define a new module, just create a class inheriting from ApiModule and provide a Prefix for your endpoints and a body for the RegisterEndpoints method.

REMEMBER: All modules are already prefixed with api/ when they are registered in the application.

using EngageLabs.MinimalApi;

public class MyModule : ApiModule
{
    public override string Prefix => "my-module";

    public override void RegisterEndpoints(IEndpointRouteBuilder app)
    {
        app.MapGet("", async (PostsRepository repository) =>
        {
            var posts = await repository.GetPostsAsync().ConfigureAwait(false);
            return Results.Ok(posts);
        });

        // ...
    }
}

Register your module

To register all of your modules, just call the MapApi() extension method in Program.cs.

using EngageLabs.MinimalApi;

var builder = WebApplication.CreateBuilder(args);

// Your service registrations

var app = builder.Build();

// Your middleware

app.MapApi(typeof(Program).Assembly); // <-- Register api modules

app.Run();

The MapApi() extension method accepts 2 parameters:

  1. An assembly (or an array of assemblies) to scan and register all the modules and their related endpoints.

  2. A configuration action to define the behavior of the entire API. This is a good spot, for example, to configure application-wide logic like authentication, rate limiting, etc.

Customize your module

If you need to define some behaviors common to all the endpoints of the module, you can override the Configure method of the ApiModule base class. You will be provided with an IEndpointConventionBuilder to define your conventions for the module.

The Configure method is a good spot, for example, to configure OpenAPI information like name, description, tags (also used in the Swagger page), etc., or some behaviors that differ from the application-wide ones defined in the global configuration.

using EngageLabs.MinimalApi;

public class MyModule : ApiModule
{
    // ... Other module code ...

    public override void Configure(IEndpointConventionBuilder group)
    {
        // Your conventions here
        group.RequireAuthorization()
            .RequireRateLimiting.RequireRateLimiting(10, TimeSpan.FromMinutes(1));

        group.WithName("My Module")
            .WithDescription("My awesome API module")
            .WithTags("API Module");
    }
}

NOTE: Conventions defined in the Configure method can be overridden by a single endpoint. For example, you can configure your module with RequireAuthentication() but have one or more MapGet().AllowAnonymous() in the RegisterEndpoints method. See showcase app readme for more info.

Sample code

See the showcase app readme for an overview of the showcase app with some examples.

Sample code

See the showcase app readme for an overview of the showcase app with some examples

About

A way to pack minimal api into configurable modules

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages