-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
50 lines (46 loc) · 1.5 KB
/
mod.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
export * from "./functions/mod.ts";
export * from "./middlewares/mod.ts";
import {
Context,
createHandler,
type HandlerOptions,
type Middleware,
} from "./middlewares/deps.ts";
import { fallBack, logger, type LoggerOptions } from "./middlewares/mod.ts";
import { resolveMainModule } from "./functions/path.ts";
export type DefaultHandlerOptions =
// deno-lint-ignore no-explicit-any
& { handlerOptions?: HandlerOptions<Record<string, any>> }
& { loggerOptions?: LoggerOptions }
& { hostname: string };
/**
* Starts a default HTTP server and handles incoming requests using
* middlewares using `composium`.
*
* @param {Middleware<Context>} tryMiddleware - The middleware to be applied to incoming requests.
* @param {Options} [options={}] - Optional configuration options for the server.
* @example
* ```ts
* import { serveStatic } from "./middlewares/mod.ts";
* createDefaultHandler(serveStatic("./examples/static/"), { hostname: "zaurbik.de" });
* ```
*/
export function createDefaultHandler(
tryMiddleware: Middleware<Context>,
options: DefaultHandlerOptions,
) {
const pid = {
path: resolveMainModule("./.pid"),
name: options.hostname,
append: true,
...options.handlerOptions?.pid,
};
const handlerOptions = { ...options.handlerOptions, pid };
const loggerOptions = { debug: true, ...options.loggerOptions };
const handler = createHandler(Context, handlerOptions)(tryMiddleware)(
fallBack,
)(
logger(options.hostname, loggerOptions),
);
return handler;
}