feat: improving infrastructure
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
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 StoccaTreRequestHandler from "/StoccaTreRequestHandler.ts";
|
||||
|
||||
const server = Deno.listen({ port: config.port ?? 8080 });
|
||||
|
||||
console.log(`Stocca Tre Server is running. Access it at: http://localhost:${ config.port }/`);
|
||||
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 StoccaTreRequestHandler from "./StoccaTreRequestHandler.ts";
|
||||
import {JSONObject} from "./JSON.ts";
|
||||
|
||||
type StoccaTreApiBody = {
|
||||
data: JSONObject,
|
||||
@@ -19,41 +16,43 @@ function newStoccaTreApiBody(): StoccaTreApiBody {
|
||||
};
|
||||
}
|
||||
|
||||
type StoccaTreServer = {
|
||||
type StoccaTreServer = StoccaTreRequestHandler & {
|
||||
dbConnection: StoccaTreDbConn,
|
||||
handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>>,
|
||||
}
|
||||
|
||||
async function processRequest(server: StoccaTreServer, requestEvent: Deno.RequestEvent) {
|
||||
const route = requestEvent.request.destination;
|
||||
const requestMethod = requestEvent.request.method;
|
||||
console.log(route, requestMethod);
|
||||
const route = /^https?:\/\/[^\/]*(\/.*)$/.exec(requestEvent.request.url)?.[1] ?? "/";
|
||||
const requestBody = (await requestEvent.request.body?.getReader().read())?.value?.toString() ?? null;
|
||||
console.log(requestBody);
|
||||
const method: HttpMethod = requestEvent.request.method as HttpMethod;
|
||||
const body: StoccaTreApiBody = newStoccaTreApiBody();
|
||||
const maybeResult = await server.handleRequest({
|
||||
const result = await server.handleRequest({
|
||||
route,
|
||||
method: requestMethod as HttpMethod,
|
||||
body: requestBody,
|
||||
method,
|
||||
});
|
||||
if (maybeResult.error) {
|
||||
await requestEvent.respondWith(Response.json({ ...body, error: `Internal server error: ${maybeResult.error.message}` }, { status: 500 }));
|
||||
if (result.error) {
|
||||
await requestEvent.respondWith(Response.json({ ...body, error: `Internal server error: ${result.error.message}` }, { status: 500 }));
|
||||
} else {
|
||||
body.data = maybeResult.just;
|
||||
body.data = result.just;
|
||||
await requestEvent.respondWith(Response.json(body, { status: 200 }));
|
||||
}
|
||||
}
|
||||
|
||||
const stoccaTreListener = Deno.listen({ port: config.port ?? 8080 });
|
||||
|
||||
console.log(`Stocca Tre Server is running. Access it at: http://localhost:${ config.port }/`);
|
||||
|
||||
const database = await createNewDbConnection();
|
||||
const ingredientResource = new resources.IngredientResource(database);
|
||||
const stoccaTreServer: StoccaTreServer = {
|
||||
dbConnection: database,
|
||||
handleRequest: (request: StoccaTreRequest) => ingredientResource.handleRequest(request),
|
||||
};
|
||||
|
||||
for await (const conn of server) {
|
||||
await serveHttp(conn);
|
||||
}
|
||||
|
||||
async function serveHttp(conn: Deno.Conn) {
|
||||
for await (const conn of stoccaTreListener) {
|
||||
const httpConn = Deno.serveHttp(conn);
|
||||
for await (const requestEvent of httpConn) {
|
||||
await processRequest({
|
||||
dbConnection: database,
|
||||
handleRequest: (request: StoccaTreRequest) => ingredientResource.handleRequest(request),
|
||||
}, requestEvent);
|
||||
await processRequest(stoccaTreServer, requestEvent);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user