Fixes dev server issues, adds frontend proxy

This commit is contained in:
Emma 2023-06-11 14:15:44 -06:00
parent b951d1970d
commit 8fb7494464
6 changed files with 30 additions and 12 deletions

View File

@ -11,7 +11,7 @@
"middleware/": "./middleware/" "middleware/": "./middleware/"
}, },
"tasks": { "tasks": {
"dev": "deno run -A startDev.ts", "dev": "deno run -A --watch startDev.ts",
"createService": "deno run -A createService.ts" "createService": "deno run -A createService.ts"
} }
} }

7
middleware/debugLog.ts Normal file
View File

@ -0,0 +1,7 @@
import { Context, Middleware } from "oak";
export const debugLog = (message: string | ((arg: Context) => string)): Middleware =>
async (ctx, next) => {
console.log(typeof message === 'function' ? message(ctx) : message);
await next();
}

View File

@ -0,0 +1,4 @@
export const urlBuilder = (path: string) => {
const isDev = import.meta.env.DEV;
return encodeURI(isDev ? '/dev' : '' + path)
}

View File

@ -8,5 +8,13 @@ export default defineConfig({
appType: 'spa', appType: 'spa',
preview: { preview: {
port: 6969 port: 6969
},
server: {
proxy: {
'/dev': {
target: 'http://localhost:3000',
rewrite: (p) => p.replace('/dev', '')
}
}
} }
}) })

View File

@ -1,7 +1,6 @@
import { proxy } from "https://deno.land/x/oak_http_proxy@2.1.0/mod.ts"; import { proxy } from "https://deno.land/x/oak_http_proxy@2.1.0/mod.ts";
import { fileExists } from "./lib/fileExists.ts"; import { fileExists } from "./lib/fileExists.ts";
import { Application, Context, Router } from "oak"; import { Application, Context, Router } from "oak";
import { path } from "https://deno.land/x/compress@v0.4.1/deps.ts";
const app = new Application(); const app = new Application();
@ -21,6 +20,7 @@ for await (const dirEntry of Deno.readDir(Deno.cwd())) {
args: [ args: [
'run', 'run',
...perms, ...perms,
'--watch',
filename, filename,
port port
] ]
@ -28,8 +28,8 @@ for await (const dirEntry of Deno.readDir(Deno.cwd())) {
const prefix = Deno.readTextFileSync(prefixfile); const prefix = Deno.readTextFileSync(prefixfile);
const routes = new Router() const routes = new Router()
.all(`/${prefix}/(.*)`, proxy((ctx: Context) => `http://localhost:${port}${ctx.request.url.pathname}`, {})) .all(prefix ? `/${prefix}/(.*)` : '/(.*)', proxy((ctx: Context) => `http://localhost:${port}${ctx.request.url.pathname}`))
app.use(routes.allowedMethods()); app.use(routes.allowedMethods());
app.use(routes.routes()); app.use(routes.routes());

View File

@ -2,21 +2,20 @@
import { CGGService } from 'cgg/Application.ts'; import { CGGService } from 'cgg/Application.ts';
import { Router, send } from 'oak'; import { Router, send } from 'oak';
const app = new CGGService({ prefix: '/' }); const prefix = ''
const app = new CGGService({ prefix: `/${prefix}` });
// app.route(new Router() // app.route(new Router()
// .get('/', ctx => ctx.response.body = 'warstone-web service') // .get('/', ctx => ctx.response.body = 'warstone-web service')
// ); // );
const ROOT_DIR = "./public/testdata", ROOT_DIR_PATH = "/public"; const ROOT_DIR = "./project-warstone/dist";
app.use(async (ctx, next) => { app.use(async (ctx) => {
if (!ctx.request.url.pathname.startsWith(ROOT_DIR_PATH)) { if (ctx.request.url.pathname.includes('favicon')) return ctx.response.status = 404;
next(); const filePath = ctx.request.url.pathname.replace(prefix, "");
return;
}
const filePath = ctx.request.url.pathname.replace(ROOT_DIR_PATH, "");
await send(ctx, filePath, { await send(ctx, filePath, {
root: ROOT_DIR, root: ROOT_DIR,
index: 'index.html'
}); });
}); });