-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from vocascan/experimental
Push v1.0.0 to production
- Loading branch information
Showing
65 changed files
with
15,965 additions
and
485 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"printWidth": 120, | ||
"endOfLine": "auto" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.