Skip to content

Commit

Permalink
Merge pull request jakowenko#67 from jakowenko/beta
Browse files Browse the repository at this point in the history
v0.10.0
  • Loading branch information
jakowenko authored Aug 3, 2021
2 parents 30d45c0 + f1fe8c3 commit 0e1df10
Show file tree
Hide file tree
Showing 69 changed files with 1,477 additions and 639 deletions.
2 changes: 2 additions & 0 deletions .develop/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ RUN ln -s /.storage /double-take/.storage

WORKDIR /double-take

ENV NODE_ENV=development

RUN npm install nodemon -g
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ COPY /api/src ./src
WORKDIR /double-take/frontend
COPY /frontend/src ./src
COPY /frontend/public ./public
COPY /frontend/.env.production ./frontend/vue.config.js ./
COPY /frontend/.env.production /frontend/vue.config.js /frontend/babel.config.js ./

RUN npm run build
RUN mv dist /tmp/dist && rm -r * && mv /tmp/dist/* .
Expand All @@ -33,4 +33,6 @@ RUN npm install nodemon -g

COPY /entrypoint.sh .

ENV NODE_ENV=production

ENTRYPOINT ["/bin/bash", "./entrypoint.sh"]
160 changes: 61 additions & 99 deletions README.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions api/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = {
},
extends: ['airbnb-base', 'plugin:prettier/recommended'],
plugins: ['prettier'],
parserOptions: {
ecmaVersion: 2020,
},
rules: {
'linebreak-style': 0,
'no-console': 0,
Expand All @@ -24,5 +27,6 @@ module.exports = {
'no-param-reassign': 0,
'no-restricted-syntax': 0,
'no-nested-ternary': 0,
'global-require': 0,
},
};
92 changes: 88 additions & 4 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "double-take-api",
"version": "0.9.1",
"version": "0.10.0",
"description": "Unified UI and API for processing and training images for facial recognition",
"scripts": {
"start": "node server.js",
Expand All @@ -23,10 +23,11 @@
"cors": "^2.8.5",
"execution-time": "^1.4.1",
"express": "^4.16.1",
"express-validator": "^6.12.0",
"express-validator": "^6.12.1",
"form-data": "^4.0.0",
"image-size": "^1.0.0",
"js-yaml": "^4.1.0",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"luxon": "^1.28.0",
"mqtt": "^4.2.8",
Expand Down
12 changes: 10 additions & 2 deletions api/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ const app = express();
app.use('*', cors());
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: false }));
app.use(
express.static(process.env.NODE_ENV === 'development' ? './frontend/dist/' : './frontend/')
);
app.use('/api', router);
app.use('/api/tmp', express.static(`/tmp`));
app.use('/', express.static(`./frontend`));
app.use('/', (req, res) => {
res.sendFile(
process.env.NODE_ENV === 'development'
? `${process.cwd()}/frontend/dist/index.html`
: `${process.cwd()}/frontend/index.html`
);
});

module.exports = { app };
25 changes: 18 additions & 7 deletions api/src/constants/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const yaml = require('js-yaml');
const fs = require('fs');
const _ = require('lodash');
const DEFAULTS = require('./defaults');
const { detectors: DETECTORS, notify: NOTIFY, ...DEFAULTS } = require('./defaults');
const { core: SYSTEM_CORE } = require('./system');
const { version } = require('../../package.json');

Expand Down Expand Up @@ -44,18 +44,29 @@ module.exports = () => {
if (configData && configData.code === 'ENOENT') setup('config.yml', '# Double Take');
else CONFIG = { ...configData };

if (!CONFIG.notify || !CONFIG.notify.gotify) delete DEFAULTS.notify.gotify;

CONFIG = _.isEmpty(CONFIG) ? DEFAULTS : _.mergeWith(DEFAULTS, CONFIG, customizer);
if (CONFIG?.notify?.gotify)
CONFIG.notify.gotify = _.mergeWith(NOTIFY.gotify, CONFIG.notify.gotify, customizer);
if (CONFIG?.detectors?.compreface)
CONFIG.detectors.compreface = _.mergeWith(
DETECTORS.compreface,
CONFIG.detectors.compreface,
customizer
);
CONFIG = _.mergeWith(CONFIG, SYSTEM_CORE);
CONFIG.version = version;
CONFIG = _(CONFIG).toPairs().sortBy(0).fromPairs().value();
return CONFIG;
};

module.exports.detectors = () => {
const detectors = [];
const results = [];
if (CONFIG.detectors)
for (const [detector] of Object.entries(CONFIG.detectors)) detectors.push(detector);
return detectors;
for (const [detector] of Object.entries(CONFIG.detectors)) results.push(detector);
return results;
};

module.exports.notify = () => {
const results = [];
if (CONFIG.notify) for (const [notify] of Object.entries(CONFIG.notify)) results.push(notify);
return results;
};
7 changes: 6 additions & 1 deletion api/src/constants/defaults.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
server: { port: 3000 },
auth: false,
confidence: {
match: 60,
unknown: 40,
Expand Down Expand Up @@ -28,6 +28,11 @@ module.exports = {
homeassistant: 'homeassistant',
},
},
detectors: {
compreface: {
det_prob_threshold: 0.8,
},
},
notify: {
gotify: {
priority: 5,
Expand Down
2 changes: 2 additions & 0 deletions api/src/constants/http-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ module.exports = {
OK: 200,
SERVER_ERROR: 500,
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
NOT_FOUND: 404,
};
1 change: 1 addition & 0 deletions api/src/constants/system.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports.core = {
server: { port: 3000 },
storage: { path: './.storage', config: { path: './.storage/config' } },
};

Expand Down
67 changes: 67 additions & 0 deletions api/src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { v4: uuidv4 } = require('uuid');
const { auth, jwt } = require('../util/auth.util');
const { respond, HTTPSuccess, HTTPError } = require('../util/respond.util');
const { AUTH } = require('../constants');
const { OK, BAD_REQUEST, UNAUTHORIZED } = require('../constants/http-status');

module.exports.status = (req, res) => {
const response = { auth: AUTH };
if (AUTH && auth.get().password) response.configured = true;
if (AUTH) response.jwtValid = jwt.verify(req.headers.authorization);
respond(HTTPSuccess(OK, response), res);
};

module.exports.tokens = {
get: (req, res) => {
const { tokens } = auth.get();
respond(HTTPSuccess(OK, tokens || []), res);
},
create: (req, res) => {
const { name } = req.body;
const tokens = auth.get().tokens || [];
tokens.unshift({ name, token: uuidv4() });
auth.set({ tokens });
respond(HTTPSuccess(OK), res);
},
delete: (req, res) => {
const { token } = req.params;
const currentTokens = auth.get().tokens || [];
const tokens = currentTokens.filter((obj) => obj.token !== token);
auth.set({ tokens });
respond(HTTPSuccess(OK, tokens), res);
},
};

module.exports.login = (req, res) => {
const { password } = req.body;
const { password: current } = auth.get();
if (password !== current) return respond(HTTPError(UNAUTHORIZED), res);
const token = jwt.sign();
respond(HTTPSuccess(OK, { token }), res);
};

module.exports.password = (req, res) => {
try {
const { password } = req.body;
auth.set({ password });
const token = jwt.sign();
respond(HTTPSuccess(OK, { token }), res);
} catch (error) {
respond(error, res);
}
};

module.exports.updatePassword = (req, res) => {
try {
const { password: currentPassword, newPassword } = req.body;
const { password } = auth.get();

if (currentPassword !== password) {
throw HTTPError(BAD_REQUEST, 'Password Incorrect');
}
auth.set({ secret: uuidv4(), password: newPassword });
respond(HTTPSuccess(OK), res);
} catch (error) {
respond(error, res);
}
};
Loading

0 comments on commit 0e1df10

Please sign in to comment.