Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
logFolderStructure.py
scan_log.txt
17 changes: 17 additions & 0 deletions project/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:16

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY backend/src/package.json /usr/src/app
RUN yarn

# Install nodemon
RUN yarn global add nodemon

# Bundle app source
COPY backend/src/ .

EXPOSE 3000
CMD ["yarn", "start"]
File renamed without changes.
15 changes: 15 additions & 0 deletions project/backend/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) [year] [fullname]

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
37 changes: 37 additions & 0 deletions project/backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Shortened Links Backend
This is the backend for the Shortened Links application. It is built using Node.js, Express, MongoDB, and Mongoose.

## Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

### Prerequisites
- Docker
- Docker Compose

### Installing
1. Clone the repository
``git clone https://github.com/marksonseedify/development-challenge``

2. Build the application
``docker-compose build``

3. Start the application
``docker-compose up``

The application will be running on http://localhost:3000

### Endpoints
- POST /shorten: Shorten a url
- GET /:id: Get the original url from a shortened id
- GET /getAllElements: Get all the shortened urls

### Built With
- Node.js
- Express
- MongoDB

### Author
Pedro Magalhães - https://github.com/pedrosgmagalhaes

## License
This project is licensed under the ISC License - see the LICENSE.md file for details.
130 changes: 130 additions & 0 deletions project/backend/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
80 changes: 80 additions & 0 deletions project/backend/src/controllers/url.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const Url = require('../models/urls');
const hash = require('../utils/hash');
const { validateUrl } = require('../utils/validateUrl');

exports.createShortUrl = async (req, res) => {
// Validate the original URL
if (!req.body.originalUrl) {
return res.status(400).json({ error: 'originalUrl is required' });
}
if (!/^(http|https):\/\/[^ "]+$/.test(req.body.originalUrl)) {
return res.status(400).json({ error: 'originalUrl is not a valid URL' });
}

const isOriginalValidUrl = await validateUrl(req.body.originalUrl);
console.log("isOriginalValidUrl", isOriginalValidUrl)

if (!isOriginalValidUrl) {
return res.status(400).json({ error: 'originalUrl is not a valid and working URL' });
}

try {

// Generate a unique short id
let shortId;
shortId = hash.encode(Date.now().toString());

// Create a new document in the database
const newUrl = new Url({
originalUrl: req.body.originalUrl,
shortId: shortId,
accessCount: 0
});

await newUrl.save();

// Return the original and shortened URLs
return res.json({
originalUrl: newUrl.originalUrl,
shortenedUrl: `${req.protocol}://${req.get('host')}/${shortId}`
});
} catch (error) {
console.log(error);
return res.status(500).json({ error: 'Error while shortening the URL' });
}
};

exports.getAllElements = async (req, res) => {
try {
// Find the URL with the given short id
const url = await Url.find().lean();
if (!url) {
return res.status(404).json({ error: 'URL not found' });
}
return res.json(url);

} catch (error) {
console.log(error);
return res.status(500).json({ error: 'Error while retrieving the URLs' });
}
};

exports.getOriginalUrl = async (req, res) => {
try {
// Find the URL with the given short id
const url = await Url.findOne({ shortId: req.params.id });
if (!url) {
return res.status(404).json({ error: 'URL not found' });
}

// Increment the access count
url.accessCount++;
await url.save();

// Redirect the user to the original URL
return res.redirect(url.originalUrl);
} catch (error) {
console.log(error);
return res.status(500).json({ error: 'Error while redirecting the URL' });
}
};
11 changes: 11 additions & 0 deletions project/backend/src/models/urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const urlSchema = new Schema({
originalUrl: { type: String, required: true },
shortId: { type: String, required: true, unique: true },
accessCount: { type: Number, default: 0 }
});

const Url = mongoose.model('Url', urlSchema);
module.exports = Url;
28 changes: 28 additions & 0 deletions project/backend/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "shortenedlinks-backend",
"version": "1.0.0",
"description": "The challenge consists on a landing page capable of generating a functional, shortened link based on an input of a URL. This project will evaluate the developer's skills and code quality when transforming a layout in a functional prototype, along with the backend and frontend properly separated.",
"main": "server.js",
"scripts": {
"start": "nodemon server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/marksonseedify/development-challenge.git"
},
"author": "Pedro Magalhaes",
"license": "ISC",
"bugs": {
"url": "https://github.com/marksonseedify/development-challenge/issues"
},
"homepage": "https://github.com/marksonseedify/development-challenge#readme",
"dependencies": {
"axios": "^1.2.3",
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"express": "^4.18.2",
"mongoose": "^6.8.4",
"nodemon": "^2.0.20"
}
}
9 changes: 9 additions & 0 deletions project/backend/src/routes/links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const router = express.Router();
const urlController = require('../controllers/url.controller');

router.get('/getAllElements', urlController.getAllElements);
router.post('/shorten', urlController.createShortUrl);
router.get('/:id', urlController.getOriginalUrl);

module.exports = router;
19 changes: 19 additions & 0 deletions project/backend/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const routesLinks = require('./routes/links');
const cors = require('cors');
const mongoose = require('mongoose');

mongoose.connect('mongodb://mongodb:27017/url-shortener', { useNewUrlParser: true });

app.use(cors({
origin: '*'
}));

app.use(bodyParser.json());
app.use('/', routesLinks);

app.listen(3000, () => {
console.log('Server is running on port 3000');
});
9 changes: 9 additions & 0 deletions project/backend/src/utils/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const crypto = require('crypto');

function encode(data) {
return crypto.createHash('sha1').update(data).digest('hex').slice(0, 6);
}

module.exports = {
encode
};
16 changes: 16 additions & 0 deletions project/backend/src/utils/validateUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const axios = require('axios');

const validateUrl = async (url) => {
try {
await axios.get(url);
return true;
} catch (error) {
console.log(error)
return false;
}
}


module.exports = {
validateUrl
};
Loading