-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Introduce the first basic route and the deno server
- Loading branch information
1 parent
46103b5
commit 19179da
Showing
13 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PORT= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
Empty file.