Compare commits
1 Commits
503e82c2f4
...
c00260fcfe
| Author | SHA1 | Date | |
|---|---|---|---|
| c00260fcfe |
@@ -1,11 +0,0 @@
|
|||||||
const config = {
|
|
||||||
database: {
|
|
||||||
user: "abc123",
|
|
||||||
password: "abc123",
|
|
||||||
host: "localhost",
|
|
||||||
port: "5432",
|
|
||||||
database: "abc123",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export { config };
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Extensible } from "./interfaces";
|
import { Extensible } from "./interfaces";
|
||||||
|
|
||||||
export type ContentType = string;
|
export type ContentType = string
|
||||||
|
|
||||||
const contentTypes= {
|
const contentTypes= {
|
||||||
text: {
|
text: {
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
// 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";
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
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));
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import { assertEquals } from "@std/assert";
|
|
||||||
import { add } from "./main.ts";
|
|
||||||
|
|
||||||
Deno.test(function addTest() {
|
|
||||||
assertEquals(add(2, 3), 5);
|
|
||||||
});
|
|
||||||
101
deno/routes.ts
101
deno/routes.ts
@@ -1,101 +0,0 @@
|
|||||||
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);
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
// 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