Lab repository to test out Docker webapi develoment, testing and CI.
I've created this repo as a laboratory to test how I could develop a simple container image composed of a WebApi and a WebServer. Unit tests and CI and Redis are a plus.
The chosen technologies where .NET Core as framework and nginx as webserver. I've also implemented xUnit tests and AppVeyor for continuous integration and Redis as caching layer.
Docker was also used, obviously.
This small guide should help you run the Docker image and run tests.
This WebAPI was developed in ASPNet Core
so in order to build, publish and run tests, you must have dotNet Core
installed on your environment.
Check out their download page and follow the installation instructions for your system.
After installing, open a terminal window and enter dotnet --version
to check if the installation was successful:
Now you should:
dotnet restore
dotnet build
dotnet publish
This will allow you to correctly build the WebAPI Docker container.
In order to run the Docker image, you must have Docker installed in you Windows, Linux or Mac. After installing Docker on your system and certifying that it is running, you should:
~\web-api-lab\docker-compose build
~\web-api-lab\docker-compose up
The result should look like this:
After that is done, you can reach the /user
endpoint on your localhost/api/user
and get this (I'm using Postman to test it):
You're done! You can now run GET
, POST
, PUT
and DELETE
data from/to your database though the WebApi.
For more info and examples on the API usage, please visit it's Postman Documenter Page.
Alternatively to the aforementioned building steps, you can simply run one of the scripts on the project's root folder.
On Windows, run the
PowerShell
scriptbuild.ps1
.
On Linux and Mac, run the
bash
scriptbuild
.
After runing one of the scripts, docker-compose up
to start all containers.
All the tests are on the user-webapitests
project inside the folder with the same name.
If using Visual Studio 2017+, you can open the whole solution and run tests on the user-webapitests
project:
Alternatively, you can run the tests simply by dotnet test
from the project's root folder (user-web-api
)
I used Redis to create a caching layer that can be easily set by making a GET
request:
localhost/api/user/SetUsersCacheData
If successful, this will return a status
response set to true
:
Likewise, you can retrieve cached data by making a GET
request:
localhost/api/user/GetUsersCacheData
If successful, this will return a string
containing all cached data:
To get an specific entry, you can GET
to GetUserCacheDataById
adding the desire id
at the URI end:
localhost/api/user/GetUserCacheDataById/1
The result is a string
of the desired entry:
In case a not-existing id
is sent, the result will be:
To remove the cached data, GET
to:
localhost/api/user/RemoveUsersCacheData
The expected result is a Boolean
set to true
: