@@ -2,13 +2,13 @@ import { logger } from "@coder/logger"
2
2
import cookieParser from "cookie-parser"
3
3
import * as express from "express"
4
4
import { promises as fs } from "fs"
5
- import http from "http"
6
5
import * as path from "path"
7
6
import * as tls from "tls"
8
7
import * as pluginapi from "../../../typings/pluginapi"
9
8
import { Disposable } from "../../common/emitter"
10
9
import { HttpCode , HttpError } from "../../common/http"
11
10
import { plural } from "../../common/util"
11
+ import { App } from "../app"
12
12
import { AuthType , DefaultedArgs } from "../cli"
13
13
import { commit , isDevMode , rootPath } from "../constants"
14
14
import { Heart } from "../heart"
@@ -28,15 +28,10 @@ import { createVSServerRouter, VSServerResult } from "./vscode"
28
28
/**
29
29
* Register all routes and middleware.
30
30
*/
31
- export const register = async (
32
- app : express . Express ,
33
- wsApp : express . Express ,
34
- server : http . Server ,
35
- args : DefaultedArgs ,
36
- ) : Promise < Disposable [ "dispose" ] > => {
31
+ export const register = async ( app : App , args : DefaultedArgs ) : Promise < Disposable [ "dispose" ] > => {
37
32
const heart = new Heart ( path . join ( paths . data , "heartbeat" ) , async ( ) => {
38
33
return new Promise ( ( resolve , reject ) => {
39
- server . getConnections ( ( error , count ) => {
34
+ app . server . getConnections ( ( error , count ) => {
40
35
if ( error ) {
41
36
return reject ( error )
42
37
}
@@ -46,11 +41,11 @@ export const register = async (
46
41
} )
47
42
} )
48
43
49
- app . disable ( "x-powered-by" )
50
- wsApp . disable ( "x-powered-by" )
44
+ app . router . disable ( "x-powered-by" )
45
+ app . wsRouter . disable ( "x-powered-by" )
51
46
52
- app . use ( cookieParser ( ) )
53
- wsApp . use ( cookieParser ( ) )
47
+ app . router . use ( cookieParser ( ) )
48
+ app . wsRouter . use ( cookieParser ( ) )
54
49
55
50
const common : express . RequestHandler = ( req , _ , next ) => {
56
51
// /healthz|/healthz/ needs to be excluded otherwise health checks will make
@@ -66,10 +61,10 @@ export const register = async (
66
61
next ( )
67
62
}
68
63
69
- app . use ( common )
70
- wsApp . use ( common )
64
+ app . router . use ( common )
65
+ app . wsRouter . use ( common )
71
66
72
- app . use ( async ( req , res , next ) => {
67
+ app . router . use ( async ( req , res , next ) => {
73
68
// If we're handling TLS ensure all requests are redirected to HTTPS.
74
69
// TODO: This does *NOT* work if you have a base path since to specify the
75
70
// protocol we need to specify the whole path.
@@ -87,24 +82,24 @@ export const register = async (
87
82
next ( )
88
83
} )
89
84
90
- app . use ( "/" , domainProxy . router )
91
- wsApp . use ( "/" , domainProxy . wsRouter . router )
85
+ app . router . use ( "/" , domainProxy . router )
86
+ app . wsRouter . use ( "/" , domainProxy . wsRouter . router )
92
87
93
- app . all ( "/proxy/(:port)(/*)?" , ( req , res ) => {
88
+ app . router . all ( "/proxy/(:port)(/*)?" , ( req , res ) => {
94
89
pathProxy . proxy ( req , res )
95
90
} )
96
- wsApp . get ( "/proxy/(:port)(/*)?" , async ( req ) => {
91
+ app . wsRouter . get ( "/proxy/(:port)(/*)?" , async ( req ) => {
97
92
await pathProxy . wsProxy ( req as pluginapi . WebsocketRequest )
98
93
} )
99
94
// These two routes pass through the path directly.
100
95
// So the proxied app must be aware it is running
101
96
// under /absproxy/<someport>/
102
- app . all ( "/absproxy/(:port)(/*)?" , ( req , res ) => {
97
+ app . router . all ( "/absproxy/(:port)(/*)?" , ( req , res ) => {
103
98
pathProxy . proxy ( req , res , {
104
99
passthroughPath : true ,
105
100
} )
106
101
} )
107
- wsApp . get ( "/absproxy/(:port)(/*)?" , async ( req ) => {
102
+ app . wsRouter . get ( "/absproxy/(:port)(/*)?" , async ( req ) => {
108
103
await pathProxy . wsProxy ( req as pluginapi . WebsocketRequest , {
109
104
passthroughPath : true ,
110
105
} )
@@ -115,40 +110,40 @@ export const register = async (
115
110
const workingDir = args . _ && args . _ . length > 0 ? path . resolve ( args . _ [ args . _ . length - 1 ] ) : undefined
116
111
pluginApi = new PluginAPI ( logger , process . env . CS_PLUGIN , process . env . CS_PLUGIN_PATH , workingDir )
117
112
await pluginApi . loadPlugins ( )
118
- pluginApi . mount ( app , wsApp )
119
- app . use ( "/api/applications" , ensureAuthenticated , apps . router ( pluginApi ) )
113
+ pluginApi . mount ( app . router , app . wsRouter )
114
+ app . router . use ( "/api/applications" , ensureAuthenticated , apps . router ( pluginApi ) )
120
115
}
121
116
122
- app . use ( express . json ( ) )
123
- app . use ( express . urlencoded ( { extended : true } ) )
117
+ app . router . use ( express . json ( ) )
118
+ app . router . use ( express . urlencoded ( { extended : true } ) )
124
119
125
- app . use (
120
+ app . router . use (
126
121
"/_static" ,
127
122
express . static ( rootPath , {
128
123
cacheControl : commit !== "development" ,
129
124
} ) ,
130
125
)
131
126
132
- app . use ( "/healthz" , health . router )
133
- wsApp . use ( "/healthz" , health . wsRouter . router )
127
+ app . router . use ( "/healthz" , health . router )
128
+ app . wsRouter . use ( "/healthz" , health . wsRouter . router )
134
129
135
130
if ( args . auth === AuthType . Password ) {
136
- app . use ( "/login" , login . router )
137
- app . use ( "/logout" , logout . router )
131
+ app . router . use ( "/login" , login . router )
132
+ app . router . use ( "/logout" , logout . router )
138
133
} else {
139
- app . all ( "/login" , ( req , res ) => redirect ( req , res , "/" , { } ) )
140
- app . all ( "/logout" , ( req , res ) => redirect ( req , res , "/" , { } ) )
134
+ app . router . all ( "/login" , ( req , res ) => redirect ( req , res , "/" , { } ) )
135
+ app . router . all ( "/logout" , ( req , res ) => redirect ( req , res , "/" , { } ) )
141
136
}
142
137
143
- app . use ( "/update" , update . router )
138
+ app . router . use ( "/update" , update . router )
144
139
145
140
let vscode : VSServerResult
146
141
try {
147
142
vscode = await createVSServerRouter ( args )
148
- app . use ( "/" , vscode . router )
149
- wsApp . use ( "/" , vscode . wsRouter . router )
150
- app . use ( "/vscode" , vscode . router )
151
- wsApp . use ( "/vscode" , vscode . wsRouter . router )
143
+ app . router . use ( "/" , vscode . router )
144
+ app . wsRouter . use ( "/" , vscode . wsRouter . router )
145
+ app . router . use ( "/vscode" , vscode . router )
146
+ app . wsRouter . use ( "/vscode" , vscode . wsRouter . router )
152
147
} catch ( error : any ) {
153
148
if ( isDevMode ) {
154
149
logger . warn ( error )
@@ -158,12 +153,12 @@ export const register = async (
158
153
}
159
154
}
160
155
161
- app . use ( ( ) => {
156
+ app . router . use ( ( ) => {
162
157
throw new HttpError ( "Not Found" , HttpCode . NotFound )
163
158
} )
164
159
165
- app . use ( errorHandler )
166
- wsApp . use ( wsErrorHandler )
160
+ app . router . use ( errorHandler )
161
+ app . wsRouter . use ( wsErrorHandler )
167
162
168
163
return ( ) => {
169
164
heart . dispose ( )
0 commit comments