A simple RESTful blog API built with Go featuring CRUD operations for blog posts.
- Create blog posts
- Read all posts or individual posts
- Update existing posts
- Delete posts
- Thread-safe in-memory storage
- JSON API responses
- Make sure you have Go installed (1.21 or later)
- Install dependencies:
go mod downloadgo run main.goThe server will start on http://localhost:8080
POST /posts
Content-Type: application/json
{
"title": "My First Post",
"content": "This is the content of my first blog post.",
"author": "John Doe"
}GET /postsGET /posts/{id}PUT /posts/{id}
Content-Type: application/json
{
"title": "Updated Title",
"content": "Updated content",
"author": "John Doe"
}DELETE /posts/{id}curl -X POST http://localhost:8080/posts \
-H "Content-Type: application/json" \
-d '{"title":"Hello World","content":"This is my first post","author":"Jane"}'curl http://localhost:8080/postscurl http://localhost:8080/posts/1curl -X PUT http://localhost:8080/posts/1 \
-H "Content-Type: application/json" \
-d '{"title":"Updated Post","content":"Updated content","author":"Jane"}'curl -X DELETE http://localhost:8080/posts/1Each blog post contains:
id- Auto-generated unique identifiertitle- Post titlecontent- Post contentauthor- Post authorcreated_at- Timestamp when post was createdupdated_at- Timestamp when post was last updated
- Data is stored in memory and will be lost when the server stops
- The application uses gorilla/mux for routing
- Thread-safe operations using mutex locks