From f34ead505b494c307623a5ad615c54af0e56f572 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Tue, 11 Oct 2022 23:49:36 -0500 Subject: [PATCH] Cleanup logging and configuration --- components/Page.tsx | 69 ------------------------------------------- config.ts | 33 ++++++++++++++++++--- log.ts | 5 ++-- routes/_app.tsx | 68 +++++++++++++++++++++++++++++++++++++----- routes/_middleware.ts | 3 +- routes/dashboard.tsx | 19 ++++-------- routes/logout.tsx | 11 +++---- 7 files changed, 103 insertions(+), 105 deletions(-) delete mode 100644 components/Page.tsx diff --git a/components/Page.tsx b/components/Page.tsx deleted file mode 100644 index 8b7dec9..0000000 --- a/components/Page.tsx +++ /dev/null @@ -1,69 +0,0 @@ -import { JSX } from "preact"; -import { type PublicUser } from "@/types.ts"; - -const NAV_ITEM_CLASSES = - "flex justify-center items-center px-4 py-2 hover:bg-gray-300 dark:hover:bg-gray-700"; - -const HEADER_CLASSES = "bg-gray-200 dark:bg-gray-800"; - -export function LoginNavItems() { - return ( - <> - - Register - - Login - - ); -} - -export function UserNavItems() { - return ( - <> - - Profile - - Dashboard - Logout - - ); -} - -export interface PageProps extends JSX.HTMLAttributes { - user?: PublicUser; -} - -export function Page(props: PageProps) { - console.log("PageProps:", props); - return ( -
-
- -
-
- {props.children} -
-
- "It's a bit much, really..." -
-
- ); -} diff --git a/config.ts b/config.ts index fc31995..a2e0028 100644 --- a/config.ts +++ b/config.ts @@ -1,6 +1,12 @@ -import "$std/log/mod.ts"; +import { + type LevelName, + LogLevels, +} from "https://deno.land/std@0.159.0/log/mod.ts"; export interface Config { + log: { + consoleLevelName: LevelName; + }; postgres: { url: string; }; @@ -10,17 +16,36 @@ export interface Config { }; } -function envOrWarn(key: string, fallback?: string): string | undefined { +function envOrWarn(key: string, fallback: string): string { const val = Deno.env.get(key); - if (!val) console.warn(`${key} is not set!`); + if (!val) console.warn(`${key} is not set! Using fallback: ${fallback}`); return val || fallback; } -export const config = { +function isLogLevelName(s: string): s is LevelName { + return s in LogLevels; +} + +const desiredLogLevel = envOrWarn("LOG_LEVEL", "INFO").toUpperCase(); +if (!isLogLevelName(desiredLogLevel)) { + console.warn( + `Desired LOG_LEVEL of '${desiredLogLevel}' is invalid. Falling back to 'INFO'`, + ); +} + +const logLevel: LevelName = isLogLevelName(desiredLogLevel) + ? desiredLogLevel + : "INFO"; + +export const config: Config = { + log: { + consoleLevelName: logLevel, + }, postgres: { url: envOrWarn( "POSTGRES_URL", "postgresql://postgres:@127.0.0.1:5432/lyricscreen", ), }, + mailgun: {}, }; diff --git a/log.ts b/log.ts index 6178e6a..cbc21f8 100644 --- a/log.ts +++ b/log.ts @@ -1,13 +1,12 @@ +import { config } from "@/config.ts"; import * as log from "$std/log/mod.ts"; export * as log from "$std/log/mod.ts"; -const levelColors = {}; - export function setupLoggers() { log.setup({ handlers: { - console: new log.handlers.ConsoleHandler("DEBUG", { + console: new log.handlers.ConsoleHandler(config.log.consoleLevelName, { formatter: `{datetime} {levelName} {msg}`, }), }, diff --git a/routes/_app.tsx b/routes/_app.tsx index 79bf24a..d9efe02 100644 --- a/routes/_app.tsx +++ b/routes/_app.tsx @@ -1,5 +1,4 @@ -import { type AppProps, Handlers, PageProps } from "$fresh/server.ts"; -import { Page } from "@/components/Page.tsx"; +import { type AppProps, Handlers } from "$fresh/server.ts"; import { type PublicUser } from "@/types.ts"; import { type ContextState } from "@/types.ts"; @@ -7,18 +6,73 @@ interface MyAppProps extends AppProps { user?: PublicUser; } -export const handler: Handlers = { +export const handler: Handlers = { async GET(request: Request, context) { + console.error("\n\nYO\n\n"); console.log("AppHandler:", request, context); - return await context.render(context.state.user); + return await context.render({ user: context.state.user }); }, }; +const NAV_ITEM_CLASSES = + "flex justify-center items-center px-4 py-2 hover:bg-gray-300 dark:hover:bg-gray-700"; + +const HEADER_CLASSES = "bg-gray-200 dark:bg-gray-800"; + +export function LoginNavItems() { + return ( + <> + + Register + + Login + + ); +} + +export function UserNavItems() { + return ( + <> + + Profile + + Dashboard + Logout + + ); +} + export default function App({ Component, ...props }: MyAppProps) { console.log("AppProps:", props); return ( - - - +
+
+ +
+
+ +
+
+ "It's a bit much, really..." +
+
); } diff --git a/routes/_middleware.ts b/routes/_middleware.ts index 9108594..aecef08 100644 --- a/routes/_middleware.ts +++ b/routes/_middleware.ts @@ -2,6 +2,7 @@ import { MiddlewareHandlerContext } from "$fresh/server.ts"; import { deleteCookie, getCookies } from "$std/http/cookie.ts"; import { getUserFromNonExpiredLoginToken } from "@/db/mod.ts"; import { type ContextState, type PublicUser, type User } from "@/types.ts"; +import { log } from "@/log.ts"; function toPublicUser(user: User): PublicUser { const { @@ -19,7 +20,7 @@ async function currentUser( ) { let hasBadAuthCookie = false; const { lsauth } = getCookies(request.headers); - console.log("lsauth cookie:", lsauth); + log.debug("lsauth cookie:", lsauth); if (lsauth) { const user = await getUserFromNonExpiredLoginToken(lsauth); if (!user) hasBadAuthCookie = true; diff --git a/routes/dashboard.tsx b/routes/dashboard.tsx index 4d7b68a..dd45ca0 100644 --- a/routes/dashboard.tsx +++ b/routes/dashboard.tsx @@ -1,13 +1,10 @@ -import { Page } from "@/components/Page.tsx"; // import { getToken, getUser } from "@/db/mod.ts"; // import * as base64 from "$std/encoding/base64.ts"; import { Handlers, PageProps } from "$fresh/server.ts"; -import { type ContextState, type PublicUser } from "@/types.ts"; +import { type ContextState } from "@/types.ts"; export const handler: Handlers = { async GET(_request: Request, context) { - const user: PublicUser | undefined = context.state.user; - if (user != undefined) delete user.passwordDigest; return await context.render(context.state.user); }, }; @@ -23,18 +20,12 @@ export default function Dashboard({ data }: PageProps) { function You(data: unknown) { return ( - -

- You are

{JSON.stringify(data)}
. -

-
+

+ You are

{JSON.stringify(data)}
. +

); } function LoginRequired() { - return ( - - You need to login first! - - ); + return You need to login first!; } diff --git a/routes/logout.tsx b/routes/logout.tsx index 0548e14..430fb48 100644 --- a/routes/logout.tsx +++ b/routes/logout.tsx @@ -1,11 +1,10 @@ import { Handlers } from "$fresh/server.ts"; -import { Page } from "@/components/Page.tsx"; // import { getToken, getUser } from "@/db/mod.ts"; // import * as base64 from "$std/encoding/base64.ts"; import { deleteCookie } from "$std/http/cookie.ts"; export const handler: Handlers = { - async GET(request: Request, context) { + async GET(_request: Request, context) { const response = await context.render(); deleteCookie(response.headers, "lsauth"); return response; @@ -14,10 +13,8 @@ export const handler: Handlers = { export default function LoggedOut() { return ( - -

- If you were logged in before, we've logged you out. -

-
+

+ If you were logged in before, we've logged you out. +

); }