fastify initial commit

This commit is contained in:
2023-01-02 18:12:02 -03:00
commit a897ec5f95
10 changed files with 131 additions and 0 deletions

15
.eslintrc.json Normal file
View File

@@ -0,0 +1,15 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true
},
"extends": "standard",
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
}
}

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
node_modules
yarn.lock

15
Dockerfile Normal file
View File

@@ -0,0 +1,15 @@
FROM ubuntu:latest
RUN apt upgrade && apt update
RUN apt install -y curl
RUN curl -fsSL https://deb.nodesource.com/setup_19.x | bash - &&\
apt-get install -y nodejs
RUN npm install --global yarn
COPY [".", "/usr/src"]
WORKDIR "/usr/src"
RUN yarn
CMD ["yarn", "dev"]

10
docker-compose.yml Normal file
View File

@@ -0,0 +1,10 @@
version: "3.8"
services:
app:
build: .
volumes:
- .:/usr/src
- /usr/src/node_modules
ports:
- "3000:3000"

10
forbidden-ids.json Normal file
View File

@@ -0,0 +1,10 @@
{"forbidden-ids": [
"settings",
"register",
"login",
"admin",
"franp",
"github",
"linkedin",
"add"
]}

33
package.json Normal file
View File

@@ -0,0 +1,33 @@
{
"name": "url-shorter",
"description": "",
"version": "1.0.0",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/index.js",
"dev": "nodemon src/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/monatheoctocat/my_package.git"
},
"keywords": [],
"author": "FranP-Code",
"license": "ISC",
"bugs": {
"url": "https://github.com/monatheoctocat/my_package/issues"
},
"homepage": "https://github.com/monatheoctocat/my_package",
"dependencies": {
"@fastify/jwt": "^6.5.0",
"fastify": "^4.10.2"
},
"devDependencies": {
"eslint": "^8.0.1",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-promise": "^6.0.0",
"nodemon": "^2.0.20"
}
}

19
src/index.js Normal file
View File

@@ -0,0 +1,19 @@
const fastify = require('fastify')({ logger: true })
const addUrl = require('./routes/add-url')
const index = require('./routes/index')
const urlShortener = require('./routes/url-shortener')
fastify.register(index)
fastify.register(urlShortener)
fastify.register(addUrl)
const start = async () => {
try {
await fastify.listen({ port: process.env.PORT || 3000, host: '0.0.0.0' })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()

16
src/routes/add-url.js Normal file
View File

@@ -0,0 +1,16 @@
module.exports = async function (fastify, options) {
fastify.register(require('@fastify/jwt'), {
secret: 'supersecret123'
})
fastify.post('/add', (req, reply) => {
// some code
const token = fastify.jwt.sign({ abc: 123 })
console.log(token)
reply.send({ token })
})
fastify.listen({ port: 3000 }, (err) => {
if (err) throw err
})
}

5
src/routes/index.js Normal file
View File

@@ -0,0 +1,5 @@
module.exports = async function (fastify, options) {
fastify.get('/', async (request, reply) => {
return { test: 'testValue' }
})
}

View File

@@ -0,0 +1,6 @@
module.exports = async function (fastify, options) {
fastify.get('/:id', async (request, reply) => {
const { id } = request.params
return { test: id }
})
}