Browse Source

check for user before getting orgs, create default config

Milo Schwartz 9 months ago
parent
commit
717aa09daa
6 changed files with 49 additions and 29 deletions
  1. 1 0
      Dockerfile
  2. 6 6
      config/config.example.yml
  3. 19 0
      server/config.ts
  4. 2 8
      server/routers/traefik/getTraefikConfig.ts
  5. 20 15
      src/app/layout.tsx
  6. 1 0
      src/app/page.tsx

+ 1 - 0
Dockerfile

@@ -22,6 +22,7 @@ RUN npm install --omit=dev
 
 
 COPY --from=builder /app/.next ./.next
 COPY --from=builder /app/.next ./.next
 COPY --from=builder /app/dist ./dist
 COPY --from=builder /app/dist ./dist
+COPY ./config/config.example.yml /app/dist/
 COPY server/db/names.json /app/dist/names.json
 COPY server/db/names.json /app/dist/names.json
 
 
 CMD ["npm", "start"]
 CMD ["npm", "start"]

+ 6 - 6
config/config.example.yml

@@ -1,20 +1,20 @@
 app:
 app:
     name: Pangolin
     name: Pangolin
     base_url: http://localhost:3000
     base_url: http://localhost:3000
-    log_level: debug
+    log_level: warning
     save_logs: "false"
     save_logs: "false"
 
 
 server:
 server:
     external_port: "3000"
     external_port: "3000"
     internal_port: "3001"
     internal_port: "3001"
-    internal_hostname: localhost
-    secure_cookies: "false"
+    internal_hostname: pangolin
+    secure_cookies: "true"
 
 
 traefik:
 traefik:
     cert_resolver: "letsencrypt"
     cert_resolver: "letsencrypt"
-    http_entrypoint: "http"
-    https_entrypoint: "https"
+    http_entrypoint: "web"
+    https_entrypoint: "websecure"
 
 
 rate_limit:
 rate_limit:
     window_minutes: "1"
     window_minutes: "1"
-    max_requests: "100"
+    max_requests: "100"

+ 19 - 0
server/config.ts

@@ -84,6 +84,25 @@ if (fs.existsSync(configFilePath1)) {
 } else if (fs.existsSync(configFilePath2)) {
 } else if (fs.existsSync(configFilePath2)) {
     environment = loadConfig(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) {
 if (!environment) {
     throw new Error("No configuration file found");
     throw new Error("No configuration file found");

+ 2 - 8
server/routers/traefik/getTraefikConfig.ts

@@ -33,12 +33,6 @@ export function buildTraefikConfig(
 
 
     const tls = {
     const tls = {
         certResolver: config.traefik.cert_resolver,
         certResolver: config.traefik.cert_resolver,
-        // domains: [ // TODO: figure out if this is neccessary
-        //     {
-        //         main: baseDomain,
-        //         sans: ["*." + baseDomain], 
-        //     },
-        // ],
     };
     };
 
 
     const http: any = {
     const http: any = {
@@ -59,8 +53,8 @@ export function buildTraefikConfig(
         },
         },
     };
     };
     for (const target of targets) {
     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] = {
         http.routers![routerName] = {
             entryPoints: [target.ssl ? config.traefik.https_entrypoint : config.traefik.https_entrypoint],
             entryPoints: [target.ssl ? config.traefik.https_entrypoint : config.traefik.https_entrypoint],

+ 20 - 15
src/app/layout.tsx

@@ -8,6 +8,7 @@ import { internal } from "@app/api";
 import { AxiosResponse } from "axios";
 import { AxiosResponse } from "axios";
 import { authCookieHeader } from "@app/api/cookies";
 import { authCookieHeader } from "@app/api/cookies";
 import { redirect } from "next/navigation";
 import { redirect } from "next/navigation";
+import { verifySession } from "@app/lib/auth/verifySession";
 
 
 export const metadata: Metadata = {
 export const metadata: Metadata = {
     title: `Dashboard - ${process.env.NEXT_PUBLIC_APP_NAME}`,
     title: `Dashboard - ${process.env.NEXT_PUBLIC_APP_NAME}`,
@@ -21,22 +22,26 @@ export default async function RootLayout({
 }: Readonly<{
 }: Readonly<{
     children: React.ReactNode;
     children: React.ReactNode;
 }>) {
 }>) {
-    // let orgs: ListOrgsResponse["orgs"] = [];
-    // try {
-    //     const res = await internal.get<AxiosResponse<ListOrgsResponse>>(
-    //         `/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<AxiosResponse<ListOrgsResponse>>(
+                `/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 (
     return (
         <html suppressHydrationWarning>
         <html suppressHydrationWarning>

+ 1 - 0
src/app/page.tsx

@@ -13,6 +13,7 @@ export default async function Page() {
 
 
     if (!user) {
     if (!user) {
         redirect("/auth/login");
         redirect("/auth/login");
+        return;
     }
     }
 
 
     let orgs: ListOrgsResponse["orgs"] = [];
     let orgs: ListOrgsResponse["orgs"] = [];