Add more files
This commit is contained in:
11
deno/config.ts
Normal file
11
deno/config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
const config = {
|
||||
database: {
|
||||
user: "abc123",
|
||||
password: "abc123",
|
||||
host: "localhost",
|
||||
port: "5432",
|
||||
database: "abc123",
|
||||
},
|
||||
};
|
||||
|
||||
export { config };
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Extensible } from "./interfaces";
|
||||
|
||||
export type ContentType = string
|
||||
export type ContentType = string;
|
||||
|
||||
const contentTypes = {
|
||||
text: {
|
||||
|
||||
7
deno/deps.ts
Normal file
7
deno/deps.ts
Normal file
@@ -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";
|
||||
8
deno/main.ts
Normal file
8
deno/main.ts
Normal file
@@ -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));
|
||||
}
|
||||
6
deno/main_test.ts
Normal file
6
deno/main_test.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { add } from "./main.ts";
|
||||
|
||||
Deno.test(function addTest() {
|
||||
assertEquals(add(2, 3), 5);
|
||||
});
|
||||
101
deno/routes.ts
Normal file
101
deno/routes.ts
Normal file
@@ -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);
|
||||
24
deno/services.ts
Normal file
24
deno/services.ts
Normal file
@@ -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 };
|
||||
Reference in New Issue
Block a user