-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
40 lines (33 loc) · 1.07 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
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
import { setupAuth, validateToken } from "./src/auth.ts";
import { setupDeploy } from "./src/deploy.ts";
const router = new Router();
setupAuth(router)
.get("/", context => {
context.response.body = "Hello Deno PaaS!";
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
// authorization middleware
app.use(async (ctx, next) => {
const authorization = ctx.request.headers.get("Authorization");
const token = authorization.replace("Bearer ", "");
const tokenValid = await validateToken(token);
if (tokenValid) {
await next();
return;
}
ctx.response.body = JSON.stringify({ error: "Not authorized" });
});
// protected routes
const protectedRouter = new Router();
protectedRouter.get("/protected", context => {
context.response.body = "Hello protected route!";
});
// add deploy routes
setupDeploy(protectedRouter);
// attach router to the app
app.use(protectedRouter.routes());
app.use(protectedRouter.allowedMethods());
app.listen({ port: 8000 });