-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
62 lines (47 loc) · 1.93 KB
/
app.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
import { Application } from "https://deno.land/x/oak/mod.ts";
import * as flags from 'https://deno.land/std/flags/mod.ts';
import {viewEngine, engineFactory, adapterFactory } from "https://deno.land/x/view_engine/mod.ts";
import { green, yellow } from "https://deno.land/std/fmt/colors.ts";
import { router } from "./router/router.ts"
import { client } from "./configs/mysql.ts";
import errorMiddleware from "./middlewares/error.ts";
const {args, exit} = Deno;
const DEFAULT_PORT = 5005;
const argPort = flags.parse(args).port;
const port = argPort ? Number(argPort) : DEFAULT_PORT;
if (isNaN(port)){
console.log("Bu bağlantı noktası numarası değil!");
exit(1);
};
const app = new Application();
const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();
app.use(viewEngine(oakAdapter, ejsEngine, {
viewRoot: "./views",
viewExt: ".ejs"
}));
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();
ctx.response.headers.set("X-Response-Time", `${ms - start}ms`);
});
app.use(errorMiddleware)
app.use(router.routes());
app.use(router.allowedMethods());
app.addEventListener("listen", ({ secure, hostname, port }) => {
const protocol = secure ? "https://" : "http://";
const url = `${protocol}${hostname ?? "localhost"}:${port}`;
console.log(`${green("Uygulama başladı.")} ${yellow(url)} portu dinleniyor!`);
});
await app.listen({ port: port });
await client.close();
// Deno, biz istemediğimiz sürece dosya okuma ve internete erişme izni vermez.
// deno run --allow-env --allow-net --allow-read --allow-write --allow-plugin --unstable app.ts
// Live Reload
// denon run --allow-env --allow-net --allow-read --allow-write --allow-plugin --unstable app.ts