Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ program
.name("@outerbase/studio")
.version("0.1.0")
.option("--port <port>", "Set port to serve", "4000")
.option("--base-path <base-path>", "Set the base path for the web application", "/")
.option("--user <username>", "Set basic authentication username")
.option("--pass <password>", "Set basic authentication password")
.option("--config <config-file>", "Launch studio using configuration file")
Expand All @@ -27,13 +28,15 @@ program
pass?: string;
user?: string;
port?: number;
basePath?: string;
config?: string;
log?: boolean;
}
) => {
let options: ServeOptions = {
driver: null,
port: flags?.port ?? 4000,
basePath: flags?.basePath ?? "/",
studio: STUDIO_PATH,
username: flags?.user,
password: flags?.pass,
Expand Down
43 changes: 25 additions & 18 deletions src/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const htmlCode = `<!doctype>
<script>
function handler(e) {
if (e.data.type !== "query" && e.data.type !== "transaction") return;
fetch("/query", {
fetch("$basePathPrefix/query", {
method: "post",
headers: {
"Content-Type": "application/json"
Expand All @@ -52,6 +52,7 @@ const htmlCode = `<!doctype>
export interface ServeOptions {
driver?: BaseDriver;
port: number;
basePath?: string;
username?: string;
password?: string;
log?: boolean;
Expand All @@ -69,11 +70,14 @@ export function serve(
driver,
studio,
port,
basePath,
username,
password,
log,
} = options

const basePathPrefix = (basePath ?? "").replace(/^\/*/, '/').replace(/\/+$/, '');

if (!driver) {
console.log("We couldn't find the right driver for this database");
return;
Expand Down Expand Up @@ -102,15 +106,16 @@ export function serve(
);
}

app.get("/", (_, res) => {
app.get(`${basePathPrefix}/`, (_, res) => {
return res.send(
htmlCode
.replace("$studio", studioFullUrl ?? "https://libsqlstudio.com/embed/sqlite")
.replace("$title", driver.connectionName())
.replace("$basePathPrefix", basePathPrefix)
);
});

app.post("/query", async (req, res) => {
app.post(`${basePathPrefix}/query`, async (req, res) => {
const body:
| { id: number; type: "query"; statement: string }
| {
Expand Down Expand Up @@ -155,22 +160,24 @@ export function serve(
});

const server = app.listen(port);
printServingMessage(port);
printServingMessage(port, basePathPrefix);

console.log("Press q | shutdown the server");
if (process.stdin.isTTY) {
console.log("Press q | shutdown the server");

process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setRawMode(true);
process.stdin.resume();

process.stdin.on("data", (buffer) => {
const c = new TextDecoder().decode(buffer);
process.stdin.on("data", (buffer) => {
const c = new TextDecoder().decode(buffer);

if (c.toUpperCase() === "Q" || buffer[0] === 3) {
console.log("Shutting down the server");
server.closeAllConnections();
process.exit();
}
});
if (c.toUpperCase() === "Q" || buffer[0] === 3) {
console.log("Shutting down the server");
server.closeAllConnections();
process.exit();
}
});
}

process.on("SIGINT", function () {
console.log("Caught interrupt signal");
Expand Down Expand Up @@ -198,11 +205,11 @@ function getIPAddress() {
return "0.0.0.0";
}

function printServingMessage(port: number) {
function printServingMessage(port: number, basePathPrefix: string) {
const text = [
"Serving!",
`- Local: http://localhost:${port}`,
`- Network: http://${getIPAddress()}:${port}`,
`- Local: http://localhost:${port}${basePathPrefix}`,
`- Network: http://${getIPAddress()}:${port}${basePathPrefix}`,
];

const paddingY = 1;
Expand Down