Skip to content

feature: Add support for self hosting #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 10 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2497,4 +2497,4 @@ impl<F: Future<Output = T>, T, E> TransposeAsync for Result<F, E> {
}
}
}
}
}
2 changes: 1 addition & 1 deletion apps/desktop/src-tauri/src/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,4 @@ fn project_config_from_recording(
}),
..default_config.unwrap_or_default()
}
}
}
2 changes: 1 addition & 1 deletion apps/desktop/src/routes/(window-chrome)/(main).tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1156,4 +1156,4 @@ function ChangelogButton() {
</Tooltip.Portal>
</Tooltip.Root>
);
}
}
2 changes: 1 addition & 1 deletion apps/desktop/src/routes/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ export default function Debug() {
</ul>
</main>
);
}
}
32 changes: 32 additions & 0 deletions apps/web/app/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use server";

import {
getServerConfig as getServerConfigInternal,
canInstanceAddUser,
isWorkspacePro,
} from "@/utils/instance/functions";
import { serverEnv } from "@cap/env";

export async function getServerConfigAction() {
const serverConfig = await getServerConfigInternal();

const googleSigninEnabled = serverEnv.GOOGLE_CLIENT_ID !== undefined;
const workosSigninEnabled = serverEnv.WORKOS_CLIENT_ID !== undefined;
return {
isCapCloud: serverConfig.isCapCloud,
signupsEnabled: serverConfig.signupsEnabled,
auth: {
google: googleSigninEnabled,
workos: workosSigninEnabled,
},
};
}

export async function canInstanceAddUserAction() {
return await canInstanceAddUser();
}

export async function isWorkspaceProAction(workspaceId: string) {
"use server";
return isWorkspacePro({ workspaceId });
}
93 changes: 93 additions & 0 deletions apps/web/app/api/admin/server-config/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { serverConfigTable } from "@cap/database/schema";
import { eq } from "drizzle-orm";
import { NextRequest, NextResponse } from "next/server";

// Helper function to check if user is a super admin
async function isSuperAdmin() {
const currentUser = await getCurrentUser();
if (!currentUser) return false;

const serverConfig = await db.query.serverConfigTable.findFirst({
where: eq(serverConfigTable.id, 1),
});

if (!serverConfig) return false;

return (
serverConfig.superAdminIds.includes(currentUser.id) ||
currentUser.email.endsWith("@cap.so")
);
}

// GET handler to retrieve server configuration
export async function GET() {
// Check if user is authorized
if (!(await isSuperAdmin())) {
return NextResponse.json({ error: "Not authorized" }, { status: 403 });
}

try {
const serverConfig = await db.query.serverConfigTable.findFirst({
where: eq(serverConfigTable.id, 1),
});

return NextResponse.json(serverConfig);
} catch (error) {
console.error("Error fetching server config:", error);
return NextResponse.json(
{ error: "Failed to fetch server configuration" },
{ status: 500 }
);
}
}

// PUT handler to update server configuration
export async function PUT(request: NextRequest) {
// Check if user is authorized
if (!(await isSuperAdmin())) {
return NextResponse.json({ error: "Not authorized" }, { status: 403 });
}

try {
const body = await request.json();

// Validate the request body
const validFields = [
"licenseKey",
"signupsEnabled",
"emailSendFromName",
"emailSendFromEmail",
"superAdminIds",
];

const updateData: Record<string, any> = {};

// Only include valid fields in the update
for (const field of validFields) {
if (body[field] !== undefined) {
updateData[field] = body[field];
}
}

// Update the server config
await db
.update(serverConfigTable)
.set(updateData)
.where(eq(serverConfigTable.id, 1));

// Get the updated config
const updatedConfig = await db.query.serverConfigTable.findFirst({
where: eq(serverConfigTable.id, 1),
});

return NextResponse.json(updatedConfig);
} catch (error) {
console.error("Error updating server config:", error);
return NextResponse.json(
{ error: "Failed to update server configuration" },
{ status: 500 }
);
}
}
Loading
Loading