Skip to content

Commit

Permalink
Fixes and mod.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsVomMars committed Mar 6, 2021
1 parent 6bfe4de commit 0202d62
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 80 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# YADWF

> **Y**et **a**nother **D**eno **w**eb-**f**ramework
## Initial setup

```ts
import { YADWF } from "https://deno.land/x/[email protected]/mod.ts";

const app = new YADWF();

app.get("/", (ctx) => ctx.text("Hello World!"));

app.start({ port: 1337 });
```
101 changes: 28 additions & 73 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
} from "./deps.ts";
import { getNestedDirectories } from "./util.ts";

export type Handler = (c: Context) => Promise<void> | void;
export type Handler = (ctx: Context) => Promise<void> | void;
export type Middleware = (next: Handler) => Handler;

export class Application {
export class YADWF {
#server?: Server;
#router: Router;

Expand All @@ -25,19 +25,19 @@ export class Application {
private async startServer(server: Server) {
this.#server = server;
for await (const req of this.#server) {
const c = new Context(req);
log.info(`${c.protocol} ${c.method} ${c.path}`);
const ctx = new Context(req);
log.info(`${ctx.protocol} ${ctx.method} ${ctx.path}`);
const { handler, params } = this.#router.find(
HTTPMethods.indexOf(c.method) as Method,
c.path,
HTTPMethods.indexOf(ctx.method) as Method,
ctx.path,
);
c.params = params; // Named parameters
ctx.params = params; // Named parameters
try {
await handler(c);
await handler(ctx);
} catch (e) {
log.error(e);
}
req.respond(c.response);
req.respond(ctx.response);
}
}

Expand Down Expand Up @@ -70,125 +70,80 @@ export class Application {
this.#server?.close();
}

get(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
get(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.GET, handler, ...middlewares);
return this;
}

post(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
post(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.POST, handler, ...middlewares);
return this;
}

put(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
put(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.PUT, handler, ...middlewares);
return this;
}

delete(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
delete(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.DELETE, handler, ...middlewares);
return this;
}

trace(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
trace(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.TRACE, handler, ...middlewares);
return this;
}

options(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
options(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.OPTIONS, handler, ...middlewares);
return this;
}

patch(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
patch(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.PATCH, handler, ...middlewares);
return this;
}

connect(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
connect(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.CONNECT, handler, ...middlewares);
return this;
}

head(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
head(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
this.addPath(path, Method.HEAD, handler, ...middlewares);
return this;
}

any(
path: string,
handler: Handler,
...middlewares: Middleware[]
): Application {
any(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF {
const methods = Object.values(Method).filter((n) => typeof n === "number");
for (const method of methods) {
this.addPath(path, method as Method, handler, ...middlewares);
}
return this;
}

file(path: string, file: string, ...middlewares: Middleware[]): Application {
file(path: string, file: string, ...middlewares: Middleware[]): YADWF {
this.addPath(
path,
Method.GET,
async (c) => await c.file(file),
async (ctx) => await ctx.file(file),
...middlewares,
);
return this;
}

static(
path: string,
directory: string,
...middlewares: Middleware[]
): Application {
static(path: string, directory: string, ...middlewares: Middleware[]): YADWF {
if (!path.endsWith("/")) path += "/";
(async () => {
const directories = await getNestedDirectories(directory);
getNestedDirectories(directory).then((directories) => {
directories.push("");
for (const dir of directories) {
const hdl = async (c: Context) => {
const file = c.path.substring(join(path, dir, sep).length);
const hdl = async (ctx: Context) => {
const file = ctx.path.substring(join(path, dir, sep).length);
if (file.length === 0) {
return await c.file(join(directory, dir, "index.html"));
return await ctx.file(join(directory, dir, "index.html"));
}
return await c.file(join(directory, dir, file));
return await ctx.file(join(directory, dir, file));
};
this.addPath(
`${path + dir}/*`.replace(/\/+/, "/"),
Expand All @@ -197,7 +152,7 @@ export class Application {
...middlewares,
);
}
})();
});
return this;
}
}
Expand Down
5 changes: 1 addition & 4 deletions mimetypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { extname } from "./deps.ts";

export function getContentType(filename: string): string {
const extension = extname(filename);
if (!Object.keys(extensions).includes(extension) || !extension) {
return "application/octet-stream";
}
return extensions[extension];
return extensions[extension] || "application/octet-stream";
}

export const utf8 = "charset=UTF-8";
Expand Down
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./app.ts";
export * from "./context.ts";
6 changes: 3 additions & 3 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class PathHandler {
private readonly method: Method,
private readonly handler: Handler,
) {
if (this.path.startsWith(":")) {
if (this.path && this.path.startsWith(":")) {
this.name = this.path.slice(1);
this.path = "*";
}
Expand Down Expand Up @@ -154,5 +154,5 @@ export interface RouterResult {
params: Record<string, string>;
}

export const NotFoundHandler: Handler = (c: Context) =>
c.text("404 Not found", Status.NotFound);
export const NotFoundHandler: Handler = (ctx: Context) =>
ctx.text("404 Not found", Status.NotFound);

0 comments on commit 0202d62

Please sign in to comment.