Giter Site home page Giter Site logo

seixaserick / dotnet-webapi-playground Goto Github PK

View Code? Open in Web Editor NEW
13.0 1.0 5.0 295 KB

Many Dotnet Web API examples. Documentation (Swagger / OpenAPI), ReturnCodes, Cache, Datatype Extensions and other algorithm tests.

License: MIT License

C# 94.58% Dockerfile 1.39% HTML 4.03%
algorithms api base64 base64-decoding base64-encoding core data-structures dotnet dotnet-core dotnetcore

dotnet-webapi-playground's Introduction

C# dotnet web API playground (many examples + Docker deploy)

Project Cover

Context

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.

What does this application do?

  • The app do many things. Many Web api endpoints are exposed when you run the project.
  • See documentation (default page when project runs)

Stack and main topics in the project

  • .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

About the Author and license

How to run this project

Clone the repository

If you already installed Git for Windows, run commands below:

git clone https://github.com/seixaserick/dotnet-webapi-playground
cd dotnet-webapi-playground

How to run this project in Docker containers

If you already installed Docker Desktop, just follow these steps below

Creating a docker image

To create a Docker image, run command line below in the command prompt of project directory:

docker build -t dotnet-webapi-playground -f Dockerfile .

Running the app in a docker container

Docker Build

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

Docker Running

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

How to build and run without Docker (Windows)

After clone the repository you can open the project with Visual Studio. Build or just debug it pressing F5.

To compile and run it in Windows (Visual Studio required)

Open the solution file dotnet-webapi-playground.sln with your Visual Studio, then press F5 to run the project!

Takeaways

  • 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.

Homework challenge

  1. 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.
  2. 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 try inputString.ToBase64Encode(); instead Function approach.
  3. Create a new POST API endpoint to receive some object and do something with it, returning a result.
  4. Try to implement Redis distributed cache in some endpoint.

dotnet-webapi-playground's People

Contributors

seixaserick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.