diff --git a/pages/deploy.tsx b/pages/deploy.tsx index 0b528c9..e2d41ab 100644 --- a/pages/deploy.tsx +++ b/pages/deploy.tsx @@ -1,6 +1,20 @@ import { useState } from 'react'; import axios from 'axios'; +// Create axios instance with custom config +const api = axios.create({ + baseURL: 'https://35.227.177.48:8443', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + timeout: 300000, // 5 minutes + // For development only - remove in production + httpsAgent: new (require('https').Agent)({ + rejectUnauthorized: false + }) +}); + export default function DeploymentForm() { const [formData, setFormData] = useState({ project_name: '', @@ -9,38 +23,48 @@ export default function DeploymentForm() { }); const [deployedUrl, setDeployedUrl] = useState(null); const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); + setError(null); + setDeployedUrl(null); try { - const envLines = formData.env_vars.split('\n').filter(line => line.trim()); - const filteredEnvVars = Object.fromEntries( - envLines.map(line => { - const [key, value] = line.split('='); - return [key.trim(), value.trim()]; - }) - ); + // Parse environment variables + const envVars = formData.env_vars + .split('\n') + .filter(line => line.trim()) + .reduce((acc, line) => { + const [key, ...values] = line.split('='); + if (key && values.length > 0) { + acc[key.trim()] = values.join('=').trim(); + } + return acc; + }, {} as Record); - const response = await axios.post('https://35.227.177.48:8443/deploy', { + // Make the API call + const response = await api.post('/deploy', { git_url: formData.git_url, project_name: formData.project_name, port: "3000", - env_vars: filteredEnvVars - }, { - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/json' - }, - timeout: 300000, + env_vars: envVars }); - const deployedPort = response.data.port || "3000"; - const formattedUrl = `https://35.227.177.48:${deployedPort}`; - setDeployedUrl(formattedUrl); - } catch (error) { - console.error('Deployment failed:', error); + if (response.data.status === 'success') { + // Use the URL from the response directly + setDeployedUrl(response.data.url); + } else { + throw new Error(response.data.error || 'Deployment failed'); + } + } catch (err) { + console.error('Deployment failed:', err); + setError( + err.response?.data?.error || + err.message || + 'Failed to deploy. Please try again.' + ); } finally { setLoading(false); } @@ -61,6 +85,8 @@ export default function DeploymentForm() { onChange={(e) => setFormData(prev => ({...prev, project_name: e.target.value}))} className="w-full p-2 border rounded focus:ring-2 focus:ring-blue-500" required + pattern="[a-z0-9-]+" + title="Only lowercase letters, numbers, and hyphens are allowed" /> @@ -74,6 +100,8 @@ export default function DeploymentForm() { onChange={(e) => setFormData(prev => ({...prev, git_url: e.target.value}))} className="w-full p-2 border rounded focus:ring-2 focus:ring-blue-500" required + pattern="https://github\.com/.*" + title="Must be a valid GitHub URL" /> @@ -88,27 +116,53 @@ export default function DeploymentForm() { placeholder="KEY=value ANOTHER_KEY=another_value" rows={4} /> +

+ One variable per line in KEY=value format +

+ {error && ( +
+

Deployment Failed

+

{error}

+
+ )} + {deployedUrl && (

Deployment Successful!

+

Your application has been deployed successfully.

{deployedUrl} + + +
)}