mirror of
https://github.com/coder/code-server.git
synced 2025-08-01 06:23:35 +08:00
Create initial server layout (#11)
* Create initial server layout * Adjust command name to entry * Add @oclif/config as dependency * Implement build process for outputting single binary * Add init message * Remove unused import, add tsconfig.json to .gitignore * Accidently pushed wacky change to output host FS files * Add options to createApp
This commit is contained in:
46
packages/server/src/server.ts
Normal file
46
packages/server/src/server.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { ReadWriteConnection } from "@coder/protocol";
|
||||
import { Server, ServerOptions } from "@coder/protocol/src/node/server";
|
||||
import * as express from "express";
|
||||
import * as http from "http";
|
||||
import * as ws from "ws";
|
||||
|
||||
export const createApp = (registerMiddleware?: (app: express.Application) => void, options?: ServerOptions): {
|
||||
readonly express: express.Application;
|
||||
readonly server: http.Server;
|
||||
readonly wss: ws.Server;
|
||||
} => {
|
||||
const app = express();
|
||||
if (registerMiddleware) {
|
||||
registerMiddleware(app);
|
||||
}
|
||||
const server = http.createServer(app);
|
||||
const wss = new ws.Server({ server });
|
||||
|
||||
wss.on("connection", (ws: WebSocket) => {
|
||||
const connection: ReadWriteConnection = {
|
||||
onMessage: (cb) => {
|
||||
ws.addEventListener("message", (event) => cb(event.data));
|
||||
},
|
||||
close: () => ws.close(),
|
||||
send: (data) => ws.send(data),
|
||||
onClose: (cb) => ws.addEventListener("close", () => cb()),
|
||||
};
|
||||
|
||||
const server = new Server(connection, options);
|
||||
});
|
||||
|
||||
/**
|
||||
* We should static-serve the `web` package at this point
|
||||
*/
|
||||
app.get("/", (req, res, next) => {
|
||||
res.write("Example! :)");
|
||||
res.status(200);
|
||||
res.end();
|
||||
});
|
||||
|
||||
return {
|
||||
express: app,
|
||||
server,
|
||||
wss,
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user