Cleanup logging and configuration
This commit is contained in:
parent
c771628263
commit
f34ead505b
7 changed files with 103 additions and 105 deletions
|
@ -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 (
|
||||
<>
|
||||
<a
|
||||
tabIndex={12}
|
||||
href="/register"
|
||||
class={`${NAV_ITEM_CLASSES} ml-auto`}
|
||||
>
|
||||
Register
|
||||
</a>
|
||||
<a tabIndex={13} href="/login" class={NAV_ITEM_CLASSES}>Login</a>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function UserNavItems() {
|
||||
return (
|
||||
<>
|
||||
<a
|
||||
tabIndex={12}
|
||||
href="/profile"
|
||||
class={`${NAV_ITEM_CLASSES} ml-auto`}
|
||||
>
|
||||
Profile
|
||||
</a>
|
||||
<a tabIndex={13} href="/dashboard" class={NAV_ITEM_CLASSES}>Dashboard</a>
|
||||
<a tabIndex={13} href="/logout" class={NAV_ITEM_CLASSES}>Logout</a>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export interface PageProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
||||
user?: PublicUser;
|
||||
}
|
||||
|
||||
export function Page(props: PageProps) {
|
||||
console.log("PageProps:", props);
|
||||
return (
|
||||
<div class="relative min-h-screen flex flex-col">
|
||||
<header class="flex justify-start items-center">
|
||||
<nav class={`flex w-full drop-shadow-md ${HEADER_CLASSES}`}>
|
||||
<a
|
||||
tabIndex={10}
|
||||
href="/"
|
||||
class={`${NAV_ITEM_CLASSES} text-black dark:text-white`}
|
||||
>
|
||||
<h1 class="text-2xl">LyricScreen</h1>
|
||||
</a>
|
||||
<a tabIndex={11} href="/note" class={NAV_ITEM_CLASSES}>Notes</a>
|
||||
{props.user ? UserNavItems() : LoginNavItems()}
|
||||
</nav>
|
||||
</header>
|
||||
<main class="p-2">
|
||||
{props.children}
|
||||
</main>
|
||||
<footer class={`p-2 w-full mt-auto ${HEADER_CLASSES}`}>
|
||||
"It's a bit much, really..."
|
||||
</footer>
|
||||
</div>
|
||||
);
|
||||
}
|
33
config.ts
33
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: {},
|
||||
};
|
||||
|
|
5
log.ts
5
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}`,
|
||||
}),
|
||||
},
|
||||
|
|
|
@ -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<unknown, ContextState> = {
|
||||
export const handler: Handlers<MyAppProps, ContextState> = {
|
||||
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 (
|
||||
<>
|
||||
<a
|
||||
tabIndex={12}
|
||||
href="/register"
|
||||
class={`${NAV_ITEM_CLASSES} ml-auto`}
|
||||
>
|
||||
Register
|
||||
</a>
|
||||
<a tabIndex={13} href="/login" class={NAV_ITEM_CLASSES}>Login</a>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function UserNavItems() {
|
||||
return (
|
||||
<>
|
||||
<a
|
||||
tabIndex={12}
|
||||
href="/profile"
|
||||
class={`${NAV_ITEM_CLASSES} ml-auto`}
|
||||
>
|
||||
Profile
|
||||
</a>
|
||||
<a tabIndex={13} href="/dashboard" class={NAV_ITEM_CLASSES}>Dashboard</a>
|
||||
<a tabIndex={13} href="/logout" class={NAV_ITEM_CLASSES}>Logout</a>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App({ Component, ...props }: MyAppProps) {
|
||||
console.log("AppProps:", props);
|
||||
return (
|
||||
<Page user={props.user}>
|
||||
<Component></Component>
|
||||
</Page>
|
||||
<div class="relative min-h-screen flex flex-col">
|
||||
<header class="flex justify-start items-center">
|
||||
<nav class={`flex w-full drop-shadow-md ${HEADER_CLASSES}`}>
|
||||
<a
|
||||
tabIndex={10}
|
||||
href="/"
|
||||
class={`${NAV_ITEM_CLASSES} text-black dark:text-white`}
|
||||
>
|
||||
<h1 class="text-2xl">LyricScreen</h1>
|
||||
</a>
|
||||
<a tabIndex={11} href="/note" class={NAV_ITEM_CLASSES}>Notes</a>
|
||||
{props.user ? UserNavItems() : LoginNavItems()}
|
||||
</nav>
|
||||
</header>
|
||||
<main class="p-2">
|
||||
<Component></Component>
|
||||
</main>
|
||||
<footer class={`p-2 w-full mt-auto ${HEADER_CLASSES}`}>
|
||||
"It's a bit much, really..."
|
||||
</footer>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<unknown, ContextState> = {
|
||||
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 (
|
||||
<Page>
|
||||
<p>
|
||||
You are <pre>{JSON.stringify(data)}</pre>.
|
||||
</p>
|
||||
</Page>
|
||||
<p>
|
||||
You are <pre>{JSON.stringify(data)}</pre>.
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
function LoginRequired() {
|
||||
return (
|
||||
<Page>
|
||||
<a href="/login">You need to login first!</a>
|
||||
</Page>
|
||||
);
|
||||
return <a href="/login">You need to login first!</a>;
|
||||
}
|
||||
|
|
|
@ -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<unknown> = {
|
||||
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<unknown> = {
|
|||
|
||||
export default function LoggedOut() {
|
||||
return (
|
||||
<Page>
|
||||
<p>
|
||||
If you were logged in before, we've logged you out.
|
||||
</p>
|
||||
</Page>
|
||||
<p>
|
||||
If you were logged in before, we've logged you out.
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue