Many times we need to publish RESTful microservices, well documented and deployed in Docker containers (Kubernetes, Google Cloud Run, Serverless platforms etc.).
This playground project shows many features that you can reuse in your projects.
It is a live project and will likely be updated frequently.
- The app do many things. Many Web api endpoints are exposed when you run the project.
- See documentation (default page when project runs)
-
.NET 6.0 webapi application
- C#
- REST WebAPI
- Objects and Json parser
- Return Codes (HTTP StatusCodes) manipulation
- Memory Caching
- Hashing generation and hash validations
- Native C# data types extensions / override
-
Docker
- docker build
- docker run
- docker container http/https interaction
- Erick is a Senior Backend Developer and Architect.
- You can reach Erick by email [email protected] or Linkedin https://www.linkedin.com/in/seixaserick/
- Other Github Repositories: https://github.com/seixaserick/
- MIT License (please check LICENSE.txt for more details)
If you already installed Git for Windows, run commands below:
git clone https://github.com/seixaserick/dotnet-webapi-playground
cd dotnet-webapi-playground
If you already installed Docker Desktop, just follow these steps below
To create a Docker image, run command line below in the command prompt of project directory:
docker build -t dotnet-webapi-playground -f Dockerfile .
Setup a Redis server in Docker if you haven't an Redis instance
docker run --name redis -d -p 6379:6379 -e REDIS_PASSWORD=supersecretpassword --restart always redis:latest /bin/sh -c 'redis-server --appendonly yes --requirepass ${REDIS_PASSWORD}'
To run the image in Docker container and interact with it, run command line below:
docker run -it -p 1977:80 --name=dotnet-webapi-playground --restart=always dotnet-webapi-playground
Open: http://localhost:1977/square/8 (without https)
To stop the container, run command line below:
docker stop dotnet-webapi-playground
To remove the container (even if it is running), run command line below:
docker rm --force dotnet-webapi-playground
After clone the repository you can open the project with Visual Studio. Build or just debug it pressing
F5
.
Open the solution file
dotnet-webapi-playground.sln
with your Visual Studio, then pressF5
to run the project!
- Swagger is a great documentation generator
- C# WebApis are very light to run inside Docker Containers with lesse than 256MB RAM
- Json Parse is made by dotnet in an easy and lightweight way
- Docker is a nice tool to run projects quickly without concern about dependencies and configurations.
- C# is very similar to other ECMA-based languages (Java, Javascript etc.)
- Hashing algorithms are "one way" only. You can't recover the inputstring from a given hash.
- Response codes can be easily customized in the API response (example: HTTP 409 Conflict, HTTP 402 PaymentRequired etc.). See the complete HTTP response codes.
- Good Markdown README.md files can help other developers to understand, clone, run and test projects.
- Create a new Object Model in the Models folder and create a new api endpoint (HTTP GET method) to do some calculations and return this new object.
- Create a new C# data type extension and use it to manipulate your native datatypes. Example: Create a
string extension
to do.ToBase64Encode()
, then you can tryinputString.ToBase64Encode();
instead Function approach. - Create a new POST API endpoint to receive some object and do something with it, returning a result.
- Try to implement Redis distributed cache in some endpoint.