diff --git a/deno/config.ts b/deno/config.ts new file mode 100644 index 0000000..0b3dfe0 --- /dev/null +++ b/deno/config.ts @@ -0,0 +1,11 @@ +const config = { + database: { + user: "abc123", + password: "abc123", + host: "localhost", + port: "5432", + database: "abc123", + }, +}; + +export { config }; diff --git a/deno/content-types.ts b/deno/content-types.ts index e3cdcd2..1970f4a 100644 --- a/deno/content-types.ts +++ b/deno/content-types.ts @@ -1,8 +1,8 @@ import { Extensible } from "./interfaces"; -export type ContentType = string +export type ContentType = string; -const contentTypes= { +const contentTypes = { text: { plain: "text/plain", html: "text/html", diff --git a/deno/deps.ts b/deno/deps.ts new file mode 100644 index 0000000..869fb08 --- /dev/null +++ b/deno/deps.ts @@ -0,0 +1,7 @@ +// Database +export { Client as PostgresClient } from "https://deno.land/x/postgres@v0.19.3/mod.ts"; +export type { ClientOptions as PostgresOptions } from "https://deno.land/x/postgres@v0.19.3/mod.ts"; + +// Redis +export { connect as redisConnect } from "https://deno.land/x/redis@v0.37.1/mod.ts"; +export type { Redis } from "https://deno.land/x/redis@v0.37.1/mod.ts"; diff --git a/deno/main.ts b/deno/main.ts new file mode 100644 index 0000000..3678714 --- /dev/null +++ b/deno/main.ts @@ -0,0 +1,8 @@ +export function add(a: number, b: number): number { + return a + b; +} + +// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts +if (import.meta.main) { + console.log("Add 2 + 3 =", add(2, 3)); +} diff --git a/deno/main_test.ts b/deno/main_test.ts new file mode 100644 index 0000000..6bdafb8 --- /dev/null +++ b/deno/main_test.ts @@ -0,0 +1,6 @@ +import { assertEquals } from "@std/assert"; +import { add } from "./main.ts"; + +Deno.test(function addTest() { + assertEquals(add(2, 3), 5); +}); diff --git a/deno/routes.ts b/deno/routes.ts new file mode 100644 index 0000000..5f0f4e7 --- /dev/null +++ b/deno/routes.ts @@ -0,0 +1,101 @@ +type Method = "get" | "post" | "put" | "patch" | "delete"; + +import { HttpCode, httpCodes } from "./http-codes.ts"; +import { ContentType, contentTypes } from "./content-types"; +import { services } from "./services"; + +type Response = { + code: HttpCode; + contentType: ContentType; + result: string; +}; + +type Handler = (req: Request) => Response; + +const phandler: Handler = (req: Request) => { + const code = httpCodes.success.OK; + return { + code, + result: "it is ok ", + contentType: contentTypes.text.plain, + }; +}; + +type Route = { + path: string; + methods: Method[]; + handler: Handler; +}; + +const routes: Route[] = [ + { + path: "/ok", + methods: ["get"], + handler: (req) => { + const code = httpCodes.success.OK; + const rn = services.random.randomNumber(); + + return { + code, + result: "it is ok " + rn, + contentType: contentTypes.text.plain, + }; + }, + }, + { + path: "/alsook", + methods: ["get"], + handler: (req) => { + const code = httpCodes.success.OK; + return { + code, + result: "it is also ok", + contentType: contentTypes.text.plain, + }; + }, + }, +]; + +type DenoRequest = globalThis.Request; +type UserRequest = {}; +type Request = { + pattern: string; + path: string; + method: Method; + parameters: object; + denoRequest: globalThis.Request; +}; +type ProcessedRoute = { pattern: URLPattern; method: Method; handler: Handler }; +const processedRoutes: ProcessedRoute[] = routes.map( + (route: Route, idx: number, allRoutes: Route[]) => { + const pattern: URLPattern = new URLPattern({ pathname: route.path }); + const method: Method = route.method; + const handler = (denoRequest: DenoRequest) => { + const req: Request = { + pattern: route.pattern, + path: denoRequest.path, + method: denoRequest.method, + parameters: { one: 1, two: 2 }, + denoRequest, + }; + return route.handler(req); + }; + + return { pattern, method, handler }; + }, +); + +function handler(req: globalThis.Request): globalThis.Response { + for (const [idx, pr] of processedRoutes.entries()) { + const match = pr.pattern.exec(req.url); + if (match) { + const resp = pr.handler(req); + + return new globalThis.Response(resp.result); + } + } + + return new globalThis.Response("not found", { status: 404 }); +} + +Deno.serve(handler); diff --git a/deno/services.ts b/deno/services.ts new file mode 100644 index 0000000..9659010 --- /dev/null +++ b/deno/services.ts @@ -0,0 +1,24 @@ +// services.ts + +import { randomNumber } from "https://deno.land/x/random_number/mod.ts"; + +// import {Client} from './deps' + +import { config } from "./config.ts"; + +//const database = Client({ + +//}) + +const database = {}; + +const random = { + randomNumber, +}; + +const services = { + database, + random, +}; + +export { services };