Skip to content

renjiniravath/social-media-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Social Media API

This API is used by the Social Media Application to add/list posts, vote and comment posts. The router used is itty-router. Data is stored in KV, a key-value database provided by Cloudflare. Refer Workers documentation to get an idea of how workers work.

The APIs accepts data in JSON.

Setting Up

You have to set up a file named wrangler.toml in the root of your file to start the API. It should contain keys as listed below.

name = "social-media-api"
type = "webpack"

account_id = "***YOUR_ACCOUNT_ID"
workers_dev = true
route = ""
zone_id = ""
compatibility_date = "**compatibility_date**"
kv_namespaces = [
    { binding = "POSTS_KV", preview_id = "**kv_preview_id**", id = "**kv_id**" }
]

[env.production]
kv_namespaces = [
    { binding = "POSTS_KV", id = "**kv_id_for_production**"}
]

Endpoints

General Error Response

{
    "status": 404,
    "error": "Post not found"
}

GET /posts

Success Response Body

[
    {
        "id": "Slash-1637743242549",
        "title": "Post Title",
        "username": "Slash",
        "content": "Post description",
        "comments": [
            {
                "id": "Slash-1637743258827",
                "text": "Inspirational post!",
                "username": "Slash"
            }
        ],
        "votes": {
            "count": 1,
            "map": {
                "Slash": 1
            }
        },
        "media": {}
    }
]

POST /posts

Request Body

{
    "title": "My first post",
    "content": "Post content",
    "username": "Slash"
}

Success Response Body

{
    "status": 200,
    "message": "Success"
}

POST /posts/:id/comment

Request Body

{
    "username": "Slash",
    "comment": "Great post!!"
}

Success Response Body

{
    "status": 200,
    "message": "Success"
}

POST /posts/:id/vote

Request Body

{
    "username": "Slash",
    "vote": 1
}

Success Response Body

{
    "status": 200,
    "message": "Success"
}

Running Development

Run the below command to start your development server.

wrangler dev

The API will be server in http://127.0.0.1:8787

Deploying Production

Run the below command to start your development server.

wrangler publish --env production