Skip to content

Commit

Permalink
Merge pull request #53 from vocascan/experimental
Browse files Browse the repository at this point in the history
Push v1.0.0 to production
  • Loading branch information
Julian authored Jun 12, 2021
2 parents ac28ca6 + 2c24a51 commit 9a68ee1
Show file tree
Hide file tree
Showing 65 changed files with 15,965 additions and 485 deletions.
26 changes: 17 additions & 9 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
POSTGRES_IP_ADDRESS": 127.0.0.1,
POSTGRES_DB_NAME": vocascan,
POSTGRES_USERNAME": your_username,
POSTGRES_PASSWORD": your_username_password,
POSTGRES_PORT": 5432,
SERVER_IP_ADDRESS": 127.0.0.1,
SERVER_PORT": 13200,
SERVER_DEBUG": true,
SECRET_KEY": your_secret_key
# SERVER
PORT=

# DATABASE
DB_CONNECTION_URL=
DB_DIALECT=
DB_STORAGE=
DB_HOST=
DB_PORT=
DB_USERNAME=
DB_PASSWORD=
DB_DATABASE=

JWT_SECRET=
SALT_ROUNDS=

DEBUG=
24 changes: 24 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"extends": ["airbnb-base/legacy", "prettier", "plugin:node/recommended"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"no-unused-vars": "warn",
"no-console": "off",
"strict": "off",
"no-plusplus": "off",
"no-process-exit": "off",
"no-restricted-syntax": "off",
"max-len": [
"error",
{
"code": 120,
"ignoreComments": true,
"ignoreTrailingComments": true,
"ignoreUrls": true,
"ignoreStrings": true,
"ignoreTemplateLiterals": true
}
]
}
}
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ jobs:
push: true
tags: ${{ steps.prepare.outputs.image }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: test

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: setup node
uses: actions/setup-node@v2
with:
node-version: '14'

- name: install
run: npm i

- name: lint
run: npm run lint -- --max-warnings=0
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/node_modules
package-lock.json
/docker/database
/docker/minio
.vscode
.env
*.sql
*.sqlite


6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"tabWidth": 2,
"printWidth": 120,
"endOfLine": "auto"
}
27 changes: 12 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
FROM node:14-alpine
FROM node:14-alpine as builder

RUN apk add --no-cache --update curl bash
WORKDIR /app
RUN apk add --no-cache build-base python

ARG NODE_ENV=development
ARG PORT=3000
ENV PORT=$PORT
WORKDIR /build

COPY package* ./
# Install the npm packages
RUN npm install && npm update
COPY ./package*.json ./

COPY . .
RUN npm i --only=production --sqlite

FROM node:14-alpine

WORKDIR /app

# Run the image as a non-root user
RUN adduser -D myuser
USER myuser
COPY --from=builder /build/node_modules /app/node_modules

EXPOSE $PORT
COPY . .

CMD ["npm", "run", "start"]
CMD ["npm", "run", "start"]
80 changes: 52 additions & 28 deletions app/Controllers/AuthController.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,63 @@
const { createUser, loginUser, validateRegister, validateLogin, generateToken } = require("../Services/AuthServiceProvider.js")
const {
createUser,
loginUser,
validateRegister,
validateLogin,
destroyUser,
changePassword,
} = require('../Services/AuthServiceProvider');
const { generateJWT, deleteKeysFromObject } = require('../utils');
const catchAsync = require('../utils/catchAsync');

async function register(req, res) {
console.log("called");
if(!(await validateRegister(req, res))) {
return
}
const register = catchAsync(async (req, res) => {
await validateRegister(req, res);

const user = await createUser(req.body)
const user = await createUser(req.body);
const token = generateJWT({ id: user.id });

const token = generateToken(user)
res.send({ token, user });
});

res.send({ token, user })
res.end()
}
const login = catchAsync(async (req, res) => {
validateLogin(req, res);

async function login(req, res) {
if(!validateLogin(req, res)) {
return
}
const user = await loginUser(req.body, res);

const user = await loginUser(req.body, res)
if (user) {
// generate JWT with userId
const token = generateJWT({ id: user.id });

if(user) {
const token = generateToken(user)
res.send({ token, user });
}
});

res.send({ token, user })
}
}
const profile = catchAsync(async (req, res) => {
res.send(deleteKeysFromObject(['roleId', 'password', 'createdAt', 'updatedAt'], req.user.toJSON()));
});

