fix: simplified development server architecture

This commit is contained in:
2023-04-29 15:46:08 +02:00
parent a72f543346
commit 6ff903fb06
6 changed files with 15 additions and 19 deletions
+3 -3
View File
@@ -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)
+2 -7
View File
@@ -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'))
}
+16
View File
@@ -0,0 +1,16 @@
package main
import (
"encoding/json"
"go-vite-kit/backend/routes"
"log"
"os"
)
func main() {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(routes.HtmlEntrypoints); err != nil {
log.Fatal(err)
}
}