Initial commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { join } from 'path'
|
||||
import express from 'express'
|
||||
import { createServer as createViteServer } from 'vite'
|
||||
import { readFile } from 'fs/promises'
|
||||
|
||||
import { getBuildRoutes } from './routes.js'
|
||||
|
||||
async function createServer() {
|
||||
const app = express()
|
||||
|
||||
// In middleware mode, if you want to use Vite's own HTML serving logic
|
||||
// use `'html'` as the `middlewareMode` (ref https://vitejs.dev/config/#server-middlewaremode)
|
||||
const vite = await createViteServer({
|
||||
server: { middlewareMode: 'html' },
|
||||
})
|
||||
|
||||
const routes = await getBuildRoutes()
|
||||
|
||||
console.log(`mounting static routes...`)
|
||||
for (const [route, file] of Object.entries(routes)) {
|
||||
const filePath = join('./frontend', file)
|
||||
console.log(`- "%s" => %s`, route, filePath)
|
||||
|
||||
app.get(route, async (req, res) => {
|
||||
const htmlFile = await readFile(filePath, 'utf8')
|
||||
const htmlViteHooksFile = await vite.transformIndexHtml(req.originalUrl, htmlFile)
|
||||
|
||||
res.setHeader('Content-Type', 'text/html')
|
||||
return res.send(htmlViteHooksFile)
|
||||
})
|
||||
}
|
||||
|
||||
app.use(vite.middlewares)
|
||||
|
||||
console.log('Started dev server on port :3000')
|
||||
app.listen(3000)
|
||||
}
|
||||
|
||||
createServer()
|
||||
@@ -0,0 +1,39 @@
|
||||
import { spawn } from 'child_process'
|
||||
|
||||
function transformRoutes(entrypoints) {
|
||||
return Object.fromEntries(entrypoints.map(({ route, filename }) => [route, filename]))
|
||||
}
|
||||
|
||||
export async function getBuildRoutes() {
|
||||
// Thanks to ChatGPT
|
||||
function readCommandOutputAsJSON(command) {
|
||||
const [cmd, ...args] = command.split(' ')
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(cmd, args)
|
||||
|
||||
let stdout = ''
|
||||
|
||||
child.stdout.on('data', data => {
|
||||
stdout += data.toString()
|
||||
})
|
||||
|
||||
child.on('close', code => {
|
||||
if (code !== 0) {
|
||||
reject(`Command ${cmd} ${args.join(' ')} failed with code ${code}`)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const output = JSON.parse(stdout)
|
||||
resolve(output)
|
||||
} catch (e) {
|
||||
reject(`Error parsing JSON output: ${e.message}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
console.log('loading build entrypoints...')
|
||||
return transformRoutes(await readCommandOutputAsJSON('go run ./meta/routes'))
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.phc.dm.unipi.it/phc/cluster-dashboard/backend/routes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
if err := enc.Encode(routes.HtmlEntrypoints); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user