subdomain sites get priority
This commit is contained in:
parent
ff6f71daf8
commit
d5e0dbd2db
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
import { Hono } from "hono"
|
import { Hono } from "hono"
|
||||||
import { serveStatic, upgradeWebSocket, websocket } from "hono/bun"
|
import { serveStatic, upgradeWebSocket, websocket } from "hono/bun"
|
||||||
import { prettyJSON } from "hono/pretty-json"
|
|
||||||
import color from "kleur"
|
import color from "kleur"
|
||||||
|
|
||||||
import type { Message } from "./shared/types"
|
import type { Message } from "./shared/types"
|
||||||
|
|
@ -34,13 +33,7 @@ import { initCommands } from "./commands"
|
||||||
|
|
||||||
const app = new Hono()
|
const app = new Hono()
|
||||||
|
|
||||||
app.use("*", prettyJSON())
|
// console logging
|
||||||
|
|
||||||
app.use('/*', serveStatic({ root: './public' }))
|
|
||||||
app.use('/img/*', serveStatic({ root: './public' }))
|
|
||||||
app.use('/vendor/*', serveStatic({ root: './public' }))
|
|
||||||
app.use('/css/*', serveStatic({ root: './src' }))
|
|
||||||
|
|
||||||
app.use("*", async (c, next) => {
|
app.use("*", async (c, next) => {
|
||||||
const start = Date.now()
|
const start = Date.now()
|
||||||
await next()
|
await next()
|
||||||
|
|
@ -49,6 +42,31 @@ app.use("*", async (c, next) => {
|
||||||
console.log(fn(`${c.res.status} ${c.req.method} ${c.req.url} (${end - start}ms)`))
|
console.log(fn(`${c.res.status} ${c.req.method} ${c.req.url} (${end - start}ms)`))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// subdomain route needs to go first
|
||||||
|
app.use("*", async (c, next) => {
|
||||||
|
const url = new URL(c.req.url)
|
||||||
|
const localhost = url.hostname.endsWith("localhost")
|
||||||
|
const domains = url.hostname.split(".")
|
||||||
|
let subdomain = ""
|
||||||
|
|
||||||
|
if (domains.length > (localhost ? 1 : 2))
|
||||||
|
subdomain = domains[0]!
|
||||||
|
|
||||||
|
if (subdomain) {
|
||||||
|
const app = serveApp(c, subdomain)
|
||||||
|
return await app
|
||||||
|
}
|
||||||
|
|
||||||
|
return next()
|
||||||
|
})
|
||||||
|
|
||||||
|
// static files
|
||||||
|
app.use('/*', serveStatic({ root: './public' }))
|
||||||
|
app.use('/img/*', serveStatic({ root: './public' }))
|
||||||
|
app.use('/vendor/*', serveStatic({ root: './public' }))
|
||||||
|
app.use('/css/*', serveStatic({ root: './src' }))
|
||||||
|
|
||||||
|
// blue screen of death error page if NOSE_DIR isn't found
|
||||||
app.use("*", async (c, next) => {
|
app.use("*", async (c, next) => {
|
||||||
const error = fatal ? fatal : !isDir(NOSE_DIR) ? `NOSE_DIR doesn't exist: ${NOSE_DIR}` : undefined
|
const error = fatal ? fatal : !isDir(NOSE_DIR) ? `NOSE_DIR doesn't exist: ${NOSE_DIR}` : undefined
|
||||||
|
|
||||||
|
|
@ -59,6 +77,8 @@ app.use("*", async (c, next) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// only transpile browser JS on the fly in dev mode
|
||||||
|
if (process.env.NODE_ENV !== "production")
|
||||||
app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => {
|
app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => {
|
||||||
let path = "./src/" + c.req.path.replace("..", ".")
|
let path = "./src/" + c.req.path.replace("..", ".")
|
||||||
|
|
||||||
|
|
@ -75,10 +95,6 @@ app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
//
|
|
||||||
// app routes
|
|
||||||
//
|
|
||||||
|
|
||||||
// inject browser-nav.js into NOSE apps when loaded in a NOSE iframe
|
// inject browser-nav.js into NOSE apps when loaded in a NOSE iframe
|
||||||
app.use("*", async (c, next) => {
|
app.use("*", async (c, next) => {
|
||||||
await next()
|
await next()
|
||||||
|
|
@ -112,23 +128,7 @@ app.use("*", async (c, next) => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use("*", async (c, next) => {
|
// source code of commands - used to load and eval() browser commands
|
||||||
const url = new URL(c.req.url)
|
|
||||||
const localhost = url.hostname.endsWith("localhost")
|
|
||||||
const domains = url.hostname.split(".")
|
|
||||||
let subdomain = ""
|
|
||||||
|
|
||||||
if (domains.length > (localhost ? 1 : 2))
|
|
||||||
subdomain = domains[0]!
|
|
||||||
|
|
||||||
if (subdomain) {
|
|
||||||
const app = serveApp(c, subdomain)
|
|
||||||
return await app
|
|
||||||
}
|
|
||||||
|
|
||||||
return next()
|
|
||||||
})
|
|
||||||
|
|
||||||
app.get("/source/:name", async c => {
|
app.get("/source/:name", async c => {
|
||||||
const name = c.req.param("name")
|
const name = c.req.param("name")
|
||||||
const sessionId = c.req.query("session") || "0"
|
const sessionId = c.req.query("session") || "0"
|
||||||
|
|
@ -142,6 +142,7 @@ app.get("/source/:name", async c => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// call a command's GET or POST function, if one exists
|
||||||
app.on(["GET", "POST"], ["/cmd/:name"], async c => {
|
app.on(["GET", "POST"], ["/cmd/:name"], async c => {
|
||||||
const sessionId = c.req.header("X-Session") || "0"
|
const sessionId = c.req.header("X-Session") || "0"
|
||||||
const cmd = c.req.param("name")
|
const cmd = c.req.param("name")
|
||||||
|
|
@ -158,12 +159,10 @@ app.on(["GET", "POST"], ["/cmd/:name"], async c => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// the terminal
|
||||||
app.get("/", c => c.html(<Layout><Terminal /></Layout>))
|
app.get("/", c => c.html(<Layout><Terminal /></Layout>))
|
||||||
|
|
||||||
//
|
// the main websocket
|
||||||
// websocket
|
|
||||||
//
|
|
||||||
|
|
||||||
app.get("/ws", c => {
|
app.get("/ws", c => {
|
||||||
const _sessionId = c.req.query("session")
|
const _sessionId = c.req.query("session")
|
||||||
const url = new URL(c.req.url)
|
const url = new URL(c.req.url)
|
||||||
|
|
@ -229,16 +228,17 @@ if (process.env.BUN_HOT) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// production mode
|
// server start
|
||||||
//
|
//
|
||||||
|
|
||||||
if (process.env.NODE_ENV === "production") {
|
if (process.env.NODE_ENV === "production") {
|
||||||
initDNS()
|
initDNS()
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
await initNoseDir()
|
||||||
// server start
|
initCommands()
|
||||||
//
|
initWebapps()
|
||||||
|
initSneakers()
|
||||||
|
|
||||||
console.log(color.cyan(NOSE_ICON))
|
console.log(color.cyan(NOSE_ICON))
|
||||||
console.log(color.blue(" NOSE_BIN:"), color.yellow(tilde(NOSE_BIN)))
|
console.log(color.blue(" NOSE_BIN:"), color.yellow(tilde(NOSE_BIN)))
|
||||||
|
|
@ -246,11 +246,6 @@ console.log(color.blue(" NOSE_DATA:"), color.yellow(tilde(NOSE_DATA)))
|
||||||
console.log(color.blue(" NOSE_DIR:"), color.yellow(tilde(NOSE_DIR)))
|
console.log(color.blue(" NOSE_DIR:"), color.yellow(tilde(NOSE_DIR)))
|
||||||
console.log(color.blue("NOSE_ROOT_BIN:"), color.yellow(tilde(NOSE_ROOT_BIN)))
|
console.log(color.blue("NOSE_ROOT_BIN:"), color.yellow(tilde(NOSE_ROOT_BIN)))
|
||||||
|
|
||||||
await initNoseDir()
|
|
||||||
initCommands()
|
|
||||||
initWebapps()
|
|
||||||
initSneakers()
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
port: process.env.PORT || 3000,
|
port: process.env.PORT || 3000,
|
||||||
hostname: "0.0.0.0",
|
hostname: "0.0.0.0",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user