Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from crit-tech/auto-connect
Browse files Browse the repository at this point in the history
Add /api/connect endpoint and event callback
  • Loading branch information
dprothero authored Jun 25, 2023
2 parents cd9b0d5 + ca50993 commit 47f9028
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
58 changes: 48 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gms-notebook-file-server",
"version": "1.2.0",
"version": "1.3.0",
"description": "Local file server for GMsNotebook.com",
"main": "dist/server.js",
"type": "module",
Expand All @@ -16,6 +16,7 @@
"author": "David Prothero",
"license": "MIT",
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
Expand Down
23 changes: 22 additions & 1 deletion server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express, { Express, Request, Response } from "express";
import bodyParser from "body-parser";
import cors from "cors";
import fs from "fs";
import path from "path";
Expand All @@ -10,11 +11,17 @@ import { getFileType } from "./filetypes.js";
import { resolveFilePath, pathExists } from "./utils.js";

export type Server = ReturnType<typeof express.application.listen>;
export type Event = {
type: string;
payload: any;
};
export type EventCallback = (event: Event) => void;

export function startServer(
port: number,
folder: string,
workFolder: string
workFolder: string,
onEvent?: EventCallback
): Server {
const app: Express = express();
const upload = multer({ dest: path.join(workFolder, "uploads") });
Expand Down Expand Up @@ -234,6 +241,20 @@ export function startServer(
}
});

app.post(
"/api/connect",
bodyParser.json(),
async (req: Request, res: Response) => {
if (onEvent) {
onEvent({ type: "connect", payload: req.body });
return res.json({ success: true });
}
res
.status(404)
.json({ success: false, message: "No listener to accept event." });
}
);

const server = app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});
Expand Down

0 comments on commit 47f9028

Please sign in to comment.