This project is a boilerplate for building .NET API applications with various features such as authentication, rate limiting, CORS, and logging using Serilog.
- 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
- .NET 9 SDK
- Keycloak for authentication
-
Clone the repository:
git clone https://github.com/FullstackCodingGuy/netapi-boilerplate.git cd netapi-boilerplate
-
Install the required NuGet packages:
dotnet restore
-
Update the
appsettings.json
andappsettings.Development.json
files with your configuration.
- 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
- The application will be available at
http://localhost:8000
andhttps://localhost:8001
(or the configured URL).
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.
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.