In this project I am designing a URL shortener Web API using .NET 5, REST API, Postgres and Redis. This application is deployable separately as a standalone service using Docker and Kubernetes (they both are mutually exclusive, keeping both as example for new learners). I have used following architecture and patterns so to say:
- Request driven architecture
- Repository pattern
- Dependency injection
- Self registered services with scoped lifetime
- Request handling by services
- Code first approach
- Persistent object
- Docker/Kubernetes
Note: updating... the project is ongoing and let's say 95% finished. Next todo includes verify unique shortened key.
Note: this is neither production ready, nor configured to run in live servers. For example: postgres security, encryption etc. are not included.
product | version |
---|---|
MacOS | Ventura 13.0.1 |
Visual Studio | 2022 |
Visual Studio Code | 1.73.1 |
.NET | 5.0 |
dotnet | 5.0.400 |
dotnet ef | 6.0.3 |
PostgreSQL | 12.0 |
Docker desktop | 20.10.21 |
Kubernetes | 1.25.1 |
- Install
dotnet-ef
if needed, check documentation. - Create ef migration:
dotnet ef migrations add InitialCreate
. In this branch,InitialCreate
has been already created, checkMigrations
directory. - Then apply the database (double check connection string):
dotnet ef database update
- Update schema (if changed after creation):
dotnet ef database update 0
- Drop the migration:
dotnet ef migrations remove
From root directory where .sln
is located (add --project "Another URL Shortener"
suffix in following commands) or from Another URL Shortener
where .csproj
is located:
dotnet build
dotnet restore
dotnet run
Request to: https://localhost:5001/api/ShortUrls/
- Create Dockerfile and .dockerignore (already created)
- Update Postgres configuration
pg_hba.conf
to accept all requests:host all all 192.168.0.127/0 trust
*** - and update Postgres configuration
postgresql.conf
to listen on all addresses:listen_addresses = '*'
*** - Then restart Postgres service
- Correct
ConnectionStrings
: update host address inDockerAppLocalSql
- Add
DockerAppLocalSql
inStartup.cs
*** Reference: Stackoverflow, thegeekstuff
From /docker/
directory execute following commands:
docker build -t another-url-shortener-image -f Dockerfile ../Another\ URL\ Shortener
- THIS:
docker create -p 44326:80 --name another-url-shortener-container another-url-shortener-image
docker start another-url-shortener-container
- OR THIS
docker run -rm -p 44326:80 another-url-shortener-image
Request to: http://localhost:44326/api/ShortUrls/
- 'Prepare Postgres' step needed.
- Docker image build needed.
- Create Kubernetes deploy services (already created in
/docker/deploy-webapi.yml
and/docker/deploy-webapp.yml
) - Execute from
/docker/
directory:kubectl apply -f ./
- Run
kubectl get all
and make sure theanother-url-shortener-kube-webapi
(Service) andanother-url-shortener-kube-webapp
(Load Balancer) are running:
Request to: http://localhost:44326/api/ShortUrls/
More details at Microsoft documentation.
Environment: MacOS, VS Code
crit: Microsoft.AspNetCore.Server.Kestrel[0]
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
Change "" to "/" in .dockerignore
file.