feat: new user resource integrating postgresql
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import createNewDbConnection, {StoccaTreDbConn} from "./database.ts";
|
||||
import createNewDbConnection, { StoccaTreDbConn } from "./database.ts";
|
||||
import * as resources from "./resources/main.ts";
|
||||
import config from "./config.json" assert { type: "json" };
|
||||
import StoccaTreRequest, {HttpMethod} from "./StoccaTreRequest.ts";
|
||||
import config from "./config.ts";
|
||||
import StoccaTreRequest, { HttpMethod } from "./StoccaTreRequest.ts";
|
||||
import StoccaTreRequestHandler from "./StoccaTreRequestHandler.ts";
|
||||
import {JSONObject} from "./JSON.ts";
|
||||
import { JSONObject } from "./JSON.ts";
|
||||
|
||||
type StoccaTreApiBody = {
|
||||
data: JSONObject,
|
||||
error?: string,
|
||||
}
|
||||
data: JSONObject;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
function newStoccaTreApiBody(): StoccaTreApiBody {
|
||||
return {
|
||||
@@ -17,22 +17,22 @@ function newStoccaTreApiBody(): StoccaTreApiBody {
|
||||
}
|
||||
|
||||
type StoccaTreServer = StoccaTreRequestHandler & {
|
||||
dbConnection: StoccaTreDbConn,
|
||||
}
|
||||
dbConnection: StoccaTreDbConn;
|
||||
};
|
||||
|
||||
async function processRequest(server: StoccaTreServer, requestEvent: Deno.RequestEvent) {
|
||||
const route = /^https?:\/\/[^\/]*(\/.*)$/.exec(requestEvent.request.url)?.[1] ?? "/";
|
||||
const requestBody = (await requestEvent.request.body?.getReader().read())?.value?.toString() ?? null;
|
||||
console.log(requestBody);
|
||||
const requestBody = (await (await requestEvent.request.blob()).text()) ?? null;
|
||||
const method: HttpMethod = requestEvent.request.method as HttpMethod;
|
||||
const body: StoccaTreApiBody = newStoccaTreApiBody();
|
||||
const result = await server.handleRequest({
|
||||
route,
|
||||
body: requestBody,
|
||||
method,
|
||||
});
|
||||
const result = await server.handleRequest(new StoccaTreRequest(method, route, requestBody));
|
||||
if (result.error) {
|
||||
await requestEvent.respondWith(Response.json({ ...body, error: `Internal server error: ${result.error.message}` }, { status: 500 }));
|
||||
await requestEvent.respondWith(
|
||||
Response.json({
|
||||
...body,
|
||||
error: `Internal server error: ${result.error.message}`,
|
||||
}, { status: 500 }),
|
||||
);
|
||||
} else {
|
||||
body.data = result.just;
|
||||
await requestEvent.respondWith(Response.json(body, { status: 200 }));
|
||||
@@ -41,10 +41,11 @@ async function processRequest(server: StoccaTreServer, requestEvent: Deno.Reques
|
||||
|
||||
const stoccaTreListener = Deno.listen({ port: config.port ?? 8080 });
|
||||
|
||||
console.log(`Stocca Tre Server is running. Access it at: http://localhost:${ config.port }/`);
|
||||
console.log(`Stocca Tre Server is running. Access it at: http://localhost:${config.port}/`);
|
||||
|
||||
const database = await createNewDbConnection();
|
||||
const database = createNewDbConnection();
|
||||
const ingredientResource = new resources.IngredientResource(database);
|
||||
const userResource = new resources.UserResource(database);
|
||||
const stoccaTreServer: StoccaTreServer = {
|
||||
dbConnection: database,
|
||||
handleRequest: (request: StoccaTreRequest) => ingredientResource.handleRequest(request),
|
||||
|
||||
Reference in New Issue
Block a user