-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
152 lines (130 loc) · 2.96 KB
/
main.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
import {
Pool,
serve,
verifyJwt,
} from './deps.ts';
const JWT_CONFIG = {
secret: Deno.env.get("API_JWT_SECRET") || '',
algo: Deno.env.get("API_JWT_ALGORITHM") || 'none',
};
const DB_CONFIG = {
user: Deno.env.get("DATABASE_USERNAME"),
password: Deno.env.get("DATABASE_PASSWORD"),
database: Deno.env.get("DATABASE_NAME"),
hostname: Deno.env.get("DATABASE_HOST"),
port: Deno.env.get("DATABASE_PORT"),
};
const POOL_CONNECTIONS = 5;
const pool = new Pool(
DB_CONFIG,
POOL_CONNECTIONS,
);
async function runQuery<T extends Record<string, unknown>>(text: string, args: unknown[] = []) {
const client = await pool.connect();
const result = await client.queryObject<T>({
text,
args,
});
client.release();
return result;
}
function queryBy(ids: number[]) {
return async <T extends Record<string, unknown>>(table: string) => {
const { rows } = await runQuery<T>(
`select * from ${table} where resume_id = ANY($1::int[])`,
[
ids,
],
);
return rows;
}
}
async function buildResponse(): Promise<string> {
// console.time('query');
const { rows: resumes } = await runQuery(`
select
u.first_name,
u.last_name,
u.email,
u.phone,
r.*
from
resumes r
left join
users u
on u.id = r.user_id
`);
const ids = resumes.map(({ id }) => id as number);
const idQuery = queryBy(ids);
const [
educations,
workExperiences,
computerSkills,
skills,
languages,
awards,
] = await Promise.all([
idQuery('resume_educations'),
idQuery('resume_work_experiences'),
idQuery('resume_computer_skills'),
idQuery('resume_skills'),
idQuery('resume_languages'),
idQuery('resume_awards'),
]);
// console.timeEnd('query');
// console.time('serialization');
const response = {
resumes: resumes.map((u) => ({
...u,
user_id: String(u.user_id),
})),
educations,
workExperiences,
computerSkills,
skills,
languages,
awards,
};
const payload = JSON.stringify(response);
// console.timeEnd('serialization');
return payload;
}
const server = serve({
port: 8080,
});
const headers = new Headers({
'Content-Type': 'application/json',
'X-Powered-By': 'ligma',
});
const AUTH_JWT_HEADER = 'jwt ';
for await (const request of server) {
const auth = request.headers.get('Authorization');
if (!auth || !auth.startsWith(AUTH_JWT_HEADER)) {
request.respond({
status: 403,
headers,
body: JSON.stringify({
error: "No Authorization header",
}),
});
continue;
}
const authJwt = auth.substr(AUTH_JWT_HEADER.length);
try {
await verifyJwt(authJwt, JWT_CONFIG.secret, JWT_CONFIG.algo as any);
} catch {
request.respond({
status: 403,
headers,
body: JSON.stringify({
error: "Invalid JWT in Authorization header",
}),
});
continue;
}
request.respond({
status: 200,
headers,
body: await buildResponse(),
});
}