From 6ff903fb0638cc25cd99d12c9cbb30be366f6949 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Sat, 29 Apr 2023 15:46:08 +0200 Subject: [PATCH] fix: simplified development server architecture --- README.md | 2 +- cmd/server/main.go | 4 ---- meta/dev-server.js | 6 +++--- meta/routes.js | 9 ++------- {cmd => meta}/routes/main.go | 0 vite.config.js | 13 +++++++++---- 6 files changed, 15 insertions(+), 19 deletions(-) rename {cmd => meta}/routes/main.go (100%) diff --git a/README.md b/README.md index aaa8532..d48d9fd 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Minimal boilerplate project for a Golang server using [Fiber](https://github.com A very important file is `backend/routes/router.go` that contains the `HtmlEntrypoints` variable that is used both by the backend and ViteJS to mount HTML entrypoints. - When building the frontend ViteJS will call `go run ./cmd/routes` to read the content of this variabile, while during development a special route called `/dev/routes` gets mounted on the backend server and this lets Vite add all necessary entrypoints to the dev server. + When building the frontend ViteJS will call `go run ./cmd/routes` to read the content of this variabile. This is also used while developing to let Vite know add all necessary entrypoints to the dev server. ## Usage diff --git a/cmd/server/main.go b/cmd/server/main.go index 64323ca..95295fd 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -44,10 +44,6 @@ func main() { app.Route("/api", router.Api) if strings.HasPrefix(config.Mode, "dev") { - app.Get("/dev/routes", func(c *fiber.Ctx) error { - return c.JSON(routes.HtmlEntrypoints) - }) - setupDevServer() } diff --git a/meta/dev-server.js b/meta/dev-server.js index 6bae1b6..667c385 100644 --- a/meta/dev-server.js +++ b/meta/dev-server.js @@ -3,7 +3,7 @@ import express from 'express' import { createServer as createViteServer } from 'vite' import { readFile } from 'fs/promises' -import { getDevelopmentRoutes } from './routes.js' +import { getBuildRoutes } from './routes.js' async function createServer() { const app = express() @@ -14,9 +14,9 @@ async function createServer() { server: { middlewareMode: 'html' }, }) - const routes = await getDevelopmentRoutes() + const routes = await getBuildRoutes() - console.log(`Mounting static routes:`) + console.log(`mounting static routes...`) for (const [route, file] of Object.entries(routes)) { const filePath = join('./frontend', file) console.log(`- "%s" => %s`, route, filePath) diff --git a/meta/routes.js b/meta/routes.js index e29be0b..e8c9c07 100644 --- a/meta/routes.js +++ b/meta/routes.js @@ -1,15 +1,9 @@ import { spawn } from 'child_process' -import axios from 'axios' function transformRoutes(entrypoints) { return Object.fromEntries(entrypoints.map(({ route, filename }) => [route, filename])) } -export async function getDevelopmentRoutes() { - const res = await axios.get('http://127.0.0.1:4000/dev/routes') - return transformRoutes(res.data) -} - export async function getBuildRoutes() { // Thanks to ChatGPT function readCommandOutputAsJSON(command) { @@ -40,5 +34,6 @@ export async function getBuildRoutes() { }) } - return transformRoutes(await readCommandOutputAsJSON('go run ./cmd/routes')) + console.log('loading build entrypoints...') + return transformRoutes(await readCommandOutputAsJSON('go run ./meta/routes')) } diff --git a/cmd/routes/main.go b/meta/routes/main.go similarity index 100% rename from cmd/routes/main.go rename to meta/routes/main.go diff --git a/vite.config.js b/vite.config.js index f5a0df8..9d5b9a9 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,12 +1,17 @@ import { defineConfig } from 'vite' -import { getBuildRoutes, getDevelopmentRoutes } from './meta/routes.js' +import { getBuildRoutes } from './meta/routes.js' import { join } from 'path' -export default defineConfig(async config => { - const routes = config.command === 'build' ? await getBuildRoutes() : await getDevelopmentRoutes() +export default defineConfig(async () => { + const routes = await getBuildRoutes() + console.log('html entrypoints:') + for (const [route, filename] of Object.entries(routes)) { + console.log(`- "${route}" => ${filename}`) + } + console.log() + const entryPoints = Object.values(routes) - console.log('Found entrypoints:', entryPoints) return { root: './frontend',