Skip to content

Boilerplate code for API application using .net 9, Batteries Included

License

Notifications You must be signed in to change notification settings

FullstackCodingGuy/DotNet-API-Boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DotNet API Boilerplate

Overview

This project is a boilerplate for building .NET API applications with various features such as authentication, rate limiting, CORS, and logging using Serilog.

.NET

Features

  • Vertical Slicing Architecture
  • Swagger
  • Minimal API
  • Authentication using JWT Bearer tokens
  • Authorization
  • Rate limiting to prevent API abuse
  • CORS policies for secure cross-origin requests
  • Response caching and compression
  • Logging with Serilog
  • Health check endpoint
  • Middlewares
  • Entity Framework
  • Serilog
  • FluentValidation
  • Vault Integration
  • MQ Integration
  • Application Resiliency
  • Performance
    • Response Compression
    • Response Caching
    • Metrics
  • Deployment
    • Docker
    • Podman
    • CloudFormation
  • CI
    • Github Action

Getting Started

Prerequisites

Installation

  1. Clone the repository:

    git clone https://github.com/FullstackCodingGuy/netapi-boilerplate.git
    cd netapi-boilerplate
  2. Install the required NuGet packages:

    dotnet restore
  3. Update the appsettings.json and appsettings.Development.json files with your configuration.

Running the Application

  1. Build and run the application:
    dotnet run
    
    or
    
    dotnet run --launch-profile "https"
    

1.1 Setting cert

dotnet dev-certs https -ep ${HOME}/.aspnet/https/dotnetapi-boilerplate.pfx -p mypassword234
dotnet dev-certs https --trust

Running with Docker

// To build the image
docker-compose build

// To run the container
docker-compose up

// To kill container
docker-compose down

  1. The application will be available at http://localhost:8000 and https://localhost:8001 (or the configured URL).

Health Check Endpoint

The application includes a health check endpoint to verify that the API is running. You can access it at:

GET /health

This will return a simple "Healthy" message.

Logging with Serilog

Serilog is configured to log to the console and a file with daily rotation. You can customize the logging settings in the serilog.json file.

Example configuration in Program.cs:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()   // Log to console
    .WriteTo.File("logs/api-log.txt", rollingInterval: RollingInterval.Day) // Log to a file (daily rotation)
    .Enrich.FromLogContext()
    .MinimumLevel.Information()
    .CreateLogger();

builder.Host.UseSerilog();

Additional Configuration

  • Authentication: Configure the JWT Bearer options in Program.cs to match your Keycloak settings.
  • CORS: Update the CORS policies in Program.cs to match your frontend URLs.
  • Rate Limiting: Adjust the rate limiting settings in Program.cs as needed.