A microservice for post management in a social media application, built with Go and gRPC.
- 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.
- Go 1.23 or latered
- PostgreSQL database
- RabbitMQ (for event-driven communication with other services)
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"Creates new post
{
"body": "new body for 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",
"views: "number of views",
"likes": "number of post likes",
"liked_by": "an array that stores the uuid's of user who like the post"
}Updates an existing post.
{
"id": "UUID of the post",
"body": "This is the updated content of my post"
}{
"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"
}
}Deletes a post.
{
"id": "UUID of the post"
}{
"result": "Post successfully deleted"
}Likes a post.
{
"post_id": "UUID of the post",
"liked_by": "UUID of the user liking the post"
}{
"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"
}
}Removes a like from a post.
{
"post_id": "UUID of the post",
"unliked_by": "UUID of the user unliking the post"
}{
"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"
}
}Retrieves the list of users who liked a post.
{
"post_id": "UUID of the post"
}{
"liked_by": ["UUID1", "UUID2", "UUID3"]
}Retrieves a single post by its ID.
{
"id": "UUID of the post"
}{
"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"
}
}Retrieves all posts.
{}{
"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"
}
]
}Reports a post for inappropriate content.
{
"id": "UUID of the post",
"reason": "Reason for reporting the post"
}{
"report_post": {
"reported_at": "2023-01-01T12:00:00Z",
"post_id": "UUID of the reported post",
"reason": "Reason for reporting the post"
}
}Retrieves all reported posts.
{}{
"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"
}
]
}Creates a new comment on a post.
{
"post_id": "UUID of the post",
"comment_text": "This is a comment on the post"
}{
"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"
}
}Deletes a comment.
{
"id": "UUID of the comment"
}{
"status": "Comment successfully deleted"
}Resets all posts (for development purposes only).
{}{
"status": "All posts successfully reset"
}The Post Service publishes events to RabbitMQ when significant post actions occur, enabling other services to react accordingly.
The service publishes events to:
- Exchange:
posts.topic(topic exchange) - Routing Keys:
post.created- When a new post is createdpost.updated- When a post is updatedpost.deleted- When a post is deletedpost.liked- When a post is likedpost.comment.added- When a comment is added to a postpost.reported- When a post is reported
{
"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"
}
}go run main.goThe service can be run using Docker:
# Build the Docker image
docker build -t post-service .
# Run the container
docker run -p 50051:50051 post-serviceWhen deploying to different CPU architectures:
docker build --platform=linux/amd64 -t post-service .For more details on Docker deployment, see README.Docker.md.