Skip to content

Simplified Instagram API clone made with .NET 9.0, .NET Aspire (orchestration, observability), MinIO (object storage) and PostgreSQL (DB).

License

Notifications You must be signed in to change notification settings

ch200c/InstagramClone

Repository files navigation

InstagramClone

Simplified Instagram API clone made with .NET 9.0, .NET Aspire (orchestration, observability), MinIO (object storage) and PostgreSQL (DB).

Solution structure

  • InstagramClone.AppHost: main entry point of the application, orchestrating other projects using .NET Aspire.
  • InstagramClone.Api: API project. Uses ASP.NET Core Identity to handle authentication, generates OpenAPI documents and has Scalar client.
  • InstagramClone.ServiceDefaults: class library that adds common .NET Aspire services. Used by InstagramClone.Api.
  • InstagramClone.DataSeeding: console app for generating test data.

Running locally with Docker

Please perform these steps in the sequence they are listed.

MinIO

This command will create and use C:/docker-sandbox/minio directory as a persistent storage for the container. You can use any other file path that you have read, write and delete access. More information can be found in MinIO documentation here.

docker run -p 9000:9000 -p 9001:9001 --name minio1 -v C:/docker-sandbox/minio:/data -e "MINIO_ROOT_USER=admin" -e "MINIO_ROOT_PASSWORD=min8chars" -d --rm quay.io/minio/minio:RELEASE.2024-12-18T13-15-44Z server data --console-address ":9001"

Once the container is running, you can use http://localhost:9001/ to access MinIO Console.

PostgreSQL

docker run --name postgres1 -e POSTGRES_PASSWORD=1234 -p 5432:5432 -d --rm postgres:17

Database migrations

To perform database migrations, you can use EF Core tools CLI and execute the following command from solution's root directory.

dotnet ef database update --project InstagramClone.Api

InstagramClone.AppHost

This is the main entry point of the application. You can run it from your favorite IDE or using dotnet CLI with the following command from solution's root directory.

dotnet run --project InstagramClone.AppHost

Upon successful run, your console output should have the URL to login to the .NET Aspire dashboard. Open that URL and use token from console output if prompted.

Login to the dashboard at https://localhost:17219/login?t=token

.NET Aspire dashboard

Optional: Data Seeding

Once the API is running, you can generate some test users, images and user followings by executing the following command from solution's root directory.

dotnet run --project InstagramClone.DataSeeding

You can easily configure the amount of users, images and user followings by tweaking variables in InstagramClone.DataSeeding project's Program.cs.

Using the application

Currently, the functionality is only contained within the API. You can find its URL by clicking on api resource in .NET Aspire dashboard and then finding https target port endpoint. Its port can change between different runs due to reverse proxy created by .NET Aspire. More inforamtion can be found here.

Following the image above:

Authentication

API endpoints require authentication. In this solution, it is handled with ASP.NET Core Identity. You can find all of the authentication related endpoints in the documentation here.

One of the most basic ways to authenticate is to use /register endpoint to register a user and then use /login to login with that user. See example using cookie-based authentication below.

/register

{
  "email": "[email protected]",
  "password": "Abc123!"
}

/login?useCookies=true

{
  "email": "[email protected]",
  "password": "Abc123!"
}

API

HTTP POST /api/media allows uploading images to the object storage. It conveniently creates a bucket for the user performing the request and all following user uploads will be performed to that bucket. On success, this endpoint returns an ID you can then query at api/media/{id}/preview or api/media/{id}/. Moreover, after a successful upload you can also view the image in MinIO Console.

Media preview endpoint Media endpoint Database view MinIO preview


HTTP POST /api/userFollowing allows following other users.


HTTP GET /api/feed returns the feed for user performing the request. It returns latest media from followed users.

TODO

  • Error handling
  • Request validation
  • Move endpoint logic out of controllers
  • Check generated EF Core queries and potential issues
  • Add indexes
  • Use Guid instead of default string for Identity PK
  • Handle empty title search
  • Detect and support multiple media types
  • Token based authentication may not be represented correctly due to scalar/scalar#4055
  • Use TypedResults when dotnet/aspnetcore#44988 is resolved

About

Simplified Instagram API clone made with .NET 9.0, .NET Aspire (orchestration, observability), MinIO (object storage) and PostgreSQL (DB).

Topics

Resources

License

Stars

Watchers

Forks