Twitter Like API

This recipe demonstrates how to create a Twitter like REST API using MongoDB (Database), JWT (API security) and JSON (Data exchange).

Models

cookbook/twitter/model/user.go

  1. loading...

cookbook/twitter/model/post.go

  1. loading...

Handlers

cookbook/twitter/handler/handler.go

  1. loading...

cookbook/twitter/handler/user.go

  1. loading...

cookbook/twitter/handler/post.go

  1. loading...

Server

cookbook/twitter/server.go

  1. loading...

API

Signup

User signup

  • Retrieve user credentials from the body and validate against database.
  • For invalid email or password, send 400 - Bad Request response.
  • For valid email and password, save user in database and send 201 - Created response.

Request

  1. curl \
  2. -X POST \
  3. http://localhost:1323/signup \
  4. -H "Content-Type: application/json" \
  5. -d '{"email":"[email protected]","password":"shhh!"}'

Response

201 - Created

  1. {
  2. "id": "58465b4ea6fe886d3215c6df",
  3. "email": "[email protected]",
  4. "password": "shhh!"
  5. }

Login

User login

  • Retrieve user credentials from the body and validate against database.
  • For invalid credentials, send 401 - Unauthorized response.
  • For valid credentials, send 200 - OK response:
    • Generate JWT for the user and send it as response.
    • Each subsequent request must include JWT in the Authorization header.

POST /login

Request

  1. curl \
  2. -X POST \
  3. http://localhost:1323/login \
  4. -H "Content-Type: application/json" \
  5. -d '{"email":"[email protected]","password":"shhh!"}'

Response

200 - OK

  1. {
  2. "id": "58465b4ea6fe886d3215c6df",
  3. "email": "[email protected]",
  4. "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0ODEyNjUxMjgsImlkIjoiNTg0NjViNGVhNmZlODg2ZDMyMTVjNmRmIn0.1IsGGxko1qMCsKkJDQ1NfmrZ945XVC9uZpcvDnKwpL0"
  5. }

Twitter Like API - 图1tip

Client should store the token, for browsers, you may use local storage.

Follow

Follow a user

  • For invalid token, send 400 - Bad Request response.
  • For valid token:
    • If user is not found, send 404 - Not Found response.
    • Add a follower to the specified user in the path parameter and send 200 - OK response.

POST /follow/:id

Request

  1. curl \
  2. -X POST \
  3. http://localhost:1323/follow/58465b4ea6fe886d3215c6df \
  4. -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0ODEyNjUxMjgsImlkIjoiNTg0NjViNGVhNmZlODg2ZDMyMTVjNmRmIn0.1IsGGxko1qMCsKkJDQ1NfmrZ945XVC9uZpcvDnKwpL0"

Response

200 - OK

Post

Post a message to specified user

  • For invalid request payload, send 400 - Bad Request response.
  • If user is not found, send 404 - Not Found response.
  • Otherwise save post in the database and return it via 201 - Created response.

POST /posts

Request

  1. curl \
  2. -X POST \
  3. http://localhost:1323/posts \
  4. -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0ODEyNjUxMjgsImlkIjoiNTg0NjViNGVhNmZlODg2ZDMyMTVjNmRmIn0.1IsGGxko1qMCsKkJDQ1NfmrZ945XVC9uZpcvDnKwpL0" \
  5. -H "Content-Type: application/json" \
  6. -d '{"to":"58465b4ea6fe886d3215c6df","message":"hello"}'

Response

201 - Created

  1. {
  2. "id": "584661b9a6fe8871a3804cba",
  3. "to": "58465b4ea6fe886d3215c6df",
  4. "from": "58465b4ea6fe886d3215c6df",
  5. "message": "hello"
  6. }

Feed

List most recent messages based on optional page and limit query parameters

GET /feed?page=1&limit=5

Request

  1. curl \
  2. -X GET \
  3. http://localhost:1323/feed \
  4. -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0ODEyNjUxMjgsImlkIjoiNTg0NjViNGVhNmZlODg2ZDMyMTVjNmRmIn0.1IsGGxko1qMCsKkJDQ1NfmrZ945XVC9uZpcvDnKwpL0"

Response

200 - OK

  1. [
  2. {
  3. "id": "584661b9a6fe8871a3804cba",
  4. "to": "58465b4ea6fe886d3215c6df",
  5. "from": "58465b4ea6fe886d3215c6df",
  6. "message": "hello"
  7. }
  8. ]