what the heck is a cors amiright
This commit is contained in:
commit
f1465f8c53
5 changed files with 187 additions and 0 deletions
57
index.html
Normal file
57
index.html
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<!-- This is a simple HTML page that embeds a Python trinket using an iframe for testing -->
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Byte Camp</title>
|
||||
<style>
|
||||
/* Base dark mode styling */
|
||||
body {
|
||||
background-color: #121212;
|
||||
color: #f0f0f0;
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
header {
|
||||
background-color: #1e1e1e;
|
||||
padding: 1rem 2rem;
|
||||
text-align: center;
|
||||
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
header h1 {
|
||||
margin: 0;
|
||||
font-size: 2rem;
|
||||
color: #00d8ff;
|
||||
}
|
||||
|
||||
main {
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
iframe {
|
||||
border: none;
|
||||
width: 100%;
|
||||
height: 600px;
|
||||
border-radius: 8px;
|
||||
background-color: #1e1e1e;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Byte Camp</h1>
|
||||
</header>
|
||||
<main>
|
||||
<iframe src="https://trinket-proxy.fly.dev/embed/python/4d53507eddfe" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
|
||||
|
||||
|
||||
|
||||
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
39
trinket-proxy/Dockerfile
Normal file
39
trinket-proxy/Dockerfile
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# syntax = docker/dockerfile:1
|
||||
|
||||
# Adjust NODE_VERSION as desired
|
||||
ARG NODE_VERSION=22.13.1
|
||||
FROM node:${NODE_VERSION}-slim AS base
|
||||
|
||||
LABEL fly_launch_runtime="Node.js"
|
||||
|
||||
# Node.js app lives here
|
||||
WORKDIR /app
|
||||
|
||||
# Set production environment
|
||||
ENV NODE_ENV="production"
|
||||
|
||||
|
||||
# Throw-away build stage to reduce size of final image
|
||||
FROM base AS build
|
||||
|
||||
# Install packages needed to build node modules
|
||||
RUN apt-get update -qq && \
|
||||
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3
|
||||
|
||||
# Install node modules
|
||||
COPY package-lock.json package.json ./
|
||||
RUN npm ci
|
||||
|
||||
# Copy application code
|
||||
COPY . .
|
||||
|
||||
|
||||
# Final stage for app image
|
||||
FROM base
|
||||
|
||||
# Copy built application
|
||||
COPY --from=build /app /app
|
||||
|
||||
# Start the server by default, this can be overwritten at runtime
|
||||
EXPOSE 3000
|
||||
CMD [ "node", "index.js" ]
|
||||
22
trinket-proxy/fly.toml
Normal file
22
trinket-proxy/fly.toml
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# fly.toml app configuration file generated for trinket-proxy on 2025-07-10T16:44:50-07:00
|
||||
#
|
||||
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
|
||||
#
|
||||
|
||||
app = 'trinket-proxy'
|
||||
primary_region = 'sea'
|
||||
|
||||
[build]
|
||||
|
||||
[http_service]
|
||||
internal_port = 3000
|
||||
force_https = true
|
||||
auto_stop_machines = 'suspend'
|
||||
auto_start_machines = true
|
||||
min_machines_running = 0
|
||||
processes = ['app']
|
||||
|
||||
[[vm]]
|
||||
memory = '1gb'
|
||||
cpu_kind = 'shared'
|
||||
cpus = 1
|
||||
50
trinket-proxy/index.js
Normal file
50
trinket-proxy/index.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
const express = require('express');
|
||||
const { createProxyMiddleware } = require('http-proxy-middleware');
|
||||
const https = require('https');
|
||||
|
||||
const app = express();
|
||||
localTesting = false; // Set to true for testing, false for production
|
||||
const proxyServerUrl = localTesting ? 'https://trinket-proxy.fly.dev' : 'http://localhost:3000';
|
||||
|
||||
const modifyHtml = (html) => {
|
||||
return html
|
||||
// Style injection for dark mode
|
||||
.replace(
|
||||
/<div id="outputContainer" class="withoutTabs">/g,
|
||||
'<div id="outputContainer" class="withoutTabs" style="background-color:black; color:white">'
|
||||
)
|
||||
// Rewrite absolute URLs
|
||||
.replace(/https:\/\/trinket\.io/g, proxyServerUrl)
|
||||
.replace(/\/\/trinket\.io/g, proxyServerUrl);
|
||||
};
|
||||
|
||||
app.use('/', async (req, res, next) => {
|
||||
const targetUrl = `https://trinket.io${req.originalUrl}`;
|
||||
|
||||
// Only modify HTML pages (embed endpoints)
|
||||
if (req.originalUrl.startsWith('/embed')) {
|
||||
https.get(targetUrl, (proxyRes) => {
|
||||
let body = '';
|
||||
proxyRes.on('data', (chunk) => body += chunk);
|
||||
proxyRes.on('end', () => {
|
||||
res.set('Content-Type', 'text/html');
|
||||
res.send(modifyHtml(body));
|
||||
});
|
||||
}).on('error', (err) => {
|
||||
console.error('Proxy error:', err);
|
||||
res.sendStatus(500);
|
||||
});
|
||||
} else {
|
||||
// Static content (JS, CSS, images, etc.)
|
||||
createProxyMiddleware({
|
||||
target: 'https://trinket.io',
|
||||
changeOrigin: true,
|
||||
selfHandleResponse: false,
|
||||
})(req, res, next);
|
||||
}
|
||||
});
|
||||
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Trinket proxy running at http://localhost:${PORT}`);
|
||||
});
|
||||
19
trinket-proxy/package.json
Normal file
19
trinket-proxy/package.json
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "trinket-proxy",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"express": "^5.1.0",
|
||||
"http-proxy-middleware": "^2.0.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@flydotio/dockerfile": "^0.7.10"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue