Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTful API #15

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
trim_trailing_whitespace = true

[*.js]
indent_size = 2
7 changes: 7 additions & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
APPLICATION_PORT=5000

DATABASE_PORT=5432
DATABASE_USER=harpoon
DATABASE_HOST=localhost
DATABASE_NAME=harpoon
DATABASE_PASSWORD=harpoon
144 changes: 144 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# 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

# 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.*

### Node Patch ###
# Serverless Webpack directories
.webpack/

# Optional stylelint cache

# SvelteKit build / generate output
.svelte-kit

# End of https://www.toptal.com/developers/gitignore/api/node
13 changes: 13 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:lts

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "run", "dev"]
13 changes: 13 additions & 0 deletions api/database/connector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { Pool } = require('pg');
const dotenv = require('dotenv')
dotenv.config()

const pool = new Pool({
user: process.env.DATABASE_USER,
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_NAME,
password: process.env.DATABASE_PASSWORD,
port: process.env.DATABASE_PORT,
});

module.exports = pool;
36 changes: 36 additions & 0 deletions api/database/database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CREATE TABLE IF NOT EXISTS public.company (
ID SERIAL PRIMARY KEY,
NAME VARCHAR(62) NOT NULL,
EMAIL VARCHAR(50) NOT NULL,
STATUS INT NOT NULL
);

CREATE TABLE IF NOT EXISTS public.rule (
ID SERIAL PRIMARY KEY,
ID_COMPANY INT NOT NULL,
STRING VARCHAR(60) NOT NULL,
FILTER TEXT NOT NULL,
SCORE VARCHAR(10) NOT NULL,
DESCRIPTION VARCHAR(64) NOT NULL,
STATUS INT NOT NULL,
CONSTRAINT FK_company_rule FOREIGN KEY (ID_COMPANY) REFERENCES public.company (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);

CREATE TABLE IF NOT EXISTS public.alert (
ID SERIAL PRIMARY KEY,
ID_COMPANY INT NOT NULL,
DATETIME TIMESTAMP NOT NULL DEFAULT current_timestamp,
STATUS INT NOT NULL,
NOTIFICATION INT NOT NULL,
HASH CHAR(32) NOT NULL,
CONSTRAINT FK_company_alert FOREIGN KEY (ID_COMPANY) REFERENCES public.company (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);

CREATE TABLE IF NOT EXISTS public.history (
ID SERIAL PRIMARY KEY,
ID_COMPANY INT NOT NULL,
SOURCE VARCHAR(255),
DATETIME TIMESTAMP NOT NULL DEFAULT current_timestamp,
STATUS INT NOT NULL,
CONSTRAINT FK_company_history FOREIGN KEY (ID_COMPANY) REFERENCES public.company (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
34 changes: 34 additions & 0 deletions api/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
services:
db:
container_name: harpoon_database
image: postgres:latest
env_file:
- .env
environment:
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
- POSTGRES_DB=${DATABASE_NAME}
volumes:
- ./database:/docker-entrypoint-initdb.d
ports:
- "${DATABASE_PORT}:${DATABASE_PORT}"
healthcheck:
test: ["CMD", "pg_isready", "-U", "${DATABASE_USER}"]
interval: 5s
timeout: 5s
retries: 3

app:
container_name: harpoon_app
env_file:
- .env
build:
context: .
dockerfile: Dockerfile
ports:
- "${APPLICATION_PORT}:${APPLICATION_PORT}"
environment:
- NODE_ENV=development
depends_on:
- db
restart: unless-stopped
20 changes: 20 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const express = require('express')
const app = express()
const dotenv = require('dotenv')
dotenv.config()

const port = process.env.APPLICATION_PORT

app.use(express.json())

const alertsRoutes = require('./routes/alerts')
const companyRoutes = require('./routes/company')
const historyRoutes = require('./routes/history')
const ruleRoutes = require('./routes/rule')

app.use('/alerts', alertsRoutes)
app.use('/company', companyRoutes)
app.use('/history', historyRoutes)
app.use('/rule', ruleRoutes)

app.listen(port, () => console.log(`App listening on port ${port}`))
Loading