EdmilDM / serverless-api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

serverless-api

This API was made with Serverless framework and Node.js. Since the scope was so small, I felt that setting up a test suite would be overkill and just decided to test manually.

It has 3 endpoints, 2 which are GET and 1 POST.

The first endpoint takes a JSON file body, and a Filename header must be provided with the filename.

Example:

File upload: request1 Header: request2 Valid json files:

{
  "latitude": 51.507351,
  "longitude": -0.127758
}
{
  "latitude": 59.507331,
  "longitude": 10.222355,
  "name": "ontario"
}

Invalid json files:

{
  "latitude": 51.507351,
  "name": "london"
}
{
  "latitude": 51.507351,
  "longitude: -900.000000
}
{
  "latitude": "new york city",
  "longitude: -900.000000
}

There are a couple of validations that the file must pass in order to store it in the S3 bucket:

  • File and filename must not be empty.
  • Filename must be .json extension.
  • It must be a valid json file.
  • It must have the mandatory fields latitude and longitude.
  • Latitude must be -90 < latitude < 90 and longitude must be -180 < longitude < 80.

The second endpoint is a simple get request without parameters that returns a list of all file names without the .json extension.

The third endpoint is another get request, but this time it only looks for a single object with the specifed key in the parameters, the key name is filename.

Example:

request3

All the code is in the API folder, with 3 subfolders, one is for the external route, which is the upload one. External which is for both location requests, and utils for various utility methods I abstracted to help me.

To run yourself (with NPM):

  1. Download project and navigate to it.

  2. Install serverless cli globally.

npm install -g serverless
  1. Configure serverless with AWS credentials, get your key and secret from your AWS account.
serverless config credentials --provider aws --key AKIAIOSFODNN7EXAMPLE --secret wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
  1. Install dependencies.
npm i
  1. Deploy with serverless.
serverless deploy
  1. Alternatively, you can run locally with serverless-offline.
serverless offline start

About


Languages

Language:JavaScript 100.0%