Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SauceArgente committed Feb 27, 2021
0 parents commit 635e391
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}
19 changes: 19 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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();
})
38 changes: 38 additions & 0 deletions lib-test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// CHIC-SPRESS : Server library for deno

import serveRoute from "./methods/serveRoute.ts";

var routes: { [id: string]: Array<any> } = {
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;
42 changes: 42 additions & 0 deletions lib-test/methods/request.ts
Original file line number Diff line number Diff line change
@@ -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<string,any>){
this.data.body = JSON.stringify(data);
}

send() {
this.req.respond(this.data);
}
}
39 changes: 39 additions & 0 deletions lib-test/methods/serveRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Request from "./request.ts";

export default (req:any,method:string,url:string,routes: { [id: string]: Array<any> })=>{
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;
}
});

}
15 changes: 15 additions & 0 deletions lib-test/methods/structures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface objValArr {
[id: string]: Array<any>;
}

export interface objValAny {
[id: string]: any;
}

export interface objValStr {
[id: string]: string;
}

export interface objValStrNum {
[id: string]: string | number;
}

0 comments on commit 635e391

Please sign in to comment.