Skip to content

Commit

Permalink
Merge pull request #24 from jaen003/feature/add-swagger
Browse files Browse the repository at this point in the history
FEATURE: Add Swagger integration
  • Loading branch information
jaen003 committed Nov 23, 2023
2 parents be677fe + 3d2613a commit 89ec8a3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
5 changes: 1 addition & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/Src/Api/bin/Debug/net7.0/Src.Api.dll",
"env": {
"ASPNETCORE_URLS": "http://0.0.0.0:8000"
}
"program": "${workspaceFolder}/Src/Api/bin/Debug/net7.0/Src.Api.dll"
}
]
}
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageVersion Include="System.Linq.Dynamic.Core" Version="1.2.25" />
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="Riok.Mapperly" Version="3.3.0-next.1" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"Backoffice": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://0.0.0.0:8000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ It is built with clean architecture, SOLID principles, and love ❤️.
* Product update (price, description, name)
* Product deletion.
* Automatic object mapping with Mapperly.
* Swagger integration for interactive API exploration and testing.
* Download OpenAPI specification in JSON format using Swagger.

<a name="setup"></a>
## 🔧 Setup
Expand Down Expand Up @@ -63,6 +65,24 @@ docker-compose up -d

**NOTE:** This project runs inside a private network and does not expose a public port to access the API. It is recommended to access the API through an API gateway.

## 📚 API Documentation

Explore the API using Swagger UI. The API documentation is generated dynamically and can be accessed through the Swagger UI interface. Additionally, you can download the OpenAPI specification in JSON format.

### Swagger UI

Access the Swagger UI to interactively explore and test the API endpoints.

* **Swagger UI URL:** [http://localhost:5004/swagger](http://localhost:5004/swagger)

### OpenAPI Specification (JSON)

Download the OpenAPI specification in JSON format to use with other tools.

* **Swagger JSON URL:** [http://localhost:5004/swagger/v1/swagger.json](http://localhost:5004/swagger/v1/swagger.json)

**NOTE:** Access to documentation is limited to the development environment only. If you also want to extend it to the production environment, it is advisable to implement an identity server.

## 🗃️ Migrations

To create a new migration, run the following command:
Expand All @@ -81,6 +101,8 @@ dotnet ef migrations add AddProductNameColumn -p Src/Core -o Shared/Infrastructu

You can modify any environment variables in the `.env` file.

**NOTE:** If you are considering using this application in production you should include the '.env' file in your '.gitignore' so as not to expose the security of the application.

## 🧪 Running Tests

To run the tests, run the following command:
Expand Down
1 change: 1 addition & 0 deletions Src/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="dotenv.net" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Swashbuckle.AspNetCore" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 15 additions & 8 deletions Src/Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,29 @@

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<ProductMapper, ProductMapper>();
builder.Services.AddScoped<RestaurantMapper, RestaurantMapper>();
builder.Services.AddTransient<RestaurantMapper, RestaurantMapper>();
builder.Services.AddSingleton<ApplicationLoggerCreator, ApplicationLoggerCreator>();
builder.Services.AddScoped<ILogger>(
builder.Services.AddTransient<ILogger>(
serviceProvider => serviceProvider.GetRequiredService<ApplicationLoggerCreator>().Create()
);
builder.Services.AddScoped<ApplicationExceptionHandler, ApplicationExceptionHandler>();
builder.Services.AddTransient<ApplicationExceptionHandler, ApplicationExceptionHandler>();
builder.Services.AddSingleton<RabbitmqEventBusConnection, RabbitmqEventBusConnection>();
builder.Services.AddScoped<RabbitmqMessagePublisher, RabbitmqMessagePublisher>();
builder.Services.AddTransient<RabbitmqMessagePublisher, RabbitmqMessagePublisher>();
builder.Services.AddTransient<RabbitmqConsumptionErrorHandler, RabbitmqConsumptionErrorHandler>();
builder.Services.CollectDomainEventInformation();
builder.Services.AddScoped<RabbitmqEventBusConfigurer, RabbitmqEventBusConfigurer>();
builder.Services.AddTransient<RabbitmqEventBusConfigurer, RabbitmqEventBusConfigurer>();
builder.Services.AddSingleton<RabbitmqDomainEventConsumer, RabbitmqDomainEventConsumer>();
PostgresqlDatabaseConnectionData databaseConnectionData = new();
builder.Services.AddPooledDbContextFactory<PostgresqlDatabaseContext>(
options => options.UseNpgsql(databaseConnectionData.ConnectionString),
databaseConnectionData.PoolSize
);
builder.Services.AddScoped<PostgresqlDatabaseMigrator, PostgresqlDatabaseMigrator>();
builder.Services.AddTransient<PostgresqlDatabaseMigrator, PostgresqlDatabaseMigrator>();
builder.Services.AddScoped<IDomainEventPublisher, RabbitmqDomainEventPublisher>();
builder.Services.AddScoped<IRestaurantRepository, PostgresqlRestaurantRepository>();
builder.Services.AddScoped<RestaurantCreator, RestaurantCreator>();
builder.Services.AddTransient<IRestaurantRepository, PostgresqlRestaurantRepository>();
builder.Services.AddTransient<RestaurantCreator, RestaurantCreator>();
builder.Services.AddScoped<IProductRepository, PostgresqlProductRepository>();
var app = builder.Build();

Expand All @@ -65,5 +66,11 @@

// Configure the HTTP request pipeline.

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
app.MapControllers();
app.Run();

0 comments on commit 89ec8a3

Please sign in to comment.