-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.ts
93 lines (77 loc) · 2.26 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
import { MuseumController } from "../museums/index.ts";
import { Application, Router } from "../deps.ts";
import { UserController } from "../users/types.ts";
interface CreateServerDependencies {
configuration: {
port: number;
};
museum: MuseumController;
user: UserController;
}
export async function createServer({
configuration: {
port,
},
museum,
user,
}: CreateServerDependencies) {
const app = new Application();
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.headers.get("X-Response-Time");
console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`);
});
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.response.headers.set("X-Response-Time", `${ms}ms`);
});
app.addEventListener("listen", (e) => {
console.log(
`Application running at http://${e.hostname || "localhost"}:${port}`,
);
});
app.addEventListener("error", (e) => {
console.log("An error occurred", e.message);
});
const apiRouter = new Router({ prefix: "/api" });
apiRouter.get("/museums", async (ctx) => {
ctx.response.body = {
museums: await museum.getAll(),
};
});
apiRouter.post("/users/register", async (ctx) => {
const { username, password } = await ctx.request.body({ type: "json" })
.value;
if (!username || !password) {
ctx.response.status = 400;
return;
}
try {
const createdUser = await user.register({ username, password });
ctx.response.status = 201;
ctx.response.body = { user: createdUser };
} catch (e) {
ctx.response.status = 400;
ctx.response.body = { message: e.message };
}
});
apiRouter.post("/login", async (ctx) => {
const { username, password } = await ctx.request.body().value;
try {
const { user: loginUser } = await user.login({ username, password });
ctx.response.body = { user: loginUser };
ctx.response.status = 201;
} catch (e) {
ctx.response.body = { message: e.message };
ctx.response.status = 400;
}
});
app.use(apiRouter.routes());
app.use(apiRouter.allowedMethods());
app.use((ctx) => {
ctx.response.body = "Hello World!";
});
await app.listen({ port });
}