Skip to content

michaelrambeau/bestofjs-webtasks

Repository files navigation

bestof.js.org webtasks

node.js micro-services used by bestof.js.org web application, built using https://webtask.io service.

Each "webtask" is either a plain JavaScript function either or a basic Express web server deployed on webtask.io.

Build Status

webtask #1: user-content-api

user-content-api webtask is an Express web server used to read and write content generated by users:

  • Github project reviews (user's rating and opinion about projects)
  • Github project links (resources related to projects: tutorials, blog entries...)

File system

  • src/server.js: local Express web server
  • src/webtask.js: entry point to build the file deployed to webtask.io using Webpack
  • build/webtask.js: bundled webtask built by Webpack, ignored by git

Paths

GET /reviews/

{
  "results": [
    {
      "_id": "56dc9906494eea0100152a71",
      "project": "56a95b5843bdc81100111331",
      "rating": 5,
      "createdBy": "michaelrambeau",
      "createdAt": "2016-03-06T20:54:31.003Z",
      "updatedAt": "2016-03-07T04:44:17.459Z",
      "comment": "* nice!\n* I like it!"
    },
    {
      "_id": "56e1399e6a6a290100d039aa",
      "project": "558dc8cf89fc680300dfa5a9",
      "rating": 5,
      "createdBy": "azukiwasher",
      "createdAt": "2016-03-10T09:08:46.620Z",
      "comment": ""
    }
  ]
}

POST /reviews/

Request body:

{
  "comment": "Good :)",
  "project": "55723c9f4140883353bc774c",
  "rating": 4,
  "title": "link too long"
}

Response:

{
  "_id": "5709945bbd5aabb81745b041",
  "project": "55723c9f4140883353bc774c",
  "rating": 4,
  "createdBy": "michaelrambeau",
  "createdAt": "2016-04-09T23:46:35.382Z",
  "comment": "Good :)"
}

Error messages

{
  "message": "A review by the same user already exists!"
}

PUT /reviews/:id

Links

GET /links/

{
  "results": [
    {
      "_id": "56ad8b90f6a3e89c08b88ca2",
      "title": "link",
      "url": "https://ponyfoo.com/articles/es6-promises-in-depth",
      "projects": [
        "55723c9f4140883353bc773e"
      ],
      "createdBy": "michaelrambeau",
      "createdAt": "2016-01-31T04:20:32.186Z"
    },
    {
      "_id": "56ad80027703eee4132d48e2",
      "title": "link2",
      "url": "http://development.bestofjs.divshot.io",
      "projects": [
        "56a95b5843bdc81100111331",
        "55fbaf4dc0b48f03006c6c98",
        "55aba39b8f937d03008d41c8",
        "55723c9f4140883353bc774e"
      ],
      "createdBy": "michaelrambeau",
      "createdAt": "2016-01-31T03:31:14.572Z",
      "updatedAt": "2016-03-07T04:43:42.133Z",
      "comment": "aaa\n\n**bbbb**\n\ncccc"
    },
    {
      "_id": "56adf53426d6e7181453e1bb",
      "title": "Bootstrap is cool",
      "url": "https://bestofjs.herokuapp.com/keystone/",
      "projects": [
        "55723c9f4140883353bc773e"
      ],
      "createdBy": "michaelrambeau",
      "createdAt": "2016-01-31T11:51:16.132Z",
      "comment": "yeah!"
    },
    {
      "_id": "56be7a7d9a26030100e70ca3",
      "title": "Yes it is cool",
      "url": "https://webtask.io/docs/101",
      "projects": [
        "55723c9f4140883353bc773e"
      ],
      "createdBy": "michaelrambeau",
      "createdAt": "2016-02-13T00:36:13.650Z",
      "updatedAt": "2016-02-13T07:25:46.940Z",
      "comment": "So cool !!\n* item 1\n* item 2"
    },
    {
      "_id": "56dca24bdf01f30100d38494",
      "title": "one more link",
      "url": "http://michaelrambeau.com/",
      "projects": [
        "56a95b5843bdc81100111331"
      ],
      "createdBy": "michaelrambeau",
      "createdAt": "2016-03-06T21:34:03.090Z",
      "updatedAt": "2016-04-03T22:14:34.706Z",
      "comment": "* item1\n* item2\n* item3\n* item4"
    }
  ]
}

POST /links/

Request body:

{
  "title": "my link",
  "projects": ["55723c9f4140883353bc774c"],
  "url": "http://mongoosejs.com/docs/guide.html",
  "comment": "Good :)"
}

Response:

{
  "_id": "570875279aa6dc0c08764a76",
  "title": "link",
  "url": "http://mongoosejs.com/docs/guide.html?a",
  "projects": [
    "55723c9f4140883353bc774c"
  ],
  "createdBy": "michaelrambeau",
  "createdAt": "2016-04-09T03:21:11.788Z",
  "comment": "Good :)"
}

Errors 400

{
  "message": "A link with the same URL already exists!"
}
{
  "message": "Link validation failed"
}

PUT /links/:id

Github Issues

POST /create-issue

Create an issue in the given repository.

Request body:

{
    "repo": "michaelrambeau/bestofjs-sandbox",
    "content": {
        "body": "This is a test",
        "title": "a new test"
    }
}

Local web server

Start the local Express web server on port default port

npm start

or, using an other port:

npm start -- --port 3333

Tests

Tests use supertest module to check Express server reponses without making http requests (no need to open any port).

npm test

Deploy on webtask.io

Requirement:

npm install wt-cli -g

STEP 1: build a single file from source files, using Webpack

npm run build

STEP 2: Create / Update the webtask, using wt-cli command line tool.

For development:

npm run deploy-dev

For production:

npm run deploy-prod

Deploy commands will display the webtask URL, when the deploy is completed.

  • dev: https://webtask.it.auth0.com/api/run/wt-mikeair-gmail_com-0/user-content-api-dev
  • production: https://webtask.it.auth0.com/api/run/wt-mikeair-gmail_com-0/user-content-api-v1

Environment variables

In local, environment variables are defined in the .env file.

  • MONGO_URI: mongodb URL, database used to store reviews and links
  • MONGO_URI_TEST: mongodb URL, database used for tests only (its content is automatically deleted by setup command)
  • AUTH0_CLIENT_SECRET: Auth0 secret key, required to get user's Github token in order to make Github API requests on behalf of the user

webtask deploy script user-content-api/deploy-webtask.js automatically passes MONGO_URI and AUTH0_CLIENT_SECRET variables, using the --secret parameter.

About

bestof.js.org micro-services built with webtask.io

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published