[Ballerina] Social Media Service

The sample is based on a simple API written for a social-media site (like twitter) which has users, associated posts and followers. Following is the high level component diagram.

drawing

Following is the entity relationship diagram.

drawing

Following is the service description.

type SocialMedia service object {
    *http:Service;

    // users resource
    resource function get users() returns User[]|error;
    resource function get users/[int id]() returns User|UserNotFound|error;
    resource function post users(@http:Payload NewUser newUser) returns http:Created|error;
    resource function delete users/[int id]() returns http:NoContent|error;

    // posts resource
    resource function get users/[int id]/posts() returns PostMeta[]|UserNotFound|error;
    resource function post users/[int id]/posts(@http:Payload NewPost newPost) returns http:Created|UserNotFound|PostForbidden|error;
};

Following are the features covered by the scenario.

  1. Writing REST APIs with verbs, URLs, data binding and status codes
  2. Accessing databases
  3. Configurability
  4. Data transformation with the data mapper
  5. HTTP client
  6. Resiliency - Retry
  7. Writing tests
  8. Using connectors - Twilio
  9. OpenAPI specification, client stubs and central
  10. Adding validations
  11. Security - OAuth2
  12. Error handlers
  13. Ballerina concurrency
  14. Integrating a message broker
  15. Observability - Tracing
  16. Docker image generation

Setup each environment

You can use the below docker compose commands.

  1. docker compose up

Try out