Skip to content

Commit e90c633

Browse files
committed
Add local route handler.
1 parent 97bd72d commit e90c633

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

src/node/cli.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface Args extends VsArgs {
5353
"new-window"?: boolean
5454

5555
link?: OptionalString
56+
"local-directory"?: OptionalString
5657
}
5758

5859
interface Option<T> {
@@ -179,6 +180,10 @@ const options: Options<Required<Args>> = {
179180
`,
180181
beta: true,
181182
},
183+
"local-directory": {
184+
type: OptionalString,
185+
description: "Serve files from a given directory",
186+
},
182187
}
183188

184189
export const optionDescriptions = (): string[] => {

src/node/routes/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { plural } from "../../common/util"
1212
import { AuthType, DefaultedArgs } from "../cli"
1313
import { rootPath } from "../constants"
1414
import { Heart } from "../heart"
15-
import { commonTemplateVars } from "../http"
15+
import { commonTemplateVars, ensureAuthenticated } from "../http"
1616
import { loadPlugins } from "../plugin"
1717
import * as domainProxy from "../proxy"
1818
import { getMediaMime, paths } from "../util"
@@ -118,6 +118,16 @@ export const register = async (
118118

119119
app.use("/", _static.router)
120120

121+
if (args["local-directory"]?.value) {
122+
const directoryPath = path.resolve(args["local-directory"].value)
123+
logger.info(`Serving files from local directory “${directoryPath}” at “/local”`)
124+
125+
const localDirectoryRouter = express.Router()
126+
_static.createServeDirectoryHandler(localDirectoryRouter, "/local", directoryPath)
127+
128+
app.use(ensureAuthenticated, localDirectoryRouter)
129+
}
130+
121131
app.use("/update", update.router)
122132

123133
await loadPlugins(app, args)

src/node/routes/static.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1-
import { Router, static as createStaticRouteHandler } from "express"
1+
import { Router, static as serveStatic } from "express"
22
import { resolve } from "path"
3+
import { ServeStaticOptions } from "serve-static"
34
import { commit, rootPath } from "../constants"
45

56
export const router = Router()
67

8+
export const createDefaultStaticRouteHandlerOptions = (): ServeStaticOptions => ({
9+
cacheControl: commit !== "development",
10+
})
11+
12+
export const createServeDirectoryHandler = (
13+
router: Router,
14+
publicRoute: string,
15+
directoryPath: string,
16+
options: ServeStaticOptions = {},
17+
): void => {
18+
router.use(
19+
publicRoute,
20+
serveStatic(directoryPath, {
21+
...createDefaultStaticRouteHandlerOptions(),
22+
...options,
23+
}),
24+
)
25+
}
26+
727
const staticPaths: Record<string, string> = {
828
"/static": "dist",
929
"/static/media": "src/browser/public/media",
1030
}
1131

1232
for (const [publicRoute, sourcePath] of Object.entries(staticPaths)) {
13-
router.use(
14-
publicRoute,
15-
createStaticRouteHandler(resolve(rootPath, sourcePath), {
16-
index: false,
17-
cacheControl: commit !== "development",
18-
}),
19-
)
33+
createServeDirectoryHandler(router, publicRoute, resolve(rootPath, sourcePath), { index: false })
2034
}

src/node/routes/vscode.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { field, logger } from "@coder/logger"
22
import * as crypto from "crypto"
3-
import { RequestHandler, Router, static as createStaticRouteHandler } from "express"
3+
import { RequestHandler, Router } from "express"
44
import { promises as fs } from "fs"
55
import * as path from "path"
66
import { resolve } from "path"
@@ -15,6 +15,7 @@ import { authenticated, commonTemplateVars, ensureAuthenticated, redirect } from
1515
import { getMediaMime, pathToFsPath } from "../util"
1616
import { VscodeProvider } from "../vscode"
1717
import { Router as WsRouter } from "../wsRouter"
18+
import { createServeDirectoryHandler } from "./static"
1819

1920
export const router = Router()
2021

@@ -103,13 +104,8 @@ wsRouter.ws("/", ensureAuthenticated, async (req) => {
103104
)
104105
await vscode.sendWebsocket(req.ws, req.query)
105106
})
106-
router.use(
107-
"/lib",
108-
createStaticRouteHandler(resolve(rootPath, "lib"), {
109-
index: false,
110-
cacheControl: commit !== "development",
111-
}),
112-
)
107+
108+
createServeDirectoryHandler(router, "/lib", resolve(rootPath, "lib"))
113109

114110
interface TarHandlerQueryParams extends ParsedQs {
115111
filePath?: string | string[]

0 commit comments

Comments
 (0)