From 8fb7494464066eb8c10383e697ca0e181ab7ce55 Mon Sep 17 00:00:00 2001 From: Emma Date: Sun, 11 Jun 2023 14:15:44 -0600 Subject: [PATCH] Fixes dev server issues, adds frontend proxy --- deno.jsonc | 2 +- middleware/debugLog.ts | 7 +++++++ project-warstone/src/utils/UrlBuilder.ts | 4 ++++ project-warstone/vite.config.ts | 8 ++++++++ startDev.ts | 6 +++--- warstone-web-service/main.ts | 15 +++++++-------- 6 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 middleware/debugLog.ts create mode 100644 project-warstone/src/utils/UrlBuilder.ts diff --git a/deno.jsonc b/deno.jsonc index c70ca11..905f3eb 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -11,7 +11,7 @@ "middleware/": "./middleware/" }, "tasks": { - "dev": "deno run -A startDev.ts", + "dev": "deno run -A --watch startDev.ts", "createService": "deno run -A createService.ts" } } \ No newline at end of file diff --git a/middleware/debugLog.ts b/middleware/debugLog.ts new file mode 100644 index 0000000..9682311 --- /dev/null +++ b/middleware/debugLog.ts @@ -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(); + } \ No newline at end of file diff --git a/project-warstone/src/utils/UrlBuilder.ts b/project-warstone/src/utils/UrlBuilder.ts new file mode 100644 index 0000000..061c885 --- /dev/null +++ b/project-warstone/src/utils/UrlBuilder.ts @@ -0,0 +1,4 @@ +export const urlBuilder = (path: string) => { + const isDev = import.meta.env.DEV; + return encodeURI(isDev ? '/dev' : '' + path) +} \ No newline at end of file diff --git a/project-warstone/vite.config.ts b/project-warstone/vite.config.ts index 33acf40..1bf567f 100644 --- a/project-warstone/vite.config.ts +++ b/project-warstone/vite.config.ts @@ -8,5 +8,13 @@ export default defineConfig({ appType: 'spa', preview: { port: 6969 + }, + server: { + proxy: { + '/dev': { + target: 'http://localhost:3000', + rewrite: (p) => p.replace('/dev', '') + } + } } }) diff --git a/startDev.ts b/startDev.ts index 9135cdd..6576b3d 100644 --- a/startDev.ts +++ b/startDev.ts @@ -1,7 +1,6 @@ import { proxy } from "https://deno.land/x/oak_http_proxy@2.1.0/mod.ts"; import { fileExists } from "./lib/fileExists.ts"; import { Application, Context, Router } from "oak"; -import { path } from "https://deno.land/x/compress@v0.4.1/deps.ts"; const app = new Application(); @@ -21,6 +20,7 @@ for await (const dirEntry of Deno.readDir(Deno.cwd())) { args: [ 'run', ...perms, + '--watch', filename, port ] @@ -28,8 +28,8 @@ for await (const dirEntry of Deno.readDir(Deno.cwd())) { const prefix = Deno.readTextFileSync(prefixfile); 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.routes()); diff --git a/warstone-web-service/main.ts b/warstone-web-service/main.ts index 1d35115..5b63912 100644 --- a/warstone-web-service/main.ts +++ b/warstone-web-service/main.ts @@ -2,21 +2,20 @@ import { CGGService } from 'cgg/Application.ts'; import { Router, send } from 'oak'; -const app = new CGGService({ prefix: '/' }); +const prefix = '' +const app = new CGGService({ prefix: `/${prefix}` }); // app.route(new Router() // .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) => { - if (!ctx.request.url.pathname.startsWith(ROOT_DIR_PATH)) { - next(); - return; - } - const filePath = ctx.request.url.pathname.replace(ROOT_DIR_PATH, ""); +app.use(async (ctx) => { + if (ctx.request.url.pathname.includes('favicon')) return ctx.response.status = 404; + const filePath = ctx.request.url.pathname.replace(prefix, ""); await send(ctx, filePath, { root: ROOT_DIR, + index: 'index.html' }); });