async function profile(req, res) {
res.send(req.user)
}
const deleteUser = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;

await destroyUser(userId);

res.status(204).end();
});

const resetPassword = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;
const { oldPassword, newPassword } = req.body;

await changePassword(userId, oldPassword, newPassword);

res.status(204).end();
});

module.exports = {
register,
login,
profile
}
register,
login,
profile,
deleteUser,
resetPassword,
};
38 changes: 38 additions & 0 deletions app/Controllers/DocsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const swaggerUi = require('swagger-ui-express');

const swaggerDocument = require('../../docs/api/swagger.json');

// disable swagger topbar
const swaggerOptions = {
customCss: `
.swagger-ui .topbar { display: none; }',
`,
};

const injectHostMiddleware = (req, _res, next) => {
req.swaggerDoc = swaggerDocument;

// get url
const currentURL = `${req.protocol}://${req.get('host')}/api`;

// inject host
if (req.swaggerDoc.servers[0].description !== 'current') {
req.swaggerDoc.servers = [{ description: 'current', url: currentURL }, ...req.swaggerDoc.servers];
}

next();
};

const swagger = [injectHostMiddleware, swaggerUi.serve, swaggerUi.setup(swaggerDocument, swaggerOptions)];

const document = [
injectHostMiddleware,
(req, res) => {
res.json(req.swaggerDoc);
},
];

module.exports = {
swagger,
document,
};
75 changes: 75 additions & 0 deletions app/Controllers/GroupController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { createGroup, getGroups, destroyGroup, updateGroup } = require('../Services/GroupServiceProvider.js');
const { getStats } = require('../Services/StatsServiceProvider.js');
const catchAsync = require('../utils/catchAsync');

const addGroup = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;

// get language package id from params
const { languagePackageId } = req.params;

// create language Package
const group = await createGroup(req.body, userId, languagePackageId);

res.send(group);
});

const sendGroups = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;

// get language package id from params
const { languagePackageId } = req.params;

// decide if we have to fetch stats
const includeStats = (req.query.stats || false) === 'true';

// get groups
const groups = await getGroups(userId, languagePackageId);

const formatted = await Promise.all(
groups.map(async (group) => ({
...group.toJSON(),

...(includeStats
? {
stats: await getStats({
groupId: group.id,
languagePackageId,
userId,
}),
}
: {}),
}))
);

res.send(formatted);
});

const deleteGroup = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;
const { groupId } = req.params;

await destroyGroup(userId, groupId);

res.status(204).end();
});

const modifyGroup = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;
const { groupId } = req.params;

await updateGroup(req.body, userId, groupId);

res.status(204).end();
});

module.exports = {
addGroup,
sendGroups,
deleteGroup,
modifyGroup,
};
14 changes: 14 additions & 0 deletions app/Controllers/InfoController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { getVersion, getGitDescribe } = require('../Services/InfoServiceProvider.js');
const catchAsync = require('../utils/catchAsync.js');

const sendInfo = catchAsync(async (_req, res) => {
res.send({
identifier: 'vocascan-server',
version: getVersion(),
commitRef: await getGitDescribe(),
});
});

module.exports = {
sendInfo,
};
22 changes: 22 additions & 0 deletions app/Controllers/LanguageController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { getAllLanguages } = require('../Services/LanguageServiceProvider');
const catchAsync = require('../utils/catchAsync');

const sendLanguages = catchAsync(async (req, res) => {
const code = (req.query.code || 'true') === 'true';
const name = (req.query.name || 'true') === 'true';
const nativeNames = (req.query.nativeNames || 'false') === 'true';
const rtl = (req.query.rtl || 'false') === 'true';

const allLanguages = await getAllLanguages([
...(code ? ['code'] : []),
...(name ? ['name'] : []),
...(nativeNames ? ['nativeNames'] : []),
...(rtl ? ['rtl'] : []),
]);

res.send(allLanguages);
});

module.exports = {
sendLanguages,
};
Loading

0 comments on commit 9a68ee1

Please sign in to comment.