Skip to content

Commit a03893a

Browse files
authored
add swagger docs on api and make run inc frontend (#6)
1 parent 4909c6d commit a03893a

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ test:
2020
pytest backend/tests/ -v
2121

2222
run:
23-
uvicorn backend.main:app --reload --host 0.0.0.0 --port 8080
23+
uvicorn backend.main:app --reload --host 0.0.0.0 --port 8080 & \
24+
cd frontend && npm run dev & \
25+
wait

frontend/src/pages/api_key.astro

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@ import { SignIn } from 'auth-astro/components';
88
99
export const prerender = false;
1010
11-
// Check if user is authenticated
12-
const session = await getSession(Astro.request);
11+
// Detect local development
12+
const isDev = import.meta.env.DEV;
1313
14-
// If not authenticated, redirect to home page
15-
if (!session) {
16-
return Astro.redirect('/');
14+
let session;
15+
if (isDev) {
16+
// Dummy session for local development — bypasses Auth0
17+
session = {
18+
user: { name: 'Dev User', email: 'dev@localhost' },
19+
accessToken: 'dev-dummy-token',
20+
};
21+
} else {
22+
// Check if user is authenticated
23+
session = await getSession(Astro.request);
24+
if (!session) {
25+
return Astro.redirect('/');
26+
}
1727
}
1828
1929
// API key will be fetched client-side to avoid exposing it in the HTML
@@ -69,6 +79,35 @@ const apiUrl = process.env.VITE_API_URL || 'https://api.swissai.cscs.ch';
6979
</div>
7080
</div>
7181

82+
<!-- API Documentation Card -->
83+
<div class="animate card p-8">
84+
<div class="flex items-start gap-4">
85+
<div class="w-12 h-12 bg-gradient-to-r from-emerald-600 to-teal-600 rounded-xl flex items-center justify-center flex-shrink-0">
86+
<svg class="w-6 h-6 text-white" fill="currentColor" viewBox="0 0 20 20">
87+
<path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4zm2 6a1 1 0 011-1h6a1 1 0 110 2H7a1 1 0 01-1-1zm1 3a1 1 0 100 2h6a1 1 0 100-2H7z" clip-rule="evenodd"/>
88+
</svg>
89+
</div>
90+
<div>
91+
<h2 class="text-xl font-bold text-slate-900 dark:text-white mb-3">API Documentation</h2>
92+
<p class="text-slate-600 dark:text-slate-400 mb-4">
93+
Explore the full API reference with interactive documentation. Try out endpoints directly from your browser.
94+
</p>
95+
<a
96+
href={`${apiUrl}/docs`}
97+
target="_blank"
98+
rel="noopener noreferrer"
99+
class="inline-flex items-center gap-2 btn-primary px-6 py-3 rounded-xl text-sm font-semibold"
100+
>
101+
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
102+
<path d="M11 3a1 1 0 100 2h2.586l-6.293 6.293a1 1 0 101.414 1.414L15 6.414V9a1 1 0 102 0V4a1 1 0 00-1-1h-5z"/>
103+
<path d="M5 5a2 2 0 00-2 2v8a2 2 0 002 2h8a2 2 0 002-2v-3a1 1 0 10-2 0v3H5V7h3a1 1 0 000-2H5z"/>
104+
</svg>
105+
View Swagger API Docs
106+
</a>
107+
</div>
108+
</div>
109+
</div>
110+
72111
<!-- Main API Key Card -->
73112
<div class="animate card p-8">
74113
<div class="mb-6">
@@ -138,6 +177,15 @@ const apiUrl = process.env.VITE_API_URL || 'https://api.swissai.cscs.ch';
138177
</div>
139178
</div>
140179

180+
<!-- Export Environment Variable -->
181+
<div class="space-y-3">
182+
<h3 class="text-lg font-semibold text-slate-800 dark:text-slate-200">Set Environment Variable</h3>
183+
<p class="text-slate-600 dark:text-slate-400">Export your API key so you can copy and paste the examples below:</p>
184+
<div class="card p-4 bg-slate-50 dark:bg-slate-800/50">
185+
<pre class="text-sm text-slate-700 dark:text-slate-300 overflow-x-auto"><code>export CSCS_SERVING_API="<span id="exportApiKey" class="text-indigo-600 dark:text-indigo-400">your-api-key</span>"</code></pre>
186+
</div>
187+
</div>
188+
141189
<!-- CURL Example -->
142190
<div class="space-y-3">
143191
<h3 class="text-lg font-semibold text-slate-800 dark:text-slate-200">CURL Example</h3>
@@ -163,6 +211,7 @@ const apiUrl = process.env.VITE_API_URL || 'https://api.swissai.cscs.ch';
163211
</div>
164212
</div>
165213
</div>
214+
166215
</div>
167216
</Container>
168217
</PageLayout>
@@ -284,8 +333,10 @@ const apiUrl = process.env.VITE_API_URL || 'https://api.swissai.cscs.ch';
284333
}
285334

286335
// Update UI with API key
336+
const exportApiKey = document.getElementById('exportApiKey');
287337
if (apiKeyDisplay) apiKeyDisplay.value = apiKey;
288338
if (headerApiKey) headerApiKey.textContent = apiKey;
339+
if (exportApiKey) exportApiKey.textContent = apiKey;
289340

290341
// Hide loading message
291342
if (loadingMessage) loadingMessage.classList.add('hidden');

0 commit comments

Comments
 (0)