Skip to content

Commit

Permalink
Merge branch 'release/2.0.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
adufr committed Aug 26, 2020
2 parents df20ea6 + 2423e53 commit 10e92c6
Show file tree
Hide file tree
Showing 68 changed files with 7,200 additions and 5,808 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_size = 2
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
14 changes: 14 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
NODE_ENV=development

HOST=0.0.0.0
PORT=3333

APP_NAME=calendz-api-calendar
APP_URL=http://${HOST}:${PORT}
# Please generate a new key:
# > $ adonis key:generate
APP_KEY=im_a_secret

REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
14 changes: 14 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
NODE_ENV=testing

HOST=127.0.0.1
PORT=4000

# APP_NAME=calendz-api-calendar
# APP_URL=http://${HOST}:${PORT}
# # Please generate a new key:
# # > $ adonis key:generate
# APP_KEY=

REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: [
'standard'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
use: true,
make: true
},
parserOptions: {
ecmaVersion: 2018
},
rules: {
}
}
21 changes: 0 additions & 21 deletions .eslintrc.json

This file was deleted.

18 changes: 12 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# editor configuration
.vscode/
.idea/

# app dependencies
node_modules/
# Node modules
node_modules

# config
# Adonis directory for storing tmp files
tmp

# Environment variables, never commit this file
.env
.env.dev
.env.prod

# coverage
coverage/
# Code coverage
.nyc_output/
coverage/

# ssh key for deployment
deploy_key
deploy_key.pub
deploy_key.pub
15 changes: 11 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ notifications:
email: false

node_js:
- "10.15.3"
- "12.18.2"

services:
- redis-server

before_script:
- cp -a .env.example .env

before_install:
- openssl aes-256-cbc -K $encrypted_d810adeb7664_key -iv $encrypted_d810adeb7664_iv
Expand All @@ -14,6 +20,7 @@ install: npm install

jobs:
include:
# Run all tasks
- stage: Tests
if: |
branch = develop AND \
Expand All @@ -32,7 +39,7 @@ jobs:
- chmod +x send.sh
- ./send.sh failure $DISCORD_WEBHOOK_URL

# Build and push both current version & latest version
# Build and push both versionned and current versions
- stage: Build Docker image
if: branch = master
script:
Expand All @@ -41,9 +48,9 @@ jobs:
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
- docker build -t calendz/api-calendar .
- docker images
- docker tag calendz/api-calendar calendz/api-calendar:latest
- docker tag calendz/api-calendar calendz/api-calendar:current
- docker tag calendz/api-calendar calendz/api-calendar:${PACKAGE_VERSION}
- docker push calendz/api-calendar:latest
- docker push calendz/api-calendar:current
- docker push calendz/api-calendar:${PACKAGE_VERSION}

# Deploy latest API-CALENDAR version
Expand Down
2 changes: 1 addition & 1 deletion .travis/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ssh-add .travis/deploy_key # Add the private key to SSH
ssh -o "StrictHostKeyChecking=no" $SSH_USER@$SSH_IP -p $SSH_PORT <<EOF
cd /home/calendz/calendz-conf
git pull
docker pull calendz/api-calendar:latest
docker pull calendz/api-calendar:current
docker-compose -f docker/docker-compose.prod.yml stop calendz-api-calendar
docker-compose -f docker/docker-compose.prod.yml rm -f calendz-api-calendar
docker-compose -f docker/docker-compose.prod.yml up -d --no-deps --build calendz-api-calendar
Expand Down
11 changes: 4 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# Node latest LTS 10.15.3 with alpine
# (a lightweight distribution)
FROM node:10.15.3-alpine
# Node LTS 12.18.2 on alpine
FROM node:12.18.2-alpine
LABEL maintainer="Calendz. <https://calendz.app/>"

# add some required packages
RUN apk update && apk upgrade && \
apk add --no-cache bash git openssh python make g++

# creates a directory for the app
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
Expand All @@ -15,5 +10,7 @@ WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install

RUN npm i -g @adonisjs/cli

# bundle all source code
COPY . .
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,44 @@ L'API de calendz est développée avec les frameworks et outils suivants* :

| Librairie | Version | Description |
| ---------------- | ---------- | ------------------------------------------------------------------------------------------------ |
| [Node.js] | 10.15.3 | Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. |
| [Express] | 4.17.1 | Fast, unopinionated, minimalist web framework for Node.js |
| [Cheerio] | 1.0.0-rc.2 | Fast, flexible, and lean implementation of core jQuery designed specifically for the server. |
| [JsonWebToken] | 8.5.1 | Industry standard RFC 7519 method for representing claims securely between two parties. |
| [Node.js] | 12.16.0 | Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. |
| [Adonis.js] | 4.1 | The Node.js Framework highly focused on developer ergonomics, stability and confidence. |
| [Cheerio] | 1.0.0-rc.3 | Fast, flexible, and lean implementation of core jQuery designed specifically for the server. |

