This commit is contained in:
Daniel Flanagan 2022-10-11 12:20:25 -05:00
parent ac44a249ed
commit 56378fbc26
Signed by untrusted user: lytedev-divvy
GPG Key ID: 6D69CEEE4ABBCD82
8 changed files with 53 additions and 14 deletions

View File

@ -1,3 +1,5 @@
import "$std/log/mod.ts";
export interface Config {
postgres: {
url: string;

View File

@ -10,6 +10,7 @@ import {
} from "https://deno.land/x/postgres@v0.16.1/query/query.ts?s=QueryArguments";
import { config } from "@/config.ts";
import * as base64 from "$std/encoding/base64.ts";
import { log } from "@/log.ts";
import {
type Identifiable,
@ -36,14 +37,14 @@ async function dbOp<T>(op: (connection: PoolClient) => Promise<T>) {
try {
result = await op(connection);
} catch (err) {
console.error("Error querying database:", { ...err });
log.error("Error querying database:", { ...err });
exception = err;
} finally {
connection.release();
}
} catch (err) {
exception = err;
console.error("Error connecting to database:", err);
log.error("Error connecting to database:", err);
}
if (exception != null) throw exception;
return result;
@ -59,7 +60,7 @@ export async function queryObject<T>(
text: sql.trim(),
args,
});
console.debug(result);
log.debug(result);
return result;
});
}
@ -88,7 +89,7 @@ export async function getNote(
id: string | { id: string },
): Promise<Note | null> {
const idVal = typeof id == "object" ? id.id : id;
console.debug("getNote id =", JSON.stringify(idVal));
log.debug("getNote id =", JSON.stringify(idVal));
return singleRow(
await queryObject<Note>(
"select * from note where id = $1",
@ -228,7 +229,7 @@ export async function getTeam(
}
function someRows<T>(result: { rows: T[] } | null): T[] | null {
console.debug(result);
log.debug(result);
if (!result || result.rows.length < 1) return null;
else return result.rows;
}
@ -236,7 +237,7 @@ function someRows<T>(result: { rows: T[] } | null): T[] | null {
function singleRow<T>(result: { rows: T[] } | null): T | null {
if (!result || result.rows.length < 1) return null;
else if (result.rows.length > 1) {
console.error(
log.error(
"This singleRow result brought back more than 1 row:",
result,
);

View File

@ -19,9 +19,10 @@ import * as $12 from "./routes/logout.tsx";
import * as $13 from "./routes/note.tsx";
import * as $14 from "./routes/note/[id].tsx";
import * as $15 from "./routes/note/create.tsx";
import * as $16 from "./routes/register.tsx";
import * as $17 from "./routes/route-config-example.tsx";
import * as $18 from "./routes/search.tsx";
import * as $16 from "./routes/plain.ts";
import * as $17 from "./routes/register.tsx";
import * as $18 from "./routes/route-config-example.tsx";
import * as $19 from "./routes/search.tsx";
import * as $$0 from "./islands/Countdown.tsx";
import * as $$1 from "./islands/Counter.tsx";
@ -43,9 +44,10 @@ const manifest = {
"./routes/note.tsx": $13,
"./routes/note/[id].tsx": $14,
"./routes/note/create.tsx": $15,
"./routes/register.tsx": $16,
"./routes/route-config-example.tsx": $17,
"./routes/search.tsx": $18,
"./routes/plain.ts": $16,
"./routes/register.tsx": $17,
"./routes/route-config-example.tsx": $18,
"./routes/search.tsx": $19,
},
islands: {
"./islands/Countdown.tsx": $$0,

View File

@ -1,7 +1,7 @@
{
"imports": {
"@/": "./",
"$std/": "https://deno.land/std@0.144.0/",
"$std/": "https://deno.land/std@0.158.0/",
"$fresh/": "https://deno.land/x/fresh@1.1.1/",
"preact": "https://esm.sh/preact@10.11.0",
"preact/": "https://esm.sh/preact@10.11.0/",

24
log.ts Normal file
View File

@ -0,0 +1,24 @@
export * as log from "$std/log/mod.ts";
import * as log from "$std/log/mod.ts";
const levelColors = {};
export function setupLoggers() {
log.setup({
handlers: {
console: new log.handlers.ConsoleHandler("DEBUG", {
formatter: `{datetime} {levelName} {msg}`,
}),
},
loggers: {
default: {
level: "DEBUG",
handlers: ["console", "file"],
},
tasks: {
level: "ERROR",
handlers: ["console"],
},
},
});
}

View File

@ -1,5 +1,6 @@
import Mailgun from "https://deno.land/x/mailgun@v1.1.1/index.ts";
import { type Message } from "https://deno.land/x/mailgun@v1.1.1/types.ts";
import * as log from "$std/log/mod.ts";
export type Email = Message;
@ -7,7 +8,7 @@ const MAILGUN_API_KEY = Deno.env.get("MAILGUN_API_KEY");
const MAILGUN_DOMAIN = Deno.env.get("MAILGUN_DOMAIN");
if (!MAILGUN_API_KEY) {
console.error(
log.error(
"MAILGUN_API_KEY not set. Emails will not be sent, only logged to the console.",
);
}

View File

@ -10,4 +10,8 @@ import manifest from "@/fresh.gen.ts";
import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "@/twind.config.ts";
import { setupLoggers } from "@/log.ts";
setupLoggers();
await start(manifest, { plugins: [twindPlugin(twindConfig)] });

5
routes/plain.ts Normal file
View File

@ -0,0 +1,5 @@
import { HandlerContext } from "$fresh/server.ts";
export function handler(_req: Request, _context: HandlerContext) {
return new Response("yo", { status: 200 });
}