Skip to content

Lab repository to test out webapi dev as well as testing, CI and CD.

License

Notifications You must be signed in to change notification settings

ruirizzi/web-api-lab

Repository files navigation

web-api-lab

AppVeyor

Build status Master status Test Status

Travis CI

Build Status

Lab repository to test out Docker webapi develoment, testing and CI.

Why?

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.

Getting Started

This small guide should help you run the Docker image and run tests.

Build the Code

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 --versionto check if the installation was successful: dotnet version command result

Now you should:

dotnet restore
dotnet build
dotnet publish

This will allow you to correctly build the WebAPI Docker container.

Runing the Docker Containers

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:

docker-compose up result

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):

user list response

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.

Building and publishing in a single step

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 script build.ps1.

On Linux and Mac, run the bash script build.

After runing one of the scripts, docker-compose up to start all containers.

Runing tests

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:

how to run tests from solution explorer

Alternatively, you can run the tests simply by dotnet test from the project's root folder (user-web-api)

Cache

Setting Cache Data

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 statusresponse set to true:

status = true result

Retrieving Cached Data

Likewise, you can retrieve cached data by making a GETrequest:

localhost/api/user/GetUsersCacheData

If successful, this will return a string containing all cached data:

user list string result

To get an specific entry, you can GETto GetUserCacheDataById adding the desire id at the URI end:

localhost/api/user/GetUserCacheDataById/1

The result is a string of the desired entry:

user string result

In case a not-existing id is sent, the result will be:

id not found result

Removing Cached Data

To remove the cached data, GETto:

localhost/api/user/RemoveUsersCacheData

The expected result is a Boolean set to true:

true result

References

License

badge

About

Lab repository to test out webapi dev as well as testing, CI and CD.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages