Skip to content

Commit

Permalink
✨ Introduce the first basic route and the deno server
Browse files Browse the repository at this point in the history
  • Loading branch information
FlavianEng committed Dec 11, 2021
1 parent 46103b5 commit 19179da
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": false,
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno"
}
7 changes: 4 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# 🦕 Deno server boilerplate

Deno server boilerplate based on abc framework

To run the server, open a terminal and run the command :
``` deno run --allow-net server.ts ```
To run the server, open a terminal and run the command :
`deno run --allow-read --allow-net server.ts`

If you want to run the server on a development environment, run the command :
``` deno run --allow-net --watch server.ts ```
`deno run --allow-read --allow-net --watch server.ts`
44 changes: 44 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
Application,
isHttpError,
Router,
Status,
} from "https://deno.land/x/oak/mod.ts";
import router from "./router.ts";

export const app = new Application();

// TASK Find a solution to separate properly the middlewares

// Logger
app.use(async (ctx, next) => {
await next();
const statusCode = await ctx.response.status;
console.log(`${statusCode} ${ctx.request.method} ${ctx.request.url}`);
});

// TASK Finish the setup of the error handler
// Error handler
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
if (isHttpError(err)) {
switch (err.status) {
case Status.NotFound:
// handle NotFound
break;
default:
// handle other statuses
}
} else {
// rethrow if you can't handle the error
throw err;
}
}
});

const globalRouter = new Router();
globalRouter.use("/api", router.routes());

app.use(globalRouter.routes());
8 changes: 8 additions & 0 deletions router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from "https://deno.land/x/oak/mod.ts";
import pingRouter from "./src/routes/ping.route.ts";

const router = new Router();

router.use("/ping", pingRouter.routes(), pingRouter.allowedMethods());

export default router;
15 changes: 15 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { config as env } from "https://deno.land/x/dotenv/mod.ts";
import { app } from "./app.ts";

const port = parseInt(env().PORT) || 8000;

app.addEventListener("listen", ({ hostname, port, secure: protocol }) => {
console.log(
`⚡ Listening on: ${protocol ? "https://" : "http://"}${
hostname ??
"localhost"
}:${port} ⚡`,
);
});

await app.listen({ port: port });
Empty file added src/controllers/.gitkeep
Empty file.
Empty file added src/repositories/.gitkeep
Empty file.
13 changes: 13 additions & 0 deletions src/routes/ping.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Router } from "https://deno.land/x/oak/mod.ts";

const router = new Router();

// TASK Add a controller and a service for this
// TASK Add some test
router.all("/", ({ response }) => {
response.status = 418;
response.headers.set("Content-Type", "application/json");
response.body = { message: "🎾 Pong" };
});

export default router;
Empty file added src/services/.gitkeep
Empty file.
Empty file added src/tests/.gitkeep
Empty file.
Empty file added src/utils/.gitkeep
Empty file.

0 comments on commit 19179da

Please sign in to comment.