-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
186 lines (163 loc) · 9.14 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
let $:any;
import { serve } from "https://deno.land/std@0.50.0/http/server.ts";
export default $ = async(val:any) => {
let __s:any;
if(val.port == undefined){
__s = 8000
console.log('\x1b[32m%s\x1b[0m','༼ つ ◕_◕ ༽つ Server running on https://localhost:8000')
}else{
__s = val.port;
console.log('\x1b[32m%s\x1b[0m','༼ つ ◕_◕ ༽つ Server running on https://localhost:'+val.port)
}
let __s__:any = serve({ port: __s})
for await (const req of __s__) {
let __headers__ = req.headers
let __headers_obj__:any = {}
for await(const i of __headers__){
__headers_obj__[i[0]] = i[1]
}
//console.log(__headers_obj__)
let len:any = Object.keys(val.rest).length;
if(len != 0){
let __index:number = -1;
let __flag = 0;
let __dump_arr:any = []
Object.keys(val.rest).map(async(item)=>{
__index++;
if(item == req.url){
__dump_arr.push(item)
__flag = 1;
let __route:any = Object.values(val.rest)[__index];
if(__route.method != undefined){
if(__route.method == req.method){
if(req.method == 'GET')
{
let __func = await __route.code
let __ans = __func(JSON.parse(JSON.stringify({"headers":__headers_obj__,"body":{},"params":{}})));
req.respond(await __ans);
}
else{
let __str:any = new TextDecoder("utf-8").decode(req.r.buf);
let __json_data__:any;
if(__str.indexOf('[') == -1){
__json_data__ = __str.slice(__str.indexOf('{'),__str.lastIndexOf('}')+1)
}else if(__str.indexOf('{') == -1){
__json_data__ = __str.slice(__str.indexOf('['),__str.lastIndexOf(']')+1)
}
else if(__str.indexOf('{') < __str.indexOf('[')){
__json_data__ = __str.slice(__str.indexOf('{'),__str.lastIndexOf('}')+1)
}else{
__json_data__ = __str.slice(__str.indexOf('['),__str.lastIndexOf(']')+1)
}
try{
let __json_parse__:any = JSON.parse(__json_data__)
if(__json_parse__.length > 0){
let __func = await __route.code
let __ans = __func(JSON.parse(JSON.stringify({"headers":__headers_obj__,"body":__json_parse__,"params":{}})));
await req.respond(await __ans);
}else if(__json_parse__.length == undefined){
let __func = await __route.code
let __ans = __func(JSON.parse(JSON.stringify({"headers":__headers_obj__,"body":__json_parse__,"params":{}})));
await req.respond(await __ans);
}
}catch(e){
console.log('\x1b[31m%s\x1b[0m','༼ つ ◕_◕ ༽つ check your json')
}finally{
}
}
}
}
}
})
if(__dump_arr.length == 0)
{
let __index:number = -1;
Object.keys(val.rest).map(async(item)=>{
__index++;
if (~item.indexOf("/:")){
let __url_attributes = req.url.split('/').filter((item:any)=>item!='')
let __item_attributes = item.split('/:').filter((item:any)=>item!='')
let __params__:any = {}
if(__url_attributes.length == __item_attributes.length){
for(let __i:number = 0;__i<__item_attributes.length;__i++){
__params__[__item_attributes[__i]] = __url_attributes[__i];
}
}
let __route:any = Object.values(val.rest)[__index];
if(__route.method != undefined){
if(__route.method == req.method){
if(req.method == 'GET'){
let __func = await __route.code
// let __ans = __func(__params__);
let __ans = __func(JSON.parse(JSON.stringify({"headers":__headers_obj__,"body":{},"params":__params__})));
req.respond(await __ans);
}else{
let __str:any = new TextDecoder("utf-8").decode(req.r.buf);
let __json_data__:any;
if(__str.indexOf('[') == -1){
__json_data__ = __str.slice(__str.indexOf('{'),__str.lastIndexOf('}')+1)
}else if(__str.indexOf('{') == -1){
__json_data__ = __str.slice(__str.indexOf('['),__str.lastIndexOf(']')+1)
}
else if(__str.indexOf('{') < __str.indexOf('[')){
__json_data__ = __str.slice(__str.indexOf('{'),__str.lastIndexOf('}')+1)
}else{
__json_data__ = __str.slice(__str.indexOf('['),__str.lastIndexOf(']')+1)
}
try{
let __json_parse__:any = JSON.parse(__json_data__)
if(__json_parse__.length > 0){
let __func = await __route.code
let __ans = __func(JSON.parse(JSON.stringify({"headers":__headers_obj__,"body":__json_parse__,"params":__params__})));
req.respond(await __ans);
}else if(__json_parse__.length == undefined){
let __func = await __route.code
let __ans = __func(JSON.parse(JSON.stringify({"headers":__headers_obj__,"body":__json_parse__,"params":__params__})));
req.respond(await __ans);
}
}catch(e){
console.log('\x1b[31m%s\x1b[0m','༼ つ ◕_◕ ༽つ check your json')
}finally{
}
}
}
}
}
})
}
}
}
}
// import $ from './index.ts'
// $({
// port:8001,
// rest:{
// '/hello':{
// method:'GET',
// code:()=>{
// return {body:'Hello World'}
// }
// },
// '/mahesh/one/two':{
// method:'GET',
// code:()=>{
// return {body:'Mahesh one two'}
// }
// },
// '/mahesh/:id/:name':{
// method:'GET',
// code:(params:any)=>{
// return {
// body:'Id '+params.id+'\n'+ 'Name '+params.name
// }
// }
// }
// }
// })
export let render = async(val:any) =>{
const client = await new TextDecoder("utf-8")
.decode(
await Deno.readFile(val)
);
return client;
}