Skip to content

Multi-process REST architecture for a gas container (server); input/output clients adjust mass by pressure thresholds.

License

Notifications You must be signed in to change notification settings

zahi1/restful-api-architecture

Repository files navigation

Gas Pressure Container — REST API System

A multi-process simulation of a gas container controlled via a REST API. A server simulates the container’s physics (mass, temperature, pressure) and exposes endpoints. Independent input and output clients read the pressure and add/remove gas mass according to configurable thresholds. The container temperature drifts every 2 seconds; if pressure crosses safety limits the container is marked destroyed and then reset.

This repository documents the REST implementation.


Project structure / architecture

  • RestGasPressure/ — ASP.NET Core Web API (the server)
    • Controllers/GasPressureController.cs exposes the REST endpoints
    • GasContainerLogic.cs holds state and business logic
    • Swagger/OpenAPI available at /swagger
  • input/ — input client
    • Periodically calls the API; when pressure is below the lower limit it increases mass
    • Client class generated with NSwag (InputClient.cs)
  • output/ — output client
    • Periodically calls the API; when pressure is above the upper limit it decreases mass
    • Client class generated with NSwag (OutputClient.cs)

Processes run independently and communicate only through HTTP.


How it works

  • State and physics

    • Temperature (K), mass (arbitrary units), pressure computed as P = (m * T) / 22.4
    • Background loop (server) changes temperature randomly every 2s and checks limits
    • Limits (defaults from code):
      • Lower add limit: PressureLimit = 100 — inputs can add mass only when pressure is below this value
      • Upper remove limit: UpperPressureLimit = 150 — outputs can remove mass only when pressure is above this value
      • Implosion limit: ImplosionLimit = 10 — below this, container is destroyed
      • Explosion limit: ExplosionLimit = 200 — above this, container is destroyed
    • When destroyed, the server logs the event and resets state to initial values
  • Clients’ behavior

    • Input client: reads pressure; if < 100, posts a small random mass increase
    • Output client: reads pressure; if > 100 (client-side threshold), it attempts a small random mass decrease; the server only applies removal when pressure is above the upper limit (> 150 by default)
    • Both clients stop acting if the container is destroyed (until it’s reset by the server’s loop)
  • Concurrency and safety

    • All state mutations are protected by a lock in GasContainerState
    • Server operations are non-blocking; the periodic work is done in a background thread

REST API

Base URL: http://127.0.0.1:5000

  • POST /increaseMass?mass=<double> — add mass if pressure is below lower limit
  • POST /decreaseMass?mass=<double> — remove mass if pressure is above upper limit
  • GET /getPressure — returns the current pressure (double)
  • GET /isContainerDestroyed — returns true/false

OpenAPI/Swagger UI: http://127.0.0.1:5000/swagger

Client proxies are generated with NSwag and checked in (input/InputClient.cs, output/OutputClient.cs).


Run locally

Prerequisites:

  • .NET 8 SDK

In separate terminals, run the server and each client.

Server (port 5000):

cd RestGasPressure
dotnet run

Input client:

cd input
dotnet run

Output client:

cd output
dotnet run

Optional quick test with curl:

curl http://127.0.0.1:5000/getPressure
curl -X POST "http://127.0.0.1:5000/increaseMass?mass=2"
curl -X POST "http://127.0.0.1:5000/decreaseMass?mass=1"
curl http://127.0.0.1:5000/isContainerDestroyed

Logs are written to stdout via NLog in all processes.


Configuration notes

  • Port: configured in RestGasPressure/Program.cs to listen on 127.0.0.1:5000
  • CORS: open policy enabled to allow any origin/method/header (for easy local testing)
  • Swagger: XML comments included; UI enabled by default

Design highlights

  • Clear separation of concerns: controller (API), logic (state/physics), networking (generated clients)
  • Contracts published via OpenAPI; strongly-typed clients generated from the contract
  • Continuous cycles for each component; robust to transient HTTP errors
  • Thread-safe state updates; checks for implosion/explosion conditions

License

This project is licensed under the MIT License. See the LICENSE file for details.


Credits

  • ASP.NET Core for the server
  • NSwag for client generation
  • NLog for logging

About

Multi-process REST architecture for a gas container (server); input/output clients adjust mass by pressure thresholds.

Topics

Resources

License

Stars

Watchers

Forks

Languages