Split repo into apiserver/ and webserver/, add auth error handling and nginx proxy timeouts

This commit is contained in:
2026-09-28 23:05:00 -05:00
parent 32149a88e1
commit 13dbe86f19
13 changed files with 419 additions and 149 deletions
+532
View File
@@ -0,0 +1,532 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flux Image Generator</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #0f0f13;
color: #e0e0e0;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem 1rem;
}
h1 {
font-size: 1.8rem;
font-weight: 600;
margin-bottom: 0.3rem;
background: linear-gradient(135deg, #a78bfa, #60a5fa);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.subtitle {
color: #666;
font-size: 0.85rem;
margin-bottom: 2rem;
}
.input-area {
width: 100%;
max-width: 640px;
display: flex;
gap: 0.5rem;
margin-bottom: 2rem;
}
#prompt {
flex: 1;
padding: 0.85rem 1rem;
font-size: 1rem;
border: 1px solid #2a2a35;
border-radius: 10px;
background: #1a1a24;
color: #e0e0e0;
outline: none;
transition: border-color 0.2s;
}
#prompt:focus {
border-color: #a78bfa;
}
#prompt::placeholder {
color: #555;
}
#generate-btn {
padding: 0.85rem 1.5rem;
font-size: 1rem;
font-weight: 600;
border: none;
border-radius: 10px;
background: linear-gradient(135deg, #7c3aed, #3b82f6);
color: white;
cursor: pointer;
transition: opacity 0.2s, transform 0.1s;
white-space: nowrap;
}
#generate-btn:hover:not(:disabled) {
opacity: 0.9;
}
#generate-btn:active:not(:disabled) {
transform: scale(0.97);
}
#generate-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.result-area {
width: 100%;
max-width: 640px;
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
}
#image-container {
display: none;
flex-direction: column;
align-items: center;
gap: 1rem;
width: 100%;
}
#image-container.visible {
display: flex;
}
#result-image {
max-width: 100%;
border-radius: 12px;
border: 1px solid #2a2a35;
background: #1a1a24;
}
#download-btn {
padding: 0.7rem 1.4rem;
font-size: 0.9rem;
font-weight: 600;
border: 1px solid #2a2a35;
border-radius: 8px;
background: #1a1a24;
color: #a78bfa;
cursor: pointer;
transition: background 0.2s;
text-decoration: none;
}
#download-btn:hover {
background: #22222e;
}
.status {
font-size: 0.9rem;
color: #888;
text-align: center;
min-height: 1.4em;
}
.status.error {
color: #f87171;
}
.spinner {
display: inline-block;
width: 1em;
height: 1em;
margin-right: 0.5em;
vertical-align: -0.15em;
border: 2px solid #a78bfa;
border-right-color: transparent;
border-radius: 50%;
animation: spin 0.6s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.config {
width: 100%;
max-width: 640px;
margin-bottom: 1.5rem;
padding: 0.8rem 1rem;
background: #1a1a24;
border: 1px solid #2a2a35;
border-radius: 10px;
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.85rem;
color: #888;
}
.config label {
white-space: nowrap;
}
.config input {
flex: 1;
padding: 0.35rem 0.5rem;
font-size: 0.85rem;
border: 1px solid #2a2a35;
border-radius: 6px;
background: #0f0f13;
color: #e0e0e0;
outline: none;
}
.config input:focus,
.config select:focus {
border-color: #a78bfa;
}
.config select {
flex: 1;
padding: 0.35rem 0.5rem;
font-size: 0.85rem;
border: 1px solid #2a2a35;
border-radius: 6px;
background: #0f0f13;
color: #e0e0e0;
outline: none;
cursor: pointer;
}
.prompt-wrapper {
flex: 1;
position: relative;
display: flex;
align-items: center;
}
.prompt-wrapper #prompt {
padding-right: 2.5rem;
}
.clear-btn {
position: absolute;
right: 0.6rem;
background: none;
border: none;
color: #555;
font-size: 1rem;
cursor: pointer;
padding: 0.3rem;
line-height: 1;
transition: color 0.2s;
z-index: 1;
}
.clear-btn:hover {
color: #a78bfa;
}
#login-overlay {
position: fixed;
inset: 0;
background: #0f0f13;
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
#login-overlay.hidden { display: none; }
.login-box {
width: 100%;
max-width: 360px;
padding: 2rem;
text-align: center;
}
.login-box h2 {
font-size: 1.5rem;
margin-bottom: 0.5rem;
background: linear-gradient(135deg, #a78bfa, #60a5fa);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.login-subtitle {
color: #666;
font-size: 0.85rem;
margin-bottom: 1.5rem;
}
.login-box input[type="text"],
.login-box input[type="password"] {
width: 100%;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
font-size: 1rem;
border: 1px solid #2a2a35;
border-radius: 8px;
background: #1a1a24;
color: #e0e0e0;
outline: none;
}
.login-box input:focus { border-color: #a78bfa; }
#login-btn {
width: 100%;
padding: 0.75rem;
font-size: 1rem;
font-weight: 600;
border: none;
border-radius: 8px;
background: linear-gradient(135deg, #7c3aed, #3b82f6);
color: white;
cursor: pointer;
}
#login-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.login-error {
color: #f87171;
font-size: 0.85rem;
margin-bottom: 0.75rem;
}
#logout-btn {
position: fixed;
top: 1rem;
right: 1rem;
z-index: 999;
background: #1a1a24;
color: #a78bfa;
border: 1px solid #2a2a35;
padding: 0.5rem 1rem;
border-radius: 8px;
cursor: pointer;
font-size: 0.85rem;
}
#logout-btn:hover { background: #22222e; }
</style>
</head>
<body>
<div id="login-overlay">
<div class="login-box">
<h2>Flux Image Generator</h2>
<p class="login-subtitle">Sign in to continue</p>
<form id="login-form">
<input type="text" id="login-username" placeholder="Username" required autocomplete="username" />
<input type="password" id="login-password" placeholder="Password" required autocomplete="current-password" />
<p id="login-error" class="login-error" style="display:none"></p>
<button type="submit" id="login-btn">Sign In</button>
</form>
</div>
</div>
<button id="logout-btn" style="display:none">Sign Out</button>
<h1>Flux Image Generator</h1>
<p class="subtitle">Powered by flux-schnell</p>
<div class="config">
<label for="api-url">API URL:</label>
<input id="api-url" value="/v1/images/generations" />
</div>
<div class="config">
<label for="size-select">Size:</label>
<select id="size-select">
<option value="1024x1024">1024x1024 (1:1)</option>
<option value="1920x1088">1920x1088 (16:9)</option>
<option value="1088x1920">1088x1920 (9:16)</option>
<option value="1376x1024">1376x1024 (4:3)</option>
<option value="1024x1376">1024x1376 (3:4)</option>
<option value="1536x1024">1536x1024 (3:2)</option>
<option value="1024x1536">1024x1536 (2:3)</option>
<option value="2048x2048">2048x2048 (1:1 XL)</option>
<option value="2048x1152">2048x1152 (16:9 XL)</option>
<option value="1152x2048">1152x2048 (9:16 XL)</option>
</select>
</div>
<div class="input-area">
<div class="prompt-wrapper">
<input id="prompt" type="text" placeholder="Describe the image you want to create..." autofocus />
<button id="clear-btn" class="clear-btn" title="Clear">✕</button>
</div>
<button id="generate-btn">Generate</button>
</div>
<p class="status" id="status"></p>
<div class="result-area">
<div id="image-container">
<img id="result-image" alt="Generated image" />
<a id="download-btn" download="flux-image.png">⬇ Download PNG</a>
</div>
</div>
<script>
const loginOverlay = document.getElementById('login-overlay');
const loginForm = document.getElementById('login-form');
const loginBtn = document.getElementById('login-btn');
const loginError = document.getElementById('login-error');
const logoutBtn = document.getElementById('logout-btn');
const promptInput = document.getElementById('prompt');
const clearBtn = document.getElementById('clear-btn');
const sizeSelect = document.getElementById('size-select');
const generateBtn = document.getElementById('generate-btn');
const apiInput = document.getElementById('api-url');
const statusEl = document.getElementById('status');
const imageContainer = document.getElementById('image-container');
const resultImage = document.getElementById('result-image');
const downloadBtn = document.getElementById('download-btn');
async function checkAuth() {
try {
const resp = await fetch('/auth/check');
if (resp.ok) {
loginOverlay.classList.add('hidden');
logoutBtn.style.display = 'block';
return true;
}
} catch (e) {
// Not authenticated
}
return false;
}
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
loginBtn.disabled = true;
loginError.style.display = 'none';
const username = document.getElementById('login-username').value;
const password = document.getElementById('login-password').value;
try {
const resp = await fetch('/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
if (resp.ok) {
loginOverlay.classList.add('hidden');
logoutBtn.style.display = 'block';
} else {
let detail;
try {
const data = await resp.json();
detail = data.detail || (resp.status >= 500 ? `Server error (${resp.status})` : 'Invalid credentials');
} catch (_) {
detail = resp.status >= 500 ? `Server error (${resp.status})` : 'Invalid credentials';
}
loginError.textContent = detail;
loginError.style.display = 'block';
}
} catch (err) {
loginError.textContent = 'Connection error';
loginError.style.display = 'block';
} finally {
loginBtn.disabled = false;
}
});
logoutBtn.addEventListener('click', async () => {
await fetch('/auth/logout', { method: 'POST' });
loginOverlay.classList.remove('hidden');
logoutBtn.style.display = 'none';
});
function setStatus(msg, isError = false) {
statusEl.innerHTML = msg;
statusEl.className = 'status' + (isError ? ' error' : '');
}
async function generate() {
const prompt = promptInput.value.trim();
if (!prompt) {
setStatus('Please enter a prompt.', true);
return;
}
const apiUrl = apiInput.value.trim();
if (!apiUrl) {
setStatus('Please enter an API URL.', true);
return;
}
generateBtn.disabled = true;
generateBtn.textContent = 'Generating...';
setStatus('<span class="spinner"></span> Generating image...');
imageContainer.classList.remove('visible');
try {
const [width, height] = sizeSelect.value.split('x').map(Number);
const response = await fetch(apiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt, model: 'flux-schnell', width, height })
});
if (response.status === 401) {
loginOverlay.classList.remove('hidden');
logoutBtn.style.display = 'none';
throw new Error('Session expired');
}
if (!response.ok) {
let detail = `HTTP ${response.status}: ${response.statusText}`;
try {
const errData = await response.json();
if (errData.detail) detail = errData.detail;
} catch (_) { /* non-JSON error body */ }
throw new Error(detail);
}
const data = await response.json();
const base64 = data.data?.[0]?.b64_json;
if (!base64 || base64 === 'null') {
throw new Error(data.detail || 'No image data returned.');
}
const blob = await fetch(`data:image/png;base64,${base64}`).then(r => r.blob());
const url = URL.createObjectURL(blob);
resultImage.src = url;
downloadBtn.href = url;
const ts = new Date().toISOString().replace(/[:.]/g, '-');
downloadBtn.download = `flux-image-${ts}.png`;
imageContainer.classList.add('visible');
setStatus('');
} catch (err) {
setStatus(`Error: ${err.message}`, true);
} finally {
generateBtn.disabled = false;
generateBtn.textContent = 'Generate';
}
}
generateBtn.addEventListener('click', generate);
promptInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') generate();
});
clearBtn.addEventListener('click', () => {
promptInput.value = '';
promptInput.focus();
});
promptInput.addEventListener('focus', () => {
promptInput.select();
});
// Check session on page load
checkAuth().then(authenticated => {
if (!authenticated) {
loginOverlay.classList.remove('hidden');
}
});
</script>
</body>
</html>