🐛 separate API proxy from frontend routes
Browse files at this commitc4abe81b0c5f959d8cbba6d8823fca09b83c39d7Parent: bc76d2d- fix
- 🐛 separate API proxy from frontend routes
5 changed files +27 -20
- README.md +1 -1
- nginx.conf +14 -11
- src/api/client.ts +3 -1
- src/components/exams/exam-room-ws.tsx +1 -1
- vite.config.ts +8 -6
README.mdmodified+1 -1
| @@ -25,7 +25,7 @@ bun run dev | |||
| 25 | 25 | ||
| 26 | Open [http://localhost:5173](http://localhost:5173). | 26 | Open [http://localhost:5173](http://localhost:5173). |
| 27 | 27 | ||
| 28 | -The Vite server proxies `/auth`, `/users`, `/notes`, `/events`, `/exams`, `/exam-room`, `/time`, and `/health` to the backend so the HttpOnly session cookie stays same-origin. | 28 | +The Vite server proxies `/api/auth`, `/api/users`, `/api/notes`, `/api/events`, `/api/exams`, `/api/courses`, `/api/marks`, `/api/time`, and `/api/health` to the backend so the HttpOnly session cookie stays same-origin while page URLs like `/notes` remain frontend routes on refresh. |
| 29 | 29 | ||
| 30 | ## Scripts | 30 | ## Scripts |
| 31 | 31 | ||
nginx.confmodified+14 -11
| @@ -3,11 +3,8 @@ server { | |||
| 3 | root /usr/share/nginx/html; | 3 | root /usr/share/nginx/html; |
| 4 | index index.html; | 4 | index index.html; |
| 5 | 5 | ||
| 6 | - location / { | 6 | + location ~ ^/api/(auth|users|notes|events|exams|courses|marks|health|time)(/|$) { |
| 7 | - try_files $uri /index.html; | 7 | + rewrite ^/api(/.*)$ $1 break; |
| 8 | - } | ||
| 9 | - | ||
| 10 | - location ~ ^/(auth|users|notes|events|exams|courses|marks)(/|$) { | ||
| 11 | proxy_pass http://hezarfen-backend:8080; | 8 | proxy_pass http://hezarfen-backend:8080; |
| 12 | proxy_http_version 1.1; | 9 | proxy_http_version 1.1; |
| 13 | proxy_set_header Host $http_host; | 10 | proxy_set_header Host $http_host; |
| @@ -18,11 +15,17 @@ server { | |||
| 18 | proxy_set_header Connection "upgrade"; | 15 | proxy_set_header Connection "upgrade"; |
| 19 | } | 16 | } |
| 20 | 17 | ||
| 21 | - location ~ ^/(health|time)$ { | 18 | + location /assets/ { |
| 22 | - proxy_pass http://hezarfen-backend:8080; | 19 | + try_files $uri =404; |
| 23 | - proxy_set_header Host $http_host; | 20 | + add_header Cache-Control "public, max-age=31536000, immutable" always; |
| 24 | - proxy_set_header X-Real-IP $remote_addr; | 21 | + } |
| 25 | - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | 22 | + |
| 26 | - proxy_set_header X-Forwarded-Proto $scheme; | 23 | + location = /index.html { |
| 24 | + add_header Cache-Control "no-store" always; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + location / { | ||
| 28 | + try_files $uri /index.html; | ||
| 29 | + add_header Cache-Control "no-store" always; | ||
| 27 | } | 30 | } |
| 28 | } | 31 | } |
src/api/client.tsmodified+3 -1
| @@ -16,6 +16,8 @@ type RequestOptions = { | |||
| 16 | signal?: AbortSignal; | 16 | signal?: AbortSignal; |
| 17 | }; | 17 | }; |
| 18 | 18 | ||
| 19 | +const API_PREFIX = "/api"; | ||
| 20 | + | ||
| 19 | export async function client<T>(path: string, options: RequestOptions = {}): Promise<T> { | 21 | export async function client<T>(path: string, options: RequestOptions = {}): Promise<T> { |
| 20 | const headers: Record<string, string> = {}; | 22 | const headers: Record<string, string> = {}; |
| 21 | let body: string | undefined; | 23 | let body: string | undefined; |
| @@ -25,7 +27,7 @@ export async function client<T>(path: string, options: RequestOptions = {}): Pro | |||
| 25 | body = JSON.stringify(options.body); | 27 | body = JSON.stringify(options.body); |
| 26 | } | 28 | } |
| 27 | 29 | ||
| 28 | - const res = await fetch(path, { | 30 | + const res = await fetch(`${API_PREFIX}${path}`, { |
| 29 | method: options.method ?? "GET", | 31 | method: options.method ?? "GET", |
| 30 | headers, | 32 | headers, |
| 31 | body, | 33 | body, |
src/components/exams/exam-room-ws.tsxmodified+1 -1
| @@ -49,7 +49,7 @@ export function ExamRoomWS(props: { exam: Exam }) { | |||
| 49 | 49 | ||
| 50 | const wsUrl = () => { | 50 | const wsUrl = () => { |
| 51 | const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; | 51 | const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; |
| 52 | - return `${proto}//${window.location.host}/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`; | 52 | + return `${proto}//${window.location.host}/api/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`; |
| 53 | }; | 53 | }; |
| 54 | 54 | ||
| 55 | const connectWs = () => { | 55 | const connectWs = () => { |
vite.config.tsmodified+8 -6
| @@ -12,12 +12,14 @@ export default defineConfig({ | |||
| 12 | }, | 12 | }, |
| 13 | }, | 13 | }, |
| 14 | server: { | 14 | server: { |
| 15 | - proxy: Object.fromEntries( | 15 | + proxy: { |
| 16 | - ["/auth", "/users", "/notes", "/events", "/exams", "/courses", "/marks", "/health", "/time"].map((p) => [ | 16 | + "/api": { |
| 17 | - p, | 17 | + target, |
| 18 | - { target, changeOrigin: true }, | 18 | + changeOrigin: true, |
| 19 | - ]), | 19 | + ws: true, |
| 20 | - ), | 20 | + rewrite: (path) => path.replace(/^\/api(?=\/|$)/, ""), |
| 21 | + }, | ||
| 22 | + }, | ||
| 21 | }, | 23 | }, |
| 22 | build: { | 24 | build: { |
| 23 | target: "esnext", | 25 | target: "esnext", |