feat: move perf,logs and user into sqlite
This commit is contained in:
@ -1,14 +1,14 @@
|
||||
//routes/middleware-error-handler/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { FreshContext } from "$fresh/server.ts";
|
||||
import { DomainError } from "@lib/errors.ts";
|
||||
import { getCookies } from "@std/http/cookie";
|
||||
import { verify } from "https://deno.land/x/djwt@v2.2/mod.ts";
|
||||
import * as cache from "@lib/cache/performance.ts";
|
||||
import * as perf from "@lib/performance.ts";
|
||||
import { JWT_SECRET } from "@lib/env.ts";
|
||||
|
||||
export async function handler(
|
||||
req: Request,
|
||||
ctx: MiddlewareHandlerContext,
|
||||
ctx: FreshContext,
|
||||
) {
|
||||
try {
|
||||
performance.mark("a");
|
||||
@ -29,7 +29,7 @@ export async function handler(
|
||||
const resp = await ctx.next();
|
||||
performance.mark("b");
|
||||
const b = performance.measure("a->b", "a", "b");
|
||||
cache.savePerformance(req.url, b.duration);
|
||||
perf.savePerformance(req.url, b.duration);
|
||||
return resp;
|
||||
} catch (error) {
|
||||
console.error("Error", error);
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { MainLayout } from "@components/layouts/main.tsx";
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { AccessDeniedError } from "@lib/errors.ts";
|
||||
import { getLogs, Log } from "@lib/cache/logs.ts";
|
||||
import { getLogs, Log } from "@lib/logs.ts";
|
||||
import { formatDate } from "@lib/string.ts";
|
||||
import { renderMarkdown } from "@lib/documents.ts";
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { MainLayout } from "@components/layouts/main.tsx";
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { getPerformances, PerformanceRes } from "@lib/cache/performance.ts";
|
||||
import { getPerformances, PerformanceRes } from "@lib/performance.ts";
|
||||
import { AccessDeniedError } from "@lib/errors.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
|
@ -4,9 +4,11 @@ import { oauth2Client } from "@lib/auth.ts";
|
||||
import { getCookies, setCookie } from "@std/http/cookie";
|
||||
import { codeChallengeMap } from "./login.ts";
|
||||
import { GITEA_SERVER, JWT_SECRET, SESSION_DURATION } from "@lib/env.ts";
|
||||
import { userDB } from "@lib/db.ts";
|
||||
import { GiteaOauthUser } from "@lib/types.ts";
|
||||
import { BadRequestError } from "@lib/errors.ts";
|
||||
import { db } from "@lib/sqlite/sqlite.ts";
|
||||
import { userTable } from "@lib/sqlite/schema.ts";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(request) {
|
||||
@ -38,15 +40,17 @@ export const handler: Handlers = {
|
||||
|
||||
const oauthUser = await userResponse.json() as GiteaOauthUser;
|
||||
|
||||
const allUsers = await userDB.findAll();
|
||||
let user = allUsers.find((u) => u.name === oauthUser.name);
|
||||
let user = await db.select().from(userTable).where(
|
||||
eq(userTable.name, oauthUser.name),
|
||||
).limit(1).then((users) => users[0]);
|
||||
|
||||
if (!user) {
|
||||
user = await userDB.create({
|
||||
createdAt: new Date(),
|
||||
const res = await db.insert(userTable).values({
|
||||
id: crypto.randomUUID(),
|
||||
email: oauthUser.email,
|
||||
name: oauthUser.name,
|
||||
});
|
||||
}).returning();
|
||||
user = res[0];
|
||||
}
|
||||
|
||||
const jwt = await create({ alg: "HS512", type: "JWT" }, {
|
||||
|
Reference in New Issue
Block a user