From 717aa09daa6ec07fe2907f13ec883867050b0958 Mon Sep 17 00:00:00 2001 From: Milo Schwartz Date: Tue, 22 Oct 2024 23:58:00 -0400 Subject: [PATCH] check for user before getting orgs, create default config --- Dockerfile | 1 + config/config.example.yml | 12 ++++---- server/config.ts | 19 ++++++++++++ server/routers/traefik/getTraefikConfig.ts | 10 ++----- src/app/layout.tsx | 35 ++++++++++++---------- src/app/page.tsx | 1 + 6 files changed, 49 insertions(+), 29 deletions(-) diff --git a/Dockerfile b/Dockerfile index bc6caf39..b20abf1e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,7 @@ RUN npm install --omit=dev COPY --from=builder /app/.next ./.next COPY --from=builder /app/dist ./dist +COPY ./config/config.example.yml /app/dist/ COPY server/db/names.json /app/dist/names.json CMD ["npm", "start"] diff --git a/config/config.example.yml b/config/config.example.yml index bf37dc82..ddeae107 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -1,20 +1,20 @@ app: name: Pangolin base_url: http://localhost:3000 - log_level: debug + log_level: warning save_logs: "false" server: external_port: "3000" internal_port: "3001" - internal_hostname: localhost - secure_cookies: "false" + internal_hostname: pangolin + secure_cookies: "true" traefik: cert_resolver: "letsencrypt" - http_entrypoint: "http" - https_entrypoint: "https" + http_entrypoint: "web" + https_entrypoint: "websecure" rate_limit: window_minutes: "1" - max_requests: "100" + max_requests: "100" \ No newline at end of file diff --git a/server/config.ts b/server/config.ts index 37b5e2cd..f427da1a 100644 --- a/server/config.ts +++ b/server/config.ts @@ -84,6 +84,25 @@ if (fs.existsSync(configFilePath1)) { } else if (fs.existsSync(configFilePath2)) { environment = loadConfig(configFilePath2); } +if (!environment) { + const exampleConfigPath = path.join("config.example.yml"); + if (fs.existsSync(exampleConfigPath)) { + try { + const exampleConfigContent = fs.readFileSync(exampleConfigPath, "utf8"); + fs.writeFileSync(configFilePath1, exampleConfigContent, "utf8"); + environment = loadConfig(configFilePath1); + } catch (error) { + if (error instanceof Error) { + throw new Error( + `Error creating configuration file from example: ${error.message}`, + ); + } + throw error; + } + } else { + throw new Error("No configuration file found and no example configuration available"); + } +} if (!environment) { throw new Error("No configuration file found"); diff --git a/server/routers/traefik/getTraefikConfig.ts b/server/routers/traefik/getTraefikConfig.ts index d83e8fad..d966ee15 100644 --- a/server/routers/traefik/getTraefikConfig.ts +++ b/server/routers/traefik/getTraefikConfig.ts @@ -33,12 +33,6 @@ export function buildTraefikConfig( const tls = { certResolver: config.traefik.cert_resolver, - // domains: [ // TODO: figure out if this is neccessary - // { - // main: baseDomain, - // sans: ["*." + baseDomain], - // }, - // ], }; const http: any = { @@ -59,8 +53,8 @@ export function buildTraefikConfig( }, }; for (const target of targets) { - const routerName = `router-${target.targetId}`; - const serviceName = `service-${target.targetId}`; + const routerName = `${target.targetId}-router`; + const serviceName = `${target.targetId}-service`; http.routers![routerName] = { entryPoints: [target.ssl ? config.traefik.https_entrypoint : config.traefik.https_entrypoint], diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 030c4c7b..2a7fb6a2 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -8,6 +8,7 @@ import { internal } from "@app/api"; import { AxiosResponse } from "axios"; import { authCookieHeader } from "@app/api/cookies"; import { redirect } from "next/navigation"; +import { verifySession } from "@app/lib/auth/verifySession"; export const metadata: Metadata = { title: `Dashboard - ${process.env.NEXT_PUBLIC_APP_NAME}`, @@ -21,22 +22,26 @@ export default async function RootLayout({ }: Readonly<{ children: React.ReactNode; }>) { - // let orgs: ListOrgsResponse["orgs"] = []; - // try { - // const res = await internal.get>( - // `/orgs`, - // authCookieHeader(), - // ); - // if (res && res.data.data.orgs) { - // orgs = res.data.data.orgs; - // } + const user = await verifySession(); - // if (!orgs.length) { - // redirect(`/setup`); - // } - // } catch (e) { - // console.error("Error fetching orgs", e); - // } + let orgs: ListOrgsResponse["orgs"] = []; + if (user) { + try { + const res = await internal.get>( + `/orgs`, + authCookieHeader(), + ); + if (res && res.data.data.orgs) { + orgs = res.data.data.orgs; + } + + if (!orgs.length) { + redirect(`/setup`); + } + } catch (e) { + console.error("Error fetching orgs", e); + } + } return ( diff --git a/src/app/page.tsx b/src/app/page.tsx index 36782e4c..16673472 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -13,6 +13,7 @@ export default async function Page() { if (!user) { redirect("/auth/login"); + return; } let orgs: ListOrgsResponse["orgs"] = [];