-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
SauceArgente
committed
Feb 27, 2021
0 parents
commit 635e391
Showing
6 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"deno.enable": true, | ||
"deno.lint": true, | ||
"deno.unstable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |