Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move wisp to plugin, remove build rq for running api #302

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 32 additions & 34 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,46 @@ import tailwind from "@astrojs/tailwind";
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
import { scramjetPath } from "@mercuryworkshop/scramjet";
import playformCompress from "@playform/compress";
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import { scramjetPath } from "@mercuryworkshop/scramjet";
import icon from "astro-icon";
import { defineConfig, envField } from "astro/config";
import { viteStaticCopy } from "vite-plugin-static-copy";
import { version } from "./package.json";
import { parsedDoc } from "./server/config.js";
import { wispPlugin } from "./server/vite-plugin-wisp";
const workerwarePath = fileURLToPath(new URL("./workerware/src", import.meta.url));

export default defineConfig({
site: parsedDoc.seo.enabled ? parsedDoc.seo.domain || process.env.SITE : 'http://localhost:4321',
env: {
schema: {
VERSION: envField.string({
context: "client",
access: "public",
optional: true,
default: version
}),
MARKETPLACE_ENABLED: envField.boolean({
context: "client",
access: "public",
optional: true,
default: parsedDoc.marketplace.enabled
}),
SEO: envField.string({
context: "client",
access: "public",
optional: true,
default: JSON.stringify({
enabled: parsedDoc.seo.enabled,
domain: new URL(parsedDoc.seo.domain).host
})
site: parsedDoc.seo.enabled
? parsedDoc.seo.domain || process.env.SITE
: "http://localhost:4321",
env: {
schema: {
VERSION: envField.string({
context: "client",
access: "public",
optional: true,
default: version
}),
MARKETPLACE_ENABLED: envField.boolean({
context: "client",
access: "public",
optional: true,
default: parsedDoc.marketplace.enabled
}),
SEO: envField.string({
context: "client",
access: "public",
optional: true,
default: JSON.stringify({
enabled: parsedDoc.seo.enabled,
domain: new URL(parsedDoc.seo.domain).host
})
}
},
})
}
},
integrations: [
tailwind(),
//sitemap(),
Expand Down Expand Up @@ -77,7 +80,7 @@ export default defineConfig({
{
src: `${scramjetPath}/**/*`.replace(/\\/g, "/"),
dest: "scram",
overwrite: false
overwrite: false
},
{
src: `${baremuxPath}/**/*`.replace(/\\/g, "/"),
Expand All @@ -90,7 +93,8 @@ export default defineConfig({
overwrite: false
}
]
})
}),
wispPlugin
],
server: {
proxy: {
Expand All @@ -113,12 +117,6 @@ export default defineConfig({
target: "http://localhost:8080",
changeOrigin: true
},
"/wisp/": {
target: "ws://localhost:8080/wisp/",
changeOrigin: true,
ws: true,
rewrite: (path) => path.replace(/^\/wisp\//, "")
},
"/styles": {
target: "http://localhost:8080",
changeOrigin: true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"scripts": {
"dev": "astro dev --host 0.0.0.0 & tsx --watch server/server.ts",
"start": "node server/server.js",
"start": "NODE_ENV=production node server/server.js",
"build:server": "tsc -p server",
"build:client": "astro check && astro build",
"build": "npm run build:server & npm run build:client",
Expand Down
82 changes: 41 additions & 41 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ import fastifyStatic from "@fastify/static";
import chalk from "chalk";
import Fastify, { FastifyReply, FastifyRequest } from "fastify";
import gradient from "gradient-string";
//@ts-ignore WHY would I want this typechecked AT ALL
import { handler as ssrHandler } from "../dist/server/entry.mjs";
import { parsedDoc } from "./config.js";
import { setupDB } from "./dbSetup.js";
import { catalogAssets, marketplaceAPI } from "./marketplace.js";
import { serverFactory } from "./serverFactory.js";

const app = Fastify({
logger: parsedDoc.server.server.logging,
ignoreDuplicateSlashes: true,
ignoreTrailingSlash: true,
serverFactory: serverFactory
logger: parsedDoc.server.server.logging,
ignoreDuplicateSlashes: true,
ignoreTrailingSlash: true,
serverFactory: serverFactory
});

await app.register(fastifyCompress, {
encodings: ["br", "gzip", "deflate"]
encodings: ["br", "gzip", "deflate"]
});

await app.register(fastifyMultipart, {
Expand All @@ -36,58 +34,60 @@ await app.register(fastifyMultipart, {
});

await app.register(fastifyHelmet, {
xPoweredBy: false,
crossOriginEmbedderPolicy: true,
crossOriginOpenerPolicy: true,
contentSecurityPolicy: false //Disabled because astro DOES NOT LIKE IT
xPoweredBy: false,
crossOriginEmbedderPolicy: true,
crossOriginOpenerPolicy: true,
contentSecurityPolicy: false //Disabled because astro DOES NOT LIKE IT
});

await app.register(fastifyStatic, {
root: fileURLToPath(new URL("../dist/client", import.meta.url))
root: fileURLToPath(new URL("../dist/client", import.meta.url))
});

//Our marketplace API. Not middleware as I don't want to deal with that LOL. Just a function that passes our app to it.
if (parsedDoc.marketplace.enabled) {
await app.register(fastifyStatic, {
root: fileURLToPath(new URL("../database_assets", import.meta.url)),
prefix: "/packages/",
decorateReply: false
});
marketplaceAPI(app);
await app.register(fastifyStatic, {
root: fileURLToPath(new URL("../database_assets", import.meta.url)),
prefix: "/packages/",
decorateReply: false
});
marketplaceAPI(app);
}

await app.register(fastifyMiddie);

app.use(ssrHandler);

if (process.env.NODE_ENV === "production") {
//@ts-ignore WHY would I want this typechecked AT ALL
const { handler: ssrHandler } = await import("../dist/server/entry.mjs")
app.use(ssrHandler);
}
const port: number =
parseInt(process.env.PORT as string) || parsedDoc.server.server.port || parseInt("8080");
const titleText = `
_ _ _ _ ____ _
| \\ | | ___| |__ _ _| | __ _ / ___| ___ _ ____ _(_) ___ ___ ___
parseInt(process.env.PORT as string) || parsedDoc.server.server.port || parseInt("8080");
const titleText = `
_ _ _ _ ____ _
| \\ | | ___| |__ _ _| | __ _ / ___| ___ _ ____ _(_) ___ ___ ___
| \\| |/ _ \\ '_ \\| | | | |/ _' | \\___ \\ / _ \\ '__\\ \\ / / |/ __/ _ \\/ __|
| |\\ | __/ |_) | |_| | | (_| | ___) | __/ | \\ V /| | (_| __/\\__ \\
|_| \\_|\\___|_.__/ \\__,_|_|\\__,_| |____/ \\___|_| \\_/ |_|\\___\\___||___/
`;
const titleColors = {
purple: "#7967dd",
pink: "#eb6f92"
purple: "#7967dd",
pink: "#eb6f92"
};

console.log(gradient(Object.values(titleColors)).multiline(titleText as string));
app.listen({ port: port, host: "0.0.0.0" }).then(async () => {
console.log(
chalk.hex("#7967dd")(
`Server listening on ${chalk.hex("#eb6f92").bold("http://localhost:" + port + "/")}`
)
);
console.log(
chalk.hex("#7967dd")(
`Server also listening on ${chalk.hex("#eb6f92").bold("http://0.0.0.0:" + port + "/")}`
)
);
if (parsedDoc.marketplace.enabled) {
await catalogAssets.sync();
await setupDB(catalogAssets);
}
console.log(
chalk.hex("#7967dd")(
`Server listening on ${chalk.hex("#eb6f92").bold("http://localhost:" + port + "/")}`
)
);
console.log(
chalk.hex("#7967dd")(
`Server also listening on ${chalk.hex("#eb6f92").bold("http://0.0.0.0:" + port + "/")}`
)
);
if (parsedDoc.marketplace.enabled) {
await catalogAssets.sync();
await setupDB(catalogAssets);
}
});
2 changes: 1 addition & 1 deletion server/serverFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ const serverFactory: FastifyServerFactory = (
return httpServer;
};

export { serverFactory };
export { serverFactory, wispOptions };
15 changes: 15 additions & 0 deletions server/vite-plugin-wisp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Socket } from "node:net";
import wisp from "wisp-server-node";
import { wispOptions } from "./serverFactory";

export const wispPlugin = {
name: "vite-plugin-wisp",
configureServer(server) {
server.wisp = wisp;
server.httpServer?.on("upgrade", (req, socket: Socket, head) =>
req.url?.endsWith("/wisp/")
? wisp.routeRequest(req, socket, head, wispOptions)
: undefined
);
}
};