From 5538a042df25bcb930f3eaf25a43df7e28ddb8e6 Mon Sep 17 00:00:00 2001 From: Yanis Rigaudeau Date: Fri, 14 Oct 2022 18:49:09 +0200 Subject: [PATCH] new services + new scripts --- api/src/app.ts | 14 ++++-- api/src/entities/user.ts | 19 +++++++- api/src/framework/express/router.ts | 2 +- api/src/framework/express/server.ts | 6 +-- api/src/framework/express/user.ts | 2 +- api/src/framework/mongo/mongo.ts | 15 ------ api/src/framework/mongo/user.ts | 36 ++++++-------- api/src/functions/user.ts | 7 ++- apps.json | 3 ++ build.py | 8 ---- core/package-lock.json | 13 +++++ core/package.json | 1 + core/src/user.ts | 12 +++++ install.py | 8 ---- prettier.py | 8 ---- scripts/build.py | 12 +++++ deploy.sh => scripts/deploy.sh | 4 +- scripts/install.py | 12 +++++ scripts/prettier.py | 12 +++++ www/package-lock.json | 18 +++++++ www/package.json | 1 + www/src/App.svelte | 40 ++++++++-------- www/src/components/test3D.svelte | 73 ++++++++++++++++++----------- 23 files changed, 200 insertions(+), 126 deletions(-) delete mode 100644 api/src/framework/mongo/mongo.ts create mode 100644 apps.json delete mode 100755 build.py delete mode 100755 install.py delete mode 100755 prettier.py create mode 100755 scripts/build.py rename deploy.sh => scripts/deploy.sh (86%) create mode 100755 scripts/install.py create mode 100755 scripts/prettier.py diff --git a/api/src/app.ts b/api/src/app.ts index 4bac630..9e05094 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -1,15 +1,21 @@ -import Server, { Services } from './framework/express/server'; -import Mongo from './framework/mongo/mongo'; +import Server from './framework/express/server'; import ip from 'ip'; +import UserModel from './framework/mongo/user'; +import { MongoClient } from 'mongodb'; const PORT = 8000; const MONGOURI = 'mongodb://localhost:27017'; const DBNAME = 'dev'; -const mongo = new Mongo(MONGOURI); +export type Services = { + userModel: UserModel; +}; + +const mongo = new MongoClient(MONGOURI); +const db = mongo.db(DBNAME); const services: Services = { - db: mongo.getDb(DBNAME), + userModel: new UserModel(db), }; const server = new Server(services); diff --git a/api/src/entities/user.ts b/api/src/entities/user.ts index 3e6517d..f82ce97 100644 --- a/api/src/entities/user.ts +++ b/api/src/entities/user.ts @@ -1,4 +1,10 @@ -import { UserCtor, UserInfo } from '@core'; +import { + UserCtor, + UserInfo, + UserInfoWithPassword, + UserWithPasswordCtor, +} from '@core'; +import { createHash, Hash } from 'crypto'; import { Entity } from './entity'; export class User extends Entity implements UserInfo { @@ -12,7 +18,18 @@ export class User extends Entity implements UserInfo { Info(): UserInfo { return { + uuid: this.uuid, name: this.name, }; } } + +export class UserWithPassword extends User implements UserInfoWithPassword { + password: Hash; + + constructor(raw: UserWithPasswordCtor) { + super(raw); + + this.password = createHash('sha256').update(`${this.uuid}+${raw.password}`); + } +} diff --git a/api/src/framework/express/router.ts b/api/src/framework/express/router.ts index 98e4fe1..ea69708 100644 --- a/api/src/framework/express/router.ts +++ b/api/src/framework/express/router.ts @@ -1,5 +1,5 @@ import { Router } from 'express'; -import { Services } from './server'; +import { Services } from '../../app'; import * as user from './user'; export function getRoutes(services: Services) { diff --git a/api/src/framework/express/server.ts b/api/src/framework/express/server.ts index fad67c2..b193a00 100644 --- a/api/src/framework/express/server.ts +++ b/api/src/framework/express/server.ts @@ -2,11 +2,7 @@ import express, { Express } from 'express'; import cors from 'cors'; import * as router from './router'; import { BeforeEach } from './middleware'; -import { Db } from 'mongodb'; - -export type Services = { - db: Db; -}; +import { Services } from '../../app'; class Server { private app: Express; diff --git a/api/src/framework/express/user.ts b/api/src/framework/express/user.ts index 7b3bde3..e60957d 100644 --- a/api/src/framework/express/user.ts +++ b/api/src/framework/express/user.ts @@ -1,7 +1,7 @@ import { RequestHandler, Router } from 'express'; +import { Services } from '../../app'; import { Create } from '../../functions/user'; import { getId } from './middleware'; -import { Services } from './server'; export function LoginHandler(services: Services): RequestHandler { return async (req, res) => { diff --git a/api/src/framework/mongo/mongo.ts b/api/src/framework/mongo/mongo.ts deleted file mode 100644 index 61feecf..0000000 --- a/api/src/framework/mongo/mongo.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { MongoClient, Db } from 'mongodb'; - -class Mongo { - private client: MongoClient; - - constructor(uri: string) { - this.client = new MongoClient(uri); - } - - getDb(dbName: string): Db { - return this.client.db(dbName); - } -} - -export default Mongo; diff --git a/api/src/framework/mongo/user.ts b/api/src/framework/mongo/user.ts index 3c968cb..5bc96b6 100644 --- a/api/src/framework/mongo/user.ts +++ b/api/src/framework/mongo/user.ts @@ -1,28 +1,22 @@ +import { Collection, Db } from 'mongodb'; import { User } from '../../entities/user'; -import { Services } from '../express/server'; -export function Create( - services: Services, -): (tracker: string, user: User) => Promise { - const { db } = services; - const coll = db.collection('users'); - const readUser = Read(services); +class UserModel { + private collection: Collection; - return async (tracker, user) => { - await coll.insertOne(user); - return readUser(tracker, user.uuid); - }; -} + constructor(db: Db) { + this.collection = db.collection('users'); + } -export function Read( - services: Services, -): (tracker: string, uuid: string) => Promise { - const { db } = services; - const coll = db.collection('users'); - - return async (tracker, uuid) => { - const user = await coll.findOne({ uuid }); + public async create(tracker: string, user: User) { + await this.collection.insertOne(user); + return this.read(tracker, user.uuid); + } + public async read(tracker: string, uuid: string) { + const user = await this.collection.findOne({ uuid }); return new User(user || { name: 'not found' }); - }; + } } + +export default UserModel; diff --git a/api/src/functions/user.ts b/api/src/functions/user.ts index 4184bdb..8ad5bb1 100644 --- a/api/src/functions/user.ts +++ b/api/src/functions/user.ts @@ -1,15 +1,14 @@ import { CreateUserBody, UserInfo } from '@core'; +import { Services } from '../app'; import { User } from '../entities/user'; -import { Services } from '../framework/express/server'; -import * as mongo from '../framework/mongo/user'; export function Create( services: Services, ): (tracker: string, raw: CreateUserBody) => Promise { - const createUser = mongo.Create(services); + const { userModel } = services; return async (tracker, raw) => { - const user = await createUser(tracker, new User(raw)); + const user = await userModel.create(tracker, new User(raw)); return user.Info(); }; } diff --git a/apps.json b/apps.json new file mode 100644 index 0000000..df0af35 --- /dev/null +++ b/apps.json @@ -0,0 +1,3 @@ +{ + "apps": ["core", "api", "www"] +} diff --git a/build.py b/build.py deleted file mode 100755 index e378885..0000000 --- a/build.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/python -import os - -for dir in os.listdir('.'): - if os.path.isdir(dir) and not dir.startswith('.'): - os.chdir(dir) - os.system('npm run build') - os.chdir('..') diff --git a/core/package-lock.json b/core/package-lock.json index 400b0e3..51809fa 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -8,10 +8,17 @@ "name": "core", "version": "1.0.0", "devDependencies": { + "@types/node": "^18.8.4", "prettier": "^2.7.1", "typescript": "^4.8.3" } }, + "node_modules/@types/node": { + "version": "18.8.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.8.4.tgz", + "integrity": "sha512-WdlVphvfR/GJCLEMbNA8lJ0lhFNBj4SW3O+O5/cEGw9oYrv0al9zTwuQsq+myDUXgNx2jgBynoVgZ2MMJ6pbow==", + "dev": true + }, "node_modules/prettier": { "version": "2.7.1", "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz", @@ -42,6 +49,12 @@ } }, "dependencies": { + "@types/node": { + "version": "18.8.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.8.4.tgz", + "integrity": "sha512-WdlVphvfR/GJCLEMbNA8lJ0lhFNBj4SW3O+O5/cEGw9oYrv0al9zTwuQsq+myDUXgNx2jgBynoVgZ2MMJ6pbow==", + "dev": true + }, "prettier": { "version": "2.7.1", "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz", diff --git a/core/package.json b/core/package.json index ec61e1c..25d9ddb 100644 --- a/core/package.json +++ b/core/package.json @@ -9,6 +9,7 @@ "prettier": "prettier -w ./src" }, "devDependencies": { + "@types/node": "^18.8.4", "prettier": "^2.7.1", "typescript": "^4.8.3" } diff --git a/core/src/user.ts b/core/src/user.ts index 3e42771..66c0487 100644 --- a/core/src/user.ts +++ b/core/src/user.ts @@ -1,11 +1,19 @@ import { EntityInfo } from './entity'; +import { Hash } from 'crypto'; export type UserInfo = { + uuid: string; name: string; }; +export type UserInfoWithPassword = { + password: Hash; +} & UserInfo; + export type User = UserInfo & EntityInfo; +export type UserWithPassword = UserInfoWithPassword & EntityInfo; + export type CreateUserBody = { name: string; password: string; @@ -14,3 +22,7 @@ export type CreateUserBody = { export type LoginUserBody = CreateUserBody; export type UserCtor = Partial; + +export type UserWithPasswordCtor = { + password: string; +} & Partial>; diff --git a/install.py b/install.py deleted file mode 100755 index 2dff5d4..0000000 --- a/install.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/python -import os - -for dir in os.listdir('.'): - if os.path.isdir(dir) and not dir.startswith('.'): - os.chdir(dir) - os.system('npm ci') - os.chdir('..') diff --git a/prettier.py b/prettier.py deleted file mode 100755 index c479420..0000000 --- a/prettier.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/python -import os - -for dir in os.listdir('.'): - if os.path.isdir(dir) and not dir.startswith('.'): - os.chdir(dir) - os.system('npm run prettier') - os.chdir('..') diff --git a/scripts/build.py b/scripts/build.py new file mode 100755 index 0000000..1349ae1 --- /dev/null +++ b/scripts/build.py @@ -0,0 +1,12 @@ +#!/usr/bin/python +import os +import json + +apps = json.load(open('apps.json'))['apps'] + +print(len(apps), 'apps:', apps) + +for app in apps: + os.chdir(app) + os.system('npm run build') + os.chdir('..') diff --git a/deploy.sh b/scripts/deploy.sh similarity index 86% rename from deploy.sh rename to scripts/deploy.sh index 12e6602..567544e 100755 --- a/deploy.sh +++ b/scripts/deploy.sh @@ -2,8 +2,8 @@ useradd node -md /home/node -s /usr/sbin/nologin -python3 install.py -python3 build.py +python3 scripts/install.py +python3 scripts/build.py rm -r /var/www/html/* mv www/public/* /var/www/html diff --git a/scripts/install.py b/scripts/install.py new file mode 100755 index 0000000..fc08757 --- /dev/null +++ b/scripts/install.py @@ -0,0 +1,12 @@ +#!/usr/bin/python +import os +import json + +apps = json.load(open('apps.json'))['apps'] + +print(len(apps), 'apps:', apps) + +for app in apps: + os.chdir(app) + os.system('npm ci') + os.chdir('..') diff --git a/scripts/prettier.py b/scripts/prettier.py new file mode 100755 index 0000000..1a32d9e --- /dev/null +++ b/scripts/prettier.py @@ -0,0 +1,12 @@ +#!/usr/bin/python +import os +import json + +apps = json.load(open('apps.json'))['apps'] + +print(len(apps), 'apps:', apps) + +for app in apps: + os.chdir(app) + os.system('npm run prettier') + os.chdir('..') diff --git a/www/package-lock.json b/www/package-lock.json index 670aa05..0147c13 100644 --- a/www/package-lock.json +++ b/www/package-lock.json @@ -19,6 +19,7 @@ "@tsconfig/svelte": "^2.0.0", "@types/three": "^0.144.0", "prettier": "^2.7.1", + "prettier-plugin-svelte": "^2.8.0", "rollup": "^2.3.4", "rollup-plugin-css-only": "^3.1.0", "rollup-plugin-livereload": "^2.0.0", @@ -1027,6 +1028,16 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, + "node_modules/prettier-plugin-svelte": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/prettier-plugin-svelte/-/prettier-plugin-svelte-2.8.0.tgz", + "integrity": "sha512-QlXv/U3bUszks3XYDPsk1fsaQC+fo2lshwKbcbO+lrSVdJ+40mB1BfL8OCAk1W9y4pJxpqO/4gqm6NtF3zNGCw==", + "dev": true, + "peerDependencies": { + "prettier": "^1.16.4 || ^2.0.0", + "svelte": "^3.2.0" + } + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -2381,6 +2392,13 @@ "integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==", "dev": true }, + "prettier-plugin-svelte": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/prettier-plugin-svelte/-/prettier-plugin-svelte-2.8.0.tgz", + "integrity": "sha512-QlXv/U3bUszks3XYDPsk1fsaQC+fo2lshwKbcbO+lrSVdJ+40mB1BfL8OCAk1W9y4pJxpqO/4gqm6NtF3zNGCw==", + "dev": true, + "requires": {} + }, "queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", diff --git a/www/package.json b/www/package.json index 68d7480..0f5bdd7 100644 --- a/www/package.json +++ b/www/package.json @@ -18,6 +18,7 @@ "@tsconfig/svelte": "^2.0.0", "@types/three": "^0.144.0", "prettier": "^2.7.1", + "prettier-plugin-svelte": "^2.8.0", "rollup": "^2.3.4", "rollup-plugin-css-only": "^3.1.0", "rollup-plugin-livereload": "^2.0.0", diff --git a/www/src/App.svelte b/www/src/App.svelte index 7f737cd..b39321e 100644 --- a/www/src/App.svelte +++ b/www/src/App.svelte @@ -1,30 +1,30 @@
- +
diff --git a/www/src/components/test3D.svelte b/www/src/components/test3D.svelte index e35a608..7893a1e 100644 --- a/www/src/components/test3D.svelte +++ b/www/src/components/test3D.svelte @@ -1,51 +1,68 @@ - + \ No newline at end of file +