From 635e3916d06e326e3306b1c4817901398ad030db Mon Sep 17 00:00:00 2001 From: SauceArgente Date: Sat, 27 Feb 2021 16:52:04 +0100 Subject: [PATCH] first commit --- .vscode/settings.json | 5 ++++ index.ts | 19 +++++++++++++++ lib-test/index.ts | 38 ++++++++++++++++++++++++++++++ lib-test/methods/request.ts | 42 ++++++++++++++++++++++++++++++++++ lib-test/methods/serveRoute.ts | 39 +++++++++++++++++++++++++++++++ lib-test/methods/structures.ts | 15 ++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 index.ts create mode 100644 lib-test/index.ts create mode 100644 lib-test/methods/request.ts create mode 100644 lib-test/methods/serveRoute.ts create mode 100644 lib-test/methods/structures.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2c7ddc5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "deno.enable": true, + "deno.lint": true, + "deno.unstable": true +} \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..c5461e0 --- /dev/null +++ b/index.ts @@ -0,0 +1,19 @@ +import {app} from "./lib-test/index.ts"; +import { serve } from "https://deno.land/std@0.88.0/http/server.ts"; + +const server = serve({ port: 8000 }); +const chic = new app(server); + +chic.get("/", (req:any) => { + req.write("/") + req.send(); +}) + + +chic.get("/users/:userID", (req:any) => { + req.json({ + type:"Success", + message:`${req.params().userID}` + }) + req.send(); +}) \ No newline at end of file diff --git a/lib-test/index.ts b/lib-test/index.ts new file mode 100644 index 0000000..33b0387 --- /dev/null +++ b/lib-test/index.ts @@ -0,0 +1,38 @@ +// CHIC-SPRESS : Server library for deno + +import serveRoute from "./methods/serveRoute.ts"; + +var routes: { [id: string]: Array } = { + get: [], +}; + +class serverClass { + server: any; + + constructor(server: any) { + this.server = server; + this.run(); + } + + get(path: string, callback: Function): void { + routes.get.push({ + path: path, + callback: callback, + }); + } + + async run() { + for await (const req of this.server) { + const method: string = req.method; + const url: string = req.url; + + switch (method) { + case "GET": + serveRoute(req,"GET",url,routes); + break; + } + } + } +} + +export const app = serverClass; diff --git a/lib-test/methods/request.ts b/lib-test/methods/request.ts new file mode 100644 index 0000000..99fdd43 --- /dev/null +++ b/lib-test/methods/request.ts @@ -0,0 +1,42 @@ +import { objValAny, objValStrNum } from "./structures.ts"; + +const defaultData = { + status: 200, + body: "", +}; + +export default class request { + req: any; + parameters: objValStrNum; + data: objValAny; + + constructor(req: any, params: objValStrNum) { + this.req = req; + this.parameters = params; + this.data = defaultData; + } + + params() { + return this.parameters; + } + + body() { + return this.req.body; + } + + status(status: number) { + this.data.status = status; + } + + write(data: string) { + this.data.body += data; + } + + json(data:Record){ + this.data.body = JSON.stringify(data); + } + + send() { + this.req.respond(this.data); + } +} diff --git a/lib-test/methods/serveRoute.ts b/lib-test/methods/serveRoute.ts new file mode 100644 index 0000000..f51a040 --- /dev/null +++ b/lib-test/methods/serveRoute.ts @@ -0,0 +1,39 @@ +import Request from "./request.ts"; + +export default (req:any,method:string,url:string,routes: { [id: string]: Array })=>{ + routes[method.toLocaleLowerCase()].forEach((route) => { + var sectionsRoute = route.path.split("/"); + var sectionsURL = url.split("/"); + + if (sectionsURL.length == sectionsRoute.length) { + // If has same number of slashes + var params : {[id:string] : string | number} = {}; + var matching: number = 0; + for (let i = 0; i < sectionsRoute.length; i++) { + var sectionR : string = sectionsRoute[i]; + var sectionU : string = sectionsURL[i]; + + if(sectionR[0] == ":"){ + matching += 1; + params[sectionR.substring(1)] = sectionU; + }else{ + if(sectionR == sectionU){ + matching += 1; + }else{ + return; + } + } + } + + if(matching == sectionsURL.length){ + var request = new Request(req,params); + route.callback(request); + }else{ + return; + } + }else{ + return; + } + }); + +} diff --git a/lib-test/methods/structures.ts b/lib-test/methods/structures.ts new file mode 100644 index 0000000..a45f512 --- /dev/null +++ b/lib-test/methods/structures.ts @@ -0,0 +1,15 @@ +export interface objValArr { + [id: string]: Array; +} + +export interface objValAny { + [id: string]: any; +} + +export interface objValStr { + [id: string]: string; +} + +export interface objValStrNum { + [id: string]: string | number; +}