**(Liste non exhaustive, uniquement les librairies principales sont présentées)*

## Installation & utilisation

### Pré-requis

* Installer Node 10.15.3
* Installer Node 12.16.0
* Créer un fichier `.env` à la source de ce repository contenant les valeurs suivantes (modifiables selon vos besoins)

NODE_ENV=development

APP_PORT=3000
HOST=0.0.0.0
PORT=3333

SCRAPPING=true
APP_NAME=calendz-api-calendar
APP_URL=http://${HOST}:${PORT}
# Please generate a new key:
# > $ adonis key:generate
APP_KEY=

REDIS_HOST=calendz-redis
REDIS_PORT=6379
REDIS_PASSWORD=password

DB_HOST=calendz-database
DB_PORT=27017
DB_NAME=calendz
DB_USER=username
DB_PASSWORD=password

### Lancement

* Lint : `npm run lint` (corrige la syntaxe du code grâce à [ESLint](https://github.com/eslint/eslint))
* Lint : `npm run lint` (analyse la syntaxe du code grâce à [ESLint](https://github.com/eslint/eslint))
* Lint : `npm run lint:fix` (corrige la syntaxe du code grâce à [ESLint](https://github.com/eslint/eslint))
* Tests : `npm run test` (lance les tests effectués lors de l'intégration continue)
* Tests : `npm run test:coverage` (calcule la couverture des tests (et génère un rapport html))
* Production : `npm run start` (lance via node, aucun process manager n'est inclus par défaut)
* Développement : `npm run dev` (lance avec [nodemon](https://nodemon.io/))
* Développement : `npm run dev` (lance en environnement de développement avec hot-reloading)

[Node.js]: https://github.com/nodejs/node
[Express]: https://github.com/expressjs/express
[Cheerio]: https://github.com/Automattic/mongoose/
[JsonWebToken]: https://github.com/auth0/node-jsonwebtoken
[Adonis.js]: https://github.com/adonisjs
[Cheerio]: https://github.com/cheeriojs/cheerio
21 changes: 21 additions & 0 deletions ace
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

/*
|--------------------------------------------------------------------------
| Ace Commands
|--------------------------------------------------------------------------
|
| The ace file is just a regular Javascript file but with no extension. You
| can call `node ace` followed by the command name and it just works.
|
| Also you can use `adonis` followed by the command name, since the adonis
| global proxies all the ace commands.
|
*/

const { Ignitor } = require('@adonisjs/ignitor')

new Ignitor(require('@adonisjs/fold'))
.appRoot(__dirname)
.fireAce()
.catch(console.error)
69 changes: 69 additions & 0 deletions app/Controllers/Http/DayController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'

const moment = require('moment')
const Cache = use('Cache')
const Scrapper = use('Scrapper')
const DateUtils = use('DateUtils')
const ScrappingException = use('App/Exceptions/ScrappingException')
const InvalidDateException = use('App/Exceptions/InvalidDateException')

class DayController {
/**
* Get courses of current day
*/
async getCurrent ({ request }) {
const { firstname, lastname, ignoreCache } = request.only(['firstname', 'lastname', 'ignoreCache'])

// get current date and format with moment
const date = moment(new Date()).format('MM/DD/YY')

// try to retrieve and return data from cache/redis
if (!ignoreCache) {
const data = await Cache.getDay(firstname, lastname, date)
if (data) return data
}

// if not cached, scrap it
const result = await Scrapper.fetchDay(firstname, lastname, date)
.catch(() => {
/* istanbul ignore next */
throw new ScrappingException()
})

// set scrap result in cache
await Cache.setDay(firstname, lastname, date, result)

return result
}

/**
* Get courses of a given day
*/
async getByDate ({ request, params }) {
const { firstname, lastname, ignoreCache } = request.only(['firstname', 'lastname', 'ignoreCache'])

// check if date is valid, if yes format it
if (!DateUtils.isValid(params.date)) throw new InvalidDateException()
const date = moment(params.date, 'MM-DD-YYYY').format('MM/DD/YY')

// try to retrieve and return data from cache/redis
if (!ignoreCache) {
const data = await Cache.getDay(firstname, lastname, date)
if (data) return data
}

// if not cached, scrap it
const result = await Scrapper.fetchDay(firstname, lastname, date)
.catch(() => {
/* istanbul ignore next */
throw new ScrappingException()
})

// set scrap result in cache
await Cache.setDay(firstname, lastname, date, result)

return result
}
}

module.exports = DayController
Loading

0 comments on commit 10e92c6

Please sign in to comment.