11import { logger } from "@coder/logger"
22import bodyParser from "body-parser"
33import cookieParser from "cookie-parser"
4- import { ErrorRequestHandler , Express } from "express"
4+ import * as express from "express"
55import { promises as fs } from "fs"
66import http from "http"
77import * as path from "path"
@@ -15,6 +15,7 @@ import { replaceTemplates } from "../http"
1515import { loadPlugins } from "../plugin"
1616import * as domainProxy from "../proxy"
1717import { getMediaMime , paths } from "../util"
18+ import { WebsocketRequest } from "../wsRouter"
1819import * as health from "./health"
1920import * as login from "./login"
2021import * as proxy from "./proxy"
@@ -36,7 +37,12 @@ declare global {
3637/**
3738 * Register all routes and middleware.
3839 */
39- export const register = async ( app : Express , server : http . Server , args : DefaultedArgs ) : Promise < void > => {
40+ export const register = async (
41+ app : express . Express ,
42+ wsApp : express . Express ,
43+ server : http . Server ,
44+ args : DefaultedArgs ,
45+ ) : Promise < void > => {
4046 const heart = new Heart ( path . join ( paths . data , "heartbeat" ) , async ( ) => {
4147 return new Promise ( ( resolve , reject ) => {
4248 server . getConnections ( ( error , count ) => {
@@ -50,14 +56,28 @@ export const register = async (app: Express, server: http.Server, args: Defaulte
5056 } )
5157
5258 app . disable ( "x-powered-by" )
59+ wsApp . disable ( "x-powered-by" )
5360
5461 app . use ( cookieParser ( ) )
62+ wsApp . use ( cookieParser ( ) )
63+
5564 app . use ( bodyParser . json ( ) )
5665 app . use ( bodyParser . urlencoded ( { extended : true } ) )
5766
58- app . use ( async ( req , res , next ) => {
67+ const common : express . RequestHandler = ( req , _ , next ) => {
5968 heart . beat ( )
6069
70+ // Add common variables routes can use.
71+ req . args = args
72+ req . heart = heart
73+
74+ next ( )
75+ }
76+
77+ app . use ( common )
78+ wsApp . use ( common )
79+
80+ app . use ( async ( req , res , next ) => {
6181 // If we're handling TLS ensure all requests are redirected to HTTPS.
6282 // TODO: This does *NOT* work if you have a base path since to specify the
6383 // protocol we need to specify the whole path.
@@ -72,31 +92,36 @@ export const register = async (app: Express, server: http.Server, args: Defaulte
7292 return res . send ( await fs . readFile ( resourcePath ) )
7393 }
7494
75- // Add common variables routes can use.
76- req . args = args
77- req . heart = heart
78-
79- return next ( )
95+ next ( )
8096 } )
8197
8298 app . use ( "/" , domainProxy . router )
99+ wsApp . use ( "/" , domainProxy . wsRouter . router )
100+
83101 app . use ( "/" , vscode . router )
102+ wsApp . use ( "/" , vscode . wsRouter . router )
103+ app . use ( "/vscode" , vscode . router )
104+ wsApp . use ( "/vscode" , vscode . wsRouter . router )
105+
84106 app . use ( "/healthz" , health . router )
107+
85108 if ( args . auth === AuthType . Password ) {
86109 app . use ( "/login" , login . router )
87110 }
111+
88112 app . use ( "/proxy" , proxy . router )
113+ wsApp . use ( "/proxy" , proxy . wsRouter . router )
114+
89115 app . use ( "/static" , _static . router )
90116 app . use ( "/update" , update . router )
91- app . use ( "/vscode" , vscode . router )
92117
93118 await loadPlugins ( app , args )
94119
95120 app . use ( ( ) => {
96121 throw new HttpError ( "Not Found" , HttpCode . NotFound )
97122 } )
98123
99- const errorHandler : ErrorRequestHandler = async ( err , req , res , next ) => {
124+ const errorHandler : express . ErrorRequestHandler = async ( err , req , res , next ) => {
100125 const resourcePath = path . resolve ( rootPath , "src/browser/pages/error.html" )
101126 res . set ( "Content-Type" , getMediaMime ( resourcePath ) )
102127 try {
@@ -117,4 +142,11 @@ export const register = async (app: Express, server: http.Server, args: Defaulte
117142 }
118143
119144 app . use ( errorHandler )
145+
146+ const wsErrorHandler : express . ErrorRequestHandler = async ( err , req ) => {
147+ logger . error ( `${ err . message } ${ err . stack } ` )
148+ ; ( req as WebsocketRequest ) . ws . destroy ( err )
149+ }
150+
151+ wsApp . use ( wsErrorHandler )
120152}
0 commit comments