This test shorts a given URL. It provides a command-line interface to interact with the API. The project does not include an external database to avoid dependencies and allowing to port quickly to AWS. See production deployment.
The solution includes all different projects on a single repository.
The server application uses NodeJS. As any other Node project, install dependencies first npm install
. The available script are the following:
npm run dev
Runs the server to expose the API.npm run test
Executes the available tests.npm run fix
It just executes standard js to keep the code uniform.
The following endpoints are exposed:
Short the given URL. This is a post a request. It takes the following process:
- The method breaks the given URL into parts domain and URL. The objective is to increase the number of URLs that the system can store.
- The shorten method creates a file to identify domains, URLs, also for status and stats.
- If the URL has created already, it returns the path.
curl --location --request POST 'http://localhost:3000/shorten/' \
--header 'Content-Type: application/json' \
--data-raw '{
"url": "https://github.com/bipsa/synthesized_url"
}'
{
"url": "http://localhost:3000/oOJ/q7x/"
}
This endpoint redirects the user to the hidden URL
curl --location --request GET 'http://localhost:3000/oOJ/q7x/'
The http code sent is 400
{
"message": "URL is not available."
}
This endpoint disable an url
curl --location --request GET 'http://localhost:3000/oOJ/q7x/disable/'
{
"response": false
}
This endpoint enable an url
curl --location --request GET 'http://localhost:3000/oOJ/q7x/enable/'
{
"response": true
}
This endpoint shows the stats for the given url
curl --location --request GET 'http://localhost:3000/oOJ/q7x/stats/'
The http code sent is 400
{
"total": 2,
"visits": [
{
"ip": "::1",
"date": "2020-05-17T03:23:22.319Z"
},
{
"ip": "::1",
"date": "2020-05-17T03:23:53.926Z"
}
]
}
The CLI application uses NodeJS. Also, install dependencies first npm install
. The available commands are the following:
node index shorten https://facebook.com
Shrinks the given URL.node index stats http://localhost:3000/rp7/Sxz/
Returns the stats from the given URL.node index disable http://localhost:3000/rp7/Sxz/
Disables the URL.node index enable http://localhost:3000/rp7/Sxz/
Enable the URL.
The method used in development gives the flexibility to port the project to AWS with not much effort, but additional configuration is required.
lambda.js
contains the code to run the project on AWS using Lambda, APIGateway, and S3. Of course, the stack could be evaluated on production by using a faster data method, but S3 gives Multiple regions, and different cache methods out of the box.
This is the request for shorten on AWS
curl --location --request POST 'https://g6m381qxf5.execute-api.us-east-1.amazonaws.com/Synthesized-Stage/shorten/' \
--header 'Content-Type: application/json' \
--data-raw '{
"url": "https://www.skillshare.com/classes/Understanding-Web-Development-A-Beginners-Guide-to-the-Web/1755504373?via=logged-in-home-row-recommended"
}'
This is the view request - Note: Some configuration is missing on the API Gateway to allow redirection :(
curl --location --request GET 'https://g6m381qxf5.execute-api.us-east-1.amazonaws.com/Synthesized-Stage/hlF/87C/'
The request to get the actions, by changing 'stats' for 'enable' or 'disable' you get those actions
curl --location --request GET 'https://g6m381qxf5.execute-api.us-east-1.amazonaws.com/Synthesized-Stage/hlF/87C/stats'
- You can give any name to the lambda function
- Add two env variables NODE_ENV, and domain (the domain is used to generate the URL, the example uses the default DNS provided by API gateway)
- Add permission for S3 to the lambda role (read and write)
- Create the api with any name you want, of course following the best practices
- Create shorten resource
- Create a post method and attached the lambda
- Create {domain} {url} {action} resources
- Add get method to url and action
- Enable sending the user headers in order to get IP address and actions
- Create the bucket and name it
synthesized-url
- You don't need public access
- Increase test coverage
- Standardize the AWS calls with the file system to reduce code.
- Script to upload everything to AWS
- Redirection issue on API Gateway