Skip to content

Commit

Permalink
Adds https-proxy as oauth authority for local or k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Jun 13, 2024
1 parent 19cd4c9 commit 7bc9bb1
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 17 deletions.
5 changes: 4 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ tasks:
deps: [ infra-up, api:only, k8s:infra-forward ]
interactive: true

local-web-app-for-develop:
deps: [ ui:viewer-dev, api:local-web-app-for-develop, ui:https-oauth-authority ]

local-web-app:
deps: [ ui:viewer-dev, api:local-web-app ]
deps: [ ui:viewer-dev, api:local-web-app, ui:https-oauth-authority ]
6 changes: 3 additions & 3 deletions backend/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ tasks:
cmds:
- kubectl port-forward service/db 27018:27017 -n languageforge --context dallas-rke

local-web-app:
local-web-app-for-develop:
label: dotnet
dir: ./LocalWebApp
cmd: dotnet watch --no-hot-reload
local-web-app-with-local-lexbox:
local-web-app:
label: Run LocalWebApp with Local LexBox
env:
Auth__DefaultAuthority: "https://localhost:3000"
Auth__DefaultAuthority: "https://localhost:3050"
dir: ./LocalWebApp
cmd: dotnet watch --no-hot-reload

Expand Down
17 changes: 17 additions & 0 deletions frontend/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,20 @@ tasks:
dir: ./viewer
deps: [ install-viewer ]
cmd: pnpm run dev-app


install-https-proxy:
dir: ./https-proxy
method: checksum
sources:
- package.json
cmds:
- corepack enable || true
- pnpm install
https-proxy:
dir: ./https-proxy
desc: "MSAL requires the oauth authority to be available over https. That's why this is here. As a bonus it dynamically looks for the UI either locally or in k8s."
aliases: [ https-oauth-authority ]
deps: [ install-https-proxy ]
cmd: pnpm run dev

3 changes: 2 additions & 1 deletion frontend/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default [
'playwright.config.ts',
'.svelte-kit/**',
'**/generated/**',
'viewer/'
'viewer/',
'https-proxy/',
],
},
js.configs.recommended,
Expand Down
24 changes: 24 additions & 0 deletions frontend/https-proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
17 changes: 17 additions & 0 deletions frontend/https-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "https-proxy",
"version": "0.0.1",
"private": true,
"packageManager": "[email protected]",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "^5.2.2",
"vite": "^5.2.0",
"@vitejs/plugin-basic-ssl": "^1.1.0"
}
}
68 changes: 68 additions & 0 deletions frontend/https-proxy/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import basicSsl from '@vitejs/plugin-basic-ssl';
import {defineConfig, type ProxyOptions} from 'vite';
import http from 'http';

async function checkTargetAvailability(url: string): Promise<boolean> {
return new Promise((resolve) => {
const req = http.get(url, (res) => {
resolve(!!res.statusCode && res.statusCode < 400);
});

req.on('error', () => {
resolve(false);
});

req.end();
});
}

const targets = ['http://localhost:3000', 'http://localhost'];

const lexboxServer: ProxyOptions = {
target: targets[0],
secure: false,
changeOrigin: false,
autoRewrite: true,
protocolRewrite: 'https',
headers: {
'x-forwarded-proto': 'https',
},
configure: async (proxy, options) => {
let availableTarget: string | undefined = undefined;

proxy.on('proxyReq', function () {
if (!availableTarget) console.warn(`Request before target (${lexboxServer.target}) was confirmed to be available.`);
});

while (!availableTarget) {
for (const target of targets) {
const isAvailable = await checkTargetAvailability(target);
if (isAvailable) {
options.target = availableTarget = target;
console.log('Will proxy to available target:', target);
return;
}
}
console.warn('No target available, retrying in 5s');
await new Promise((resolve) => setTimeout(resolve, 5000));
}
},
};

export default defineConfig({
build: {
target: 'esnext',
sourcemap: true
},
plugins: [
basicSsl(),
],
server: {
port: 3050,
host: true,
strictPort: true,
proxy: {
'/': lexboxServer,
}
},
});
13 changes: 12 additions & 1 deletion frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages:
- 'frontend'
- 'https-proxy'
- 'viewer'
11 changes: 1 addition & 10 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ import precompileIntl from 'svelte-intl-precompile/sveltekit-plugin';
import {type ProxyOptions, searchForWorkspaceRoot} from 'vite';
import { sveltekit } from '@sveltejs/kit/vite';




const inDocker = process.env['DockerDev'] === 'true';
const exposeServer = !inDocker;
const exposeServer = false;
const lexboxServer: ProxyOptions = {
target: 'http://localhost:5158',
secure: false,
changeOrigin: false,
autoRewrite: true,
protocolRewrite: 'https',
headers: {
'x-forwarded-proto': 'https',
}
};

export default defineConfig({
Expand Down

0 comments on commit 7bc9bb1

Please sign in to comment.