feat: improving infrastructure
This commit is contained in:
@@ -7,14 +7,6 @@ type JSONValue =
|
|||||||
|
|
||||||
type JSONArray = Array<JSONValue>;
|
type JSONArray = Array<JSONValue>;
|
||||||
|
|
||||||
type Maybe<T> = {
|
export interface JSONObject {
|
||||||
just: T,
|
|
||||||
error: null,
|
|
||||||
} | {
|
|
||||||
just: null,
|
|
||||||
error: { message: string },
|
|
||||||
};
|
|
||||||
|
|
||||||
interface JSONObject {
|
|
||||||
[x: string]: JSONValue;
|
[x: string]: JSONValue;
|
||||||
}
|
}
|
||||||
2
server/Maybe.ts
Normal file
2
server/Maybe.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export type Maybe<T> = Just<T> | { just?: never, error: { message: string }};
|
||||||
|
export type Just<T> = { just: T, error?: never };
|
||||||
@@ -3,4 +3,5 @@ export type HttpMethod = "POST" | "GET" | "PUT" | "DELETE";
|
|||||||
export default interface StoccaTreRequest {
|
export default interface StoccaTreRequest {
|
||||||
method: HttpMethod,
|
method: HttpMethod,
|
||||||
route: string,
|
route: string,
|
||||||
|
body: string | null,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import StoccaTreRequest from "./StoccaTreRequest.ts";
|
import StoccaTreRequest from "./StoccaTreRequest.ts";
|
||||||
|
import {Maybe} from "./Maybe.ts";
|
||||||
|
import {JSONObject} from "./JSON.ts";
|
||||||
|
|
||||||
export default interface StoccaTreRequestHandler {
|
export default interface StoccaTreRequestHandler {
|
||||||
handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>>,
|
handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>>,
|
||||||
|
|||||||
6
server/config.json
Normal file
6
server/config.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"username": "root",
|
||||||
|
"port": 8080,
|
||||||
|
"hostname": "127.0.0.1",
|
||||||
|
"password": "sekna123jk"
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Client } from "https://deno.land/x/mysql/mod.ts";
|
import { Client } from "https://deno.land/x/mysql/mod.ts";
|
||||||
import config from "@/config.json" assert { type: "json" };
|
import config from "./config.json" assert { type: "json" };
|
||||||
|
import {Maybe} from "./Maybe.ts";
|
||||||
|
|
||||||
export type WithoutId<T> = Omit<T, "id">;
|
export type WithoutId<T> = Omit<T, "id">;
|
||||||
export interface StoccaTreDbConn {
|
export interface StoccaTreDbConn {
|
||||||
@@ -19,7 +20,6 @@ export default async function createNewDbConnection(): Promise<StoccaTreDbConn>
|
|||||||
const result = await mysqlClient.query(query);
|
const result = await mysqlClient.query(query);
|
||||||
return {
|
return {
|
||||||
just: result as T,
|
just: result as T,
|
||||||
error: null,
|
|
||||||
};
|
};
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
if (e && typeof (e as { message?: any }).message === "string") {
|
if (e && typeof (e as { message?: any }).message === "string") {
|
||||||
@@ -29,7 +29,6 @@ export default async function createNewDbConnection(): Promise<StoccaTreDbConn>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
just: null,
|
|
||||||
error: {
|
error: {
|
||||||
message: errMessage
|
message: errMessage
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
{
|
{
|
||||||
"imports": {
|
"imports": {
|
||||||
"/": "./"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
import createNewDbConnection, {StoccaTreDbConn} from "/database.ts";
|
import createNewDbConnection, {StoccaTreDbConn} from "./database.ts";
|
||||||
import * as resources from "/resources/main.ts";
|
import * as resources from "./resources/main.ts";
|
||||||
import config from "/config.json" assert { type: "json" };
|
import config from "./config.json" assert { type: "json" };
|
||||||
import StoccaTreRequest, {HttpMethod} from "/StoccaTreRequest.ts";
|
import StoccaTreRequest, {HttpMethod} from "./StoccaTreRequest.ts";
|
||||||
import StoccaTreRequestHandler from "/StoccaTreRequestHandler.ts";
|
import StoccaTreRequestHandler from "./StoccaTreRequestHandler.ts";
|
||||||
|
import {JSONObject} from "./JSON.ts";
|
||||||
const server = Deno.listen({ port: config.port ?? 8080 });
|
|
||||||
|
|
||||||
console.log(`Stocca Tre Server is running. Access it at: http://localhost:${ config.port }/`);
|
|
||||||
|
|
||||||
type StoccaTreApiBody = {
|
type StoccaTreApiBody = {
|
||||||
data: JSONObject,
|
data: JSONObject,
|
||||||
@@ -19,41 +16,43 @@ function newStoccaTreApiBody(): StoccaTreApiBody {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
type StoccaTreServer = {
|
type StoccaTreServer = StoccaTreRequestHandler & {
|
||||||
dbConnection: StoccaTreDbConn,
|
dbConnection: StoccaTreDbConn,
|
||||||
handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processRequest(server: StoccaTreServer, requestEvent: Deno.RequestEvent) {
|
async function processRequest(server: StoccaTreServer, requestEvent: Deno.RequestEvent) {
|
||||||
const route = requestEvent.request.destination;
|
const route = /^https?:\/\/[^\/]*(\/.*)$/.exec(requestEvent.request.url)?.[1] ?? "/";
|
||||||
const requestMethod = requestEvent.request.method;
|
const requestBody = (await requestEvent.request.body?.getReader().read())?.value?.toString() ?? null;
|
||||||
console.log(route, requestMethod);
|
console.log(requestBody);
|
||||||
|
const method: HttpMethod = requestEvent.request.method as HttpMethod;
|
||||||
const body: StoccaTreApiBody = newStoccaTreApiBody();
|
const body: StoccaTreApiBody = newStoccaTreApiBody();
|
||||||
const maybeResult = await server.handleRequest({
|
const result = await server.handleRequest({
|
||||||
route,
|
route,
|
||||||
method: requestMethod as HttpMethod,
|
body: requestBody,
|
||||||
|
method,
|
||||||
});
|
});
|
||||||
if (maybeResult.error) {
|
if (result.error) {
|
||||||
await requestEvent.respondWith(Response.json({ ...body, error: `Internal server error: ${maybeResult.error.message}` }, { status: 500 }));
|
await requestEvent.respondWith(Response.json({ ...body, error: `Internal server error: ${result.error.message}` }, { status: 500 }));
|
||||||
} else {
|
} else {
|
||||||
body.data = maybeResult.just;
|
body.data = result.just;
|
||||||
await requestEvent.respondWith(Response.json(body, { status: 200 }));
|
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 database = await createNewDbConnection();
|
||||||
const ingredientResource = new resources.IngredientResource(database);
|
const ingredientResource = new resources.IngredientResource(database);
|
||||||
|
const stoccaTreServer: StoccaTreServer = {
|
||||||
|
dbConnection: database,
|
||||||
|
handleRequest: (request: StoccaTreRequest) => ingredientResource.handleRequest(request),
|
||||||
|
};
|
||||||
|
|
||||||
for await (const conn of server) {
|
for await (const conn of stoccaTreListener) {
|
||||||
await serveHttp(conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function serveHttp(conn: Deno.Conn) {
|
|
||||||
const httpConn = Deno.serveHttp(conn);
|
const httpConn = Deno.serveHttp(conn);
|
||||||
for await (const requestEvent of httpConn) {
|
for await (const requestEvent of httpConn) {
|
||||||
await processRequest({
|
await processRequest(stoccaTreServer, requestEvent);
|
||||||
dbConnection: database,
|
|
||||||
handleRequest: (request: StoccaTreRequest) => ingredientResource.handleRequest(request),
|
|
||||||
}, requestEvent);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "stocca-tre-server",
|
"name": "stocca-tre-server",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "deno run --allow-net --import-map=importMap.json main.ts"
|
"start": "deno run --allow-net --import-map=import_map.json main.ts"
|
||||||
},
|
},
|
||||||
"description": "Backend for StoccaTre",
|
"description": "Backend for StoccaTre",
|
||||||
"author": "Daniel Ledda",
|
"author": "Daniel Ledda",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {StoccaTreDbConn, WithoutId} from "../../database.ts";
|
import {StoccaTreDbConn, WithoutId} from "../../database.ts";
|
||||||
import {IngredientModel} from "./IngredientModel.ts";
|
import {IngredientModel} from "./IngredientModel.ts";
|
||||||
|
import {Maybe} from "../../Maybe.ts";
|
||||||
|
|
||||||
export default class IngredientCollection {
|
export default class IngredientCollection {
|
||||||
private dbConnection: StoccaTreDbConn;
|
private dbConnection: StoccaTreDbConn;
|
||||||
@@ -11,17 +12,16 @@ export default class IngredientCollection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<any> {
|
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<any> {
|
||||||
const result = await this.dbConnection.query<any>(
|
return await this.dbConnection.query<any>(
|
||||||
`INSERT INTO ingredients (id, name, displayName, displayNameDE) VALUES (NULL, '${ingredient.name}', '${ingredient.displayName}', '${ingredient.displayNameDE}');`
|
`INSERT INTO ingredients (id, name, displayName, displayNameDE) VALUES (NULL, '${ingredient.name}', '${ingredient.displayName}', '${ingredient.displayNameDE}');`
|
||||||
);
|
);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
|
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
|
||||||
if (!this.allGotten) {
|
if (!this.allGotten) {
|
||||||
const result = await this.dbConnection.query<IngredientModel[]>("SELECT * FROM ingredients");
|
const result = await this.dbConnection.query<IngredientModel[]>("SELECT * FROM ingredients");
|
||||||
if (result.just) {
|
if (!result.error) {
|
||||||
result.just.forEach((ingredient) => this.mapById.set(ingredient.id, ingredient));
|
result.just.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
|
||||||
} else {
|
} else {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,6 @@ export default class IngredientCollection {
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
just: this.mapById.values(),
|
just: this.mapById.values(),
|
||||||
error: null,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import {StoccaTreDbConn} from "/database.ts";
|
import {StoccaTreDbConn} from "../../database.ts";
|
||||||
import IngredientCollection from "IngredientCollection.ts";
|
import IngredientCollection from "./IngredientCollection.ts";
|
||||||
import StoccaTreRequest from "/StoccaTreRequest.ts";
|
import StoccaTreRequest from "../../StoccaTreRequest.ts";
|
||||||
|
import {Maybe} from "../../Maybe.ts";
|
||||||
|
import {JSONObject} from "../../JSON.ts";
|
||||||
|
import {IngredientModel} from "./IngredientModel.ts";
|
||||||
|
|
||||||
export default class IngredientResource {
|
export default class IngredientResource {
|
||||||
private dbConnection: StoccaTreDbConn;
|
private dbConnection: StoccaTreDbConn;
|
||||||
@@ -11,20 +14,40 @@ export default class IngredientResource {
|
|||||||
this.collection = new IngredientCollection(dbConnection);
|
this.collection = new IngredientCollection(dbConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static isIngredient(json: JSONObject): json is IngredientModel {
|
||||||
|
if (json) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||||
return await this.allIngredients(request);
|
switch (request.route) {
|
||||||
|
case "/add":
|
||||||
|
if (request.method === "POST") {
|
||||||
|
const ingredient = JSON.parse(request.body ?? "");
|
||||||
|
return await this.collection.addIngredient(JSON.parse(request.body));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "/all":
|
||||||
|
return await this.allIngredients(request);
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return { error: {message: "Invalid route" }};
|
||||||
}
|
}
|
||||||
|
|
||||||
async allIngredients(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
async allIngredients(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||||
const getAllIngredientResult = await this.collection.getAllIngredients();
|
const getAllIngredientResult = await this.collection.getAllIngredients();
|
||||||
if (!getAllIngredientResult.error) {
|
if (getAllIngredientResult.error) {
|
||||||
return getAllIngredientResult.just;
|
return getAllIngredientResult;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
just: {
|
just: {
|
||||||
ingredients: Array.from(getAllIngredientResult.just),
|
ingredients: Array.from(getAllIngredientResult.just),
|
||||||
},
|
},
|
||||||
error: "",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "../tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"allowJs": false,
|
|
||||||
"lib": [ "ES2021" ]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user