From 69f4fecf5ade0ff79ee75eeada1f88b2fce8dd12 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 12 Jun 2023 10:42:17 +0200 Subject: [PATCH] feat(server): make startup message easier to see (#1273) --- docs/getting-started/running-locally.md | 6 ++++-- src/server/mod.ts | 17 ++++++++++++++++- tests/test_utils.ts | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/getting-started/running-locally.md b/docs/getting-started/running-locally.md index 667b4b18..a8646393 100644 --- a/docs/getting-started/running-locally.md +++ b/docs/getting-started/running-locally.md @@ -11,7 +11,8 @@ automatically read from `.env`. ``` $ deno task start Watcher Process started. -Listening on http://localhost:8000 + 🍋 Fresh ready + Local: http://localhost:8000 ``` If you want to start manually without Deno task, `deno run` the `main.ts` with @@ -52,7 +53,8 @@ Combining all of this we get the following `deno run` command: ``` $ deno run --allow-net --allow-read --allow-env --allow-run --watch=static/,routes/ main.ts Watcher Process started. -Listening on http://localhost:8000 + 🍋 Fresh ready + Local: http://localhost:8000 ``` If you now visit http://localhost:8000, you can see the running project. Try diff --git a/src/server/mod.ts b/src/server/mod.ts index 4897ec3b..65f437df 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -1,4 +1,5 @@ import { ServerContext } from "./context.ts"; +import * as colors from "https://deno.land/std@0.190.0/fmt/colors.ts"; import { serve } from "./deps.ts"; export { Status } from "./deps.ts"; import { @@ -71,9 +72,23 @@ export async function createHandler( export async function start(routes: Manifest, opts: StartOptions = {}) { const ctx = await ServerContext.fromManifest(routes, opts); opts.port ??= parseInt(Deno.env.get("PORT") || "8000"); + + if (!opts.onListen) { + opts.onListen = (params) => { + console.log( + `\n%c 🍋 Fresh ready %c`, + "background-color: #86efac; color: black; font-weight: bold", + "", + ); + const address = colors.cyan(`http://localhost:${params.port}/`); + const localLabel = colors.bold("Local:"); + console.log(` ${localLabel} ${address}\n`); + }; + } + if (opts.experimentalDenoServe === true) { // @ts-ignore as `Deno.serve` is still unstable. - await Deno.serve(ctx.handler() as Deno.ServeHandler, opts); + await Deno.serve({ ...opts, handler: ctx.handler() }); } else { await serve(ctx.handler(), opts); } diff --git a/tests/test_utils.ts b/tests/test_utils.ts index 6e638de1..ac0bc8f8 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -17,7 +17,7 @@ export async function startFreshServer(options: Deno.CommandOptions) { let started = false; for await (const line of lines) { - if (line.includes("Listening on http://")) { + if (line.includes("Fresh ready")) { started = true; break; }