Skip to content

imhasandl/post-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CI Go Report Card GoDoc Coverage Go Version

Post Service

A microservice for post management in a social media application, built with Go and gRPC.

Overview posts by user ID

  • Update existing posts The Post Service is responsible for managing posts, comments, and interactions in the social media platform. It provides functionality for creating, retrieving, updating, and deleting posts, as well as liking/unliking posts, adding comments, and reporting inappropriate content. The service uses gRPC for communication with other services in the microservices architecture.

Prerequisitesror handling and logging.

  • Go 1.23 or latered
  • PostgreSQL database
  • RabbitMQ (for event-driven communication with other services)

Configurationfers

Create a .env file in the root directory with the following variables:

PORT=":50051"2.  Install dependencies using `go mod tidy`.
DB_URL="postgres://username:password@host:port/database?sslmode=disable"
# DB_URL="postgres://username:password@db:port/database?sslmode=disable" // FOR DOCKER COMPOSE
TOKEN_SECRET="YOUR_JWT_SECRET_KEY"
RABBITMQ_URL="amqp://username:password@host:port"

CreatePost

Creates new post

Request format

{
   "body": "new body for post"
}

Response format

{
   "id": "UUID of the post",
   "created_at": "2023-01-01T12:00:00Z",
   "updated_at": "2023-01-01T12:30:00Z",
   "posted_by": "UUID of the user who created the post",
   "body": "This is the updated content of my post",
   "views: "number of views",
   "likes": "number of post likes",
   "liked_by": "an array that stores the uuid's of user who like the post"
}

ChangePost

Updates an existing post.

Request Format

{
   "id": "UUID of the post",
   "body": "This is the updated content of my post"
}

Response Format

{
   "post": {
      "id": "UUID of the post",
      "created_at": "2023-01-01T12:00:00Z",
      "updated_at": "2023-01-01T12:30:00Z",
      "posted_by": "UUID of the user who created the post",
      "body": "This is the updated content of my post"
   }
}

DeletePost

Deletes a post.

Request Format

{
   "id": "UUID of the post"
}

Response Format

{
   "result": "Post successfully deleted"
}

LikePost

Likes a post.

Request Format

{
   "post_id": "UUID of the post",
   "liked_by": "UUID of the user liking the post"
}

Response Format

{
   "post": {
      "id": "UUID of the post",
      "created_at": "2023-01-01T12:00:00Z",
      "updated_at": "2023-01-01T12:00:00Z",
      "posted_by": "UUID of the user who created the post",
      "body": "Content of the post"
   }
}

UnlikePost

Removes a like from a post.

Request Format

{
   "post_id": "UUID of the post",
   "unliked_by": "UUID of the user unliking the post"
}

Response Format

{
   "post": {
      "id": "UUID of the post",
      "created_at": "2023-01-01T12:00:00Z",
      "updated_at": "2023-01-01T12:00:00Z",
      "posted_by": "UUID of the user who created the post",
      "body": "Content of the post"
   }
}

GetLikersFromPost

Retrieves the list of users who liked a post.

Request Format

{
   "post_id": "UUID of the post"
}

Response Format

{
   "liked_by": ["UUID1", "UUID2", "UUID3"]
}

GetPostByID

Retrieves a single post by its ID.

Request Format

{
   "id": "UUID of the post"
}

Response Format

{
   "post": {
      "id": "UUID of the post",
      "created_at": "2023-01-01T12:00:00Z",
      "updated_at": "2023-01-01T12:00:00Z",
      "posted_by": "UUID of the user who created the post",
      "body": "Content of the post"
   }
}

GetAllPosts

Retrieves all posts.

Request Format

{}

Response Format

{
   "posts": [
      {
         "id": "UUID of post 1",
         "created_at": "2023-01-01T12:00:00Z",
         "updated_at": "2023-01-01T12:00:00Z",
         "posted_by": "UUID of the user who created the post",
         "body": "Content of post 1"
      },
      {
         "id": "UUID of post 2",
         "created_at": "2023-01-02T10:00:00Z",
         "updated_at": "2023-01-02T10:00:00Z",
         "posted_by": "UUID of the user who created the post",
         "body": "Content of post 2"
      }
   ]
}

ReportPost

Reports a post for inappropriate content.

Request Format

{
   "id": "UUID of the post",
   "reason": "Reason for reporting the post"
}

Response Format

{
   "report_post": {
      "reported_at": "2023-01-01T12:00:00Z",
      "post_id": "UUID of the reported post",
      "reason": "Reason for reporting the post"
   }
}

GetAllReports

Retrieves all reported posts.

Request Format

{}

Response Format

{
   "report_post": [
      {
         "reported_at": "2023-01-01T12:00:00Z",
         "post_id": "UUID of the reported post",
         "reason": "Reason for reporting the post"
      },
      {
         "reported_at": "2023-01-02T10:00:00Z",
         "post_id": "UUID of another reported post",
         "reason": "Reason for reporting the post"
      }
   ]
}

CreateComment

Creates a new comment on a post.

Request Format

{
   "post_id": "UUID of the post",
   "comment_text": "This is a comment on the post"
}

Response Format

{
   "comment": {
      "id": "UUID of the comment",
      "created_at": "2023-01-01T12:00:00Z",
      "post_id": "UUID of the post",
      "commented_by": "UUID of the user who created the comment",
      "comment_text": "This is a comment on the post"
   }
}

DeleteComment

Deletes a comment.

Request Format

{
   "id": "UUID of the comment"
}

Response Format

{
   "status": "Comment successfully deleted"
}

ResetPosts

Resets all posts (for development purposes only).

Request Format

{}

Response Format

{
   "status": "All posts successfully reset"
}

RabbitMQ Integration

The Post Service publishes events to RabbitMQ when significant post actions occur, enabling other services to react accordingly.

Event Publication

The service publishes events to:

  • Exchange: posts.topic (topic exchange)
  • Routing Keys:
    • post.created - When a new post is created
    • post.updated - When a post is updated
    • post.deleted - When a post is deleted
    • post.liked - When a post is liked
    • post.comment.added - When a comment is added to a post
    • post.reported - When a post is reported

Message Format Example

{
   "event_type": "post.created",
   "post_id": "UUID of the post",
   "user_id": "UUID of the user who created the post",
   "timestamp": "2023-01-01T12:00:00Z",
   "data": {
      "body": "Content of the post"
   }
}

Running the Service

go run main.go

Docker Support

The service can be run using Docker:

# Build the Docker image
docker build -t post-service .

# Run the container
docker run -p 50051:50051 post-service

When deploying to different CPU architectures:

docker build --platform=linux/amd64 -t post-service .

For more details on Docker deployment, see README.Docker.md.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published