项目作者: patelniks58

项目描述 :
C# dotnet core api using kubernetes deployment, also using docker, error logging, health checks, readiness check, liveness check, unit test using xunit, postman automation for integration test
高级语言: C#
项目地址: git://github.com/patelniks58/dotnetcore-boilerplate-with-kubernetes.git


netcore-boilerplate

Boilerplate of API in .NET Core 3.1

Boilerplate is a piece of code that helps you to quickly kick-off a project or start writing your source code. It is kind of a template - instead
of starting an empty project and adding the same snippets each time, you can use the boilerplate that already contains such code.
The Project is configured to run unit tests while pushing, to setup your local for this, run
git config core.hooksPath git-hooks/

Source code contains

  1. Swagger + Swashbuckle
  2. HealthChecks
    Application has 2 types of Health checks, Liveness and Readiness. These are exposed via 3 endpoints:
    Visit http://localhost:5000/health to run ALL health checks
    Visit http://localhost:5000/health/live to run Liveness only health checks
    Visit http://localhost:5000/health/ready to run Readiness only health checks

Sample response from health checks

  1. {
  2. "status": "Healthy",
  3. "results": {
  4. "LivenessHealthCheck": {
  5. "status": "Healthy",
  6. "description": "Live and Healthy!",
  7. "data": {}
  8. },
  9. "ReadinessCredentialsHealthCheck": {
  10. "status": "Healthy",
  11. "description": "Ready and Healthy!",
  12. "data": {}
  13. },
  14. "ReadinessPingHealthCheck": {
  15. "status": "Healthy",
  16. "description": "SAMPLE_VARIABLE endpoint is up!",
  17. "data": {}
  18. }
  19. }
  20. }
  1. Tests
  2. Code quality
  3. Docker
  4. Serilog

Architecture

Api

Coop.Sample.Api

Core

Coop.Sample.Core

Tests

Unit tests

Coop.Sample.Api.UnitTests
Coop.Sample.Core.UnitTests

Checking test coverage

In the test/Coop.Sample.Api.UnitTests folder,
step 1 : run dotnet test /p:CollectCoverage=true which will create coverage.json
step 2 : run dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover which will create coverage.opencover.xml
dev can filter result to remove extra Assembly using /p:Exclude=[Assembly-Filter]Type-Filter Ex. /p:Exclude=\"[Coop.Sample.Api.*]*,[*]*\", /p:Include=\"[Coop.Sample.Api.UnitTests]*,[*]Controller*\"
step 3 : install ReportGenerator tool to do that run dotnet tool install -g dotnet-reportgenerator-globaltool
step 4 : Generate Report run reportgenerator "-reports:coverage.opencover.xml" "-targetdir:report" which will create folder test/Coop.Sample.Api.UnitTests/report
step 5 : open index.html under test/Coop.Sample.Api.UnitTests/report on browser to see report.

For more information for test coverage visit https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md#source-files

How to adapt to your project

Generally it is totally up to you! But in case you do not have any plan, You can follow below simple steps:

  1. Download/clone/fork repository
  2. Remove components and/or classes that you do not need to
  3. Rename files (e.g. sln, csproj, ruleset), folders, namespaces etc.
  4. Give us a star!

Build the solution

Just execute dotnet build in the root directory, it takes Coop.Sample.sln and build everything.

Start the application

Standalone

Then the application (API) can be started by dotnet run command executed in the src/Coop.Sample.Api directory.
By default it will be available under http://localhost:5000/, but keep in mind that documentation is available under
http://localhost:5000/api-docs/.

In order to run the project locally, run
docker-compose up to run as process in terminal OR
docker-compose up -d to run in background OR
docker-compose up --build to force new image build and run (using any of the above options) in repo’s root directory. This should build (if required) and start the application on port 80.

Then visit: http://localhost/api-docs/

Overriding docker files

In order to run docker locally with custom settings,

Copy docker-compose.override-example.yml to docker-compose.override.yml.
Add credentials via environment variable section. You can also add new or update existing settings if required.
Now run docker-compose up and visit http://localhost:5000/api-docs/

The .override file overrides the base docker-compose.yml file automatically when present. This file is part of .gitignore and should never be pushed as it’s your local setup file and could also have credentials.

Run unit tests

Run dotnet test command in the root directory, it will look for test projects in Coop.Sample.sln and run them.