This is a Go-based microservice for managing posts and comments. It provides a RESTful API for user registration, authentication, creating posts, and adding comments to posts.
- Go 1.16 or later
- PostgreSQL 12 or later
-
Clone the repository:
git clone https://github.com/yourusername/post-comment-service.git cd post-comment-service
-
Install dependencies:
go mod tidy
-
Set up the PostgreSQL database:
- Create a new database named
post_comments_db
- Create a user with appropriate permissions
- Create a new database named
-
Update the
config.yaml
file in theinternal/config/
directory with your database credentials:database: url: "postgres://yourusername:yourpassword@localhost:5432/post_comments_db?sslmode=disable"
-
Run database migrations:
go run tools/create_migration.go -name <your_migration_name>
To start the service, run:
go run cmd/main.go
The service will start on localhost:8080
by default.
POST /register
: Register a new userPOST /login
: Authenticate and receive a JWT tokenGET /posts
: List all postsPOST /posts
: Create a new post (requires authentication)GET /posts/{id}
: Get a specific postPOST /posts/{postID}/comments
: Add a comment to a post (requires authentication)GET /posts/{postID}/comments
: Get all comments for a post
Here are some curl commands to interact with the API:
-
Register a user:
curl -X POST http://localhost:8080/register -H "Content-Type: application/json" -d "{\"username\":\"testuser\",\"password\":\"testpassword\"}"
-
Login:
curl -X POST http://localhost:8080/login -H "Content-Type: application/json" -d "{\"username\":\"testuser\",\"password\":\"testpassword\"}"
-
Create a post (replace
YOUR_JWT_TOKEN
with the token received from login):curl -X POST http://localhost:8080/posts -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_JWT_TOKEN" -d "{\"title\":\"My First Post\",\"content\":\"This is the content of my first post.\"}"
-
Get all posts:
curl -X GET http://localhost:8080/posts
-
Add a comment to a post (replace
POST_ID
andYOUR_JWT_TOKEN
):curl -X POST http://localhost:8080/posts/POST_ID/comments -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_JWT_TOKEN" -d "{\"content\":\"This is a comment on the post.\"}"