user create wip
This commit is contained in:
16
api/src/app.ts
Normal file
16
api/src/app.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import Server, { Services } from './framework/express/server';
|
||||
import Mongo from './framework/mongo/mongo';
|
||||
import ip from "ip";
|
||||
|
||||
const PORT = 8000;
|
||||
const MONGOURI = "mongodb://localhost:27017";
|
||||
|
||||
const mongo = new Mongo(MONGOURI);
|
||||
|
||||
const services: Services = {
|
||||
db: mongo.getDb()
|
||||
}
|
||||
|
||||
const server = new Server(services);
|
||||
|
||||
server.start(PORT, () => console.log(`Running on http://${ip.address()}:${PORT}`));
|
9
api/src/framework/express/middleware.ts
Normal file
9
api/src/framework/express/middleware.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
export function BeforeEach(): RequestHandler {
|
||||
return (req, res, next) => {
|
||||
req.headers["request-id"] = randomUUID();
|
||||
next();
|
||||
}
|
||||
}
|
11
api/src/framework/express/router.ts
Normal file
11
api/src/framework/express/router.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Router } from "express";
|
||||
import { Services } from "./server";
|
||||
import * as user from "./user";
|
||||
|
||||
export function getRoutes(services: Services) {
|
||||
const router = Router();
|
||||
|
||||
router.use("/user", user.getRoutes(services));
|
||||
|
||||
return router;
|
||||
}
|
27
api/src/framework/express/server.ts
Normal file
27
api/src/framework/express/server.ts
Normal file
@ -0,0 +1,27 @@
|
||||
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;
|
||||
}
|
||||
|
||||
class Server {
|
||||
private app: Express;
|
||||
|
||||
constructor(services: Services) {
|
||||
this.app = express();
|
||||
this.app.use(express.json());
|
||||
this.app.use(cors());
|
||||
this.app.use(BeforeEach());
|
||||
this.app.use(router.getRoutes(services));
|
||||
}
|
||||
|
||||
start(port: number, func: () => void): void {
|
||||
this.app.listen(port, func);
|
||||
}
|
||||
}
|
||||
|
||||
export default Server;
|
30
api/src/framework/express/user.ts
Normal file
30
api/src/framework/express/user.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { RequestHandler, Router } from 'express';
|
||||
import { Create } from '../../functions/user';
|
||||
import { Services } from './server';
|
||||
|
||||
export function LoginHandler(services: Services): RequestHandler {
|
||||
return async (req, res) => {
|
||||
return res.send('Hey!\n');
|
||||
};
|
||||
}
|
||||
|
||||
export function CreateHandler(services: Services): RequestHandler {
|
||||
const createUser = Create(services);
|
||||
|
||||
return async (req, res) => {
|
||||
const user = await createUser(req.get("request-id"), req.body);
|
||||
|
||||
res.send(user);
|
||||
}
|
||||
}
|
||||
|
||||
export function getRoutes(services: Services) {
|
||||
const router = Router();
|
||||
|
||||
router.post('/login', LoginHandler(services));
|
||||
router.post('/create', CreateHandler(services));
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
|
16
api/src/framework/mongo/mongo.ts
Normal file
16
api/src/framework/mongo/mongo.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { MongoClient, Db } from "mongodb";
|
||||
|
||||
class Mongo {
|
||||
private client: MongoClient;
|
||||
private dbName: string;
|
||||
|
||||
constructor(uri: string) {
|
||||
this.client = new MongoClient(uri);
|
||||
}
|
||||
|
||||
getDb(): Db {
|
||||
return this.client.db(this.dbName);
|
||||
}
|
||||
}
|
||||
|
||||
export default Mongo;
|
14
api/src/framework/mongo/user.ts
Normal file
14
api/src/framework/mongo/user.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { CreateUserBody, User, UserInfo } from "@core";
|
||||
import { Services } from "../express/server";
|
||||
|
||||
export function Create(services: Services): (tracker: string, user: CreateUserBody) => Promise<UserInfo> {
|
||||
const { db } = services;
|
||||
const coll = db.collection("users");
|
||||
|
||||
return async (tracker, user) => {
|
||||
await coll.insertOne(user);
|
||||
return {
|
||||
name: "test"
|
||||
}
|
||||
}
|
||||
}
|
11
api/src/functions/user.ts
Normal file
11
api/src/functions/user.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { CreateUserBody, UserInfo } from "@core";
|
||||
import { Services } from "../framework/express/server";
|
||||
import * as mongo from "../framework/mongo/user";
|
||||
|
||||
export function Create(services: Services): (tracker: string, user: CreateUserBody) => Promise<UserInfo> {
|
||||
const createUser = mongo.Create(services);
|
||||
|
||||
return async (tracker, user) => {
|
||||
return createUser(tracker, user);
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
import cors from 'cors';
|
||||
import express from 'express';
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use(cors());
|
||||
app.get('/', (req, res) => res.send('coucou ?\n'));
|
||||
|
||||
app.listen(8000, () => console.log('running...'));
|
Reference in New Issue
Block a user