optimize docker deployment

This commit is contained in:
musistudio
2025-09-03 09:58:09 +08:00
parent 5761e165fd
commit e670302e9e
3 changed files with 43 additions and 47 deletions

7
Dockerfile Normal file
View File

@@ -0,0 +1,7 @@
FROM node:20-alpine
RUN npm install -g @musistudio/claude-code-router
EXPOSE 3456
CMD ["ccr", "start"]

View File

@@ -1,24 +0,0 @@
FROM node:20-alpine
WORKDIR /app
# Copy all files
COPY . .
# Install pnpm globally
RUN npm install -g pnpm
# Install dependencies
RUN pnpm install --frozen-lockfile
# Fix rollup optional dependencies issue
RUN cd ui && npm install
# Build the entire project including UI
RUN pnpm run build
# Expose port
EXPOSE 3456
# Start the router service
CMD ["node", "dist/cli.js", "start"]

View File

@@ -83,25 +83,38 @@ export const readConfigFile = async () => {
} catch (readError: any) { } catch (readError: any) {
if (readError.code === "ENOENT") { if (readError.code === "ENOENT") {
// Config file doesn't exist, prompt user for initial setup // Config file doesn't exist, prompt user for initial setup
const name = await question("Enter Provider Name: "); try {
const APIKEY = await question("Enter Provider API KEY: "); // Initialize directories
const baseUrl = await question("Enter Provider URL: "); await initDir();
const model = await question("Enter MODEL Name: ");
const config = Object.assign({}, DEFAULT_CONFIG, { // Backup existing config file if it exists
Providers: [ const backupPath = await backupConfigFile();
{ if (backupPath) {
name, console.log(
api_base_url: baseUrl, `Backed up existing configuration file to ${backupPath}`
api_key: APIKEY, );
models: [model], }
}, const config = {
], PORT: 3456,
Router: { Providers: [],
default: `${name},${model}`, Router: {},
}, }
}); // Create a minimal default config file
await writeConfigFile(config); await writeConfigFile(config);
return config; console.log(
"Created minimal default configuration file at ~/.claude-code-router/config.json"
);
console.log(
"Please edit this file with your actual configuration."
);
return config
} catch (error: any) {
console.error(
"Failed to create default configuration:",
error.message
);
process.exit(1);
}
} else { } else {
console.error(`Failed to read config file at ${CONFIG_FILE}`); console.error(`Failed to read config file at ${CONFIG_FILE}`);
console.error("Error details:", readError.message); console.error("Error details:", readError.message);
@@ -116,19 +129,19 @@ export const backupConfigFile = async () => {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const backupPath = `${CONFIG_FILE}.${timestamp}.bak`; const backupPath = `${CONFIG_FILE}.${timestamp}.bak`;
await fs.copyFile(CONFIG_FILE, backupPath); await fs.copyFile(CONFIG_FILE, backupPath);
// Clean up old backups, keeping only the 3 most recent // Clean up old backups, keeping only the 3 most recent
try { try {
const configDir = path.dirname(CONFIG_FILE); const configDir = path.dirname(CONFIG_FILE);
const configFileName = path.basename(CONFIG_FILE); const configFileName = path.basename(CONFIG_FILE);
const files = await fs.readdir(configDir); const files = await fs.readdir(configDir);
// Find all backup files for this config // Find all backup files for this config
const backupFiles = files const backupFiles = files
.filter(file => file.startsWith(configFileName) && file.endsWith('.bak')) .filter(file => file.startsWith(configFileName) && file.endsWith('.bak'))
.sort() .sort()
.reverse(); // Sort in descending order (newest first) .reverse(); // Sort in descending order (newest first)
// Delete all but the 3 most recent backups // Delete all but the 3 most recent backups
if (backupFiles.length > 3) { if (backupFiles.length > 3) {
for (let i = 3; i < backupFiles.length; i++) { for (let i = 3; i < backupFiles.length; i++) {
@@ -139,7 +152,7 @@ export const backupConfigFile = async () => {
} catch (cleanupError) { } catch (cleanupError) {
console.warn("Failed to clean up old backups:", cleanupError); console.warn("Failed to clean up old backups:", cleanupError);
} }
return backupPath; return backupPath;
} }
} catch (error) { } catch (error) {