Skip to content

Commit 1acfb0c

Browse files
committed
fix: http-server: add '/list' endpoint, that displays a HTML page with a list of the packages
1 parent d5ac4a1 commit 1acfb0c

3 files changed

Lines changed: 54 additions & 11 deletions

File tree

apps/http-server/packages/generic/src/server.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import cors from '@koa/cors'
88
import bodyParser from 'koa-bodyparser'
99

1010
import { HTTPServerConfig, LoggerInstance, stringifyError, first } from '@sofie-package-manager/api'
11-
import { BadResponse, Storage, isBadResponse } from './storage/storage'
11+
import { BadResponse, PackageInfo, Storage, isBadResponse } from './storage/storage'
1212
import { FileStorage } from './storage/fileStorage'
1313
import { CTX, PACKAGE_JSON_VERSION, valueOrFirst } from './lib'
1414
import { parseFormData } from 'pechkin'
@@ -90,6 +90,9 @@ export class PackageProxyServer {
9090
this.router.get('/packages', async (ctx) => {
9191
await this.handleStorage(ctx, async () => this.storage.listPackages(ctx))
9292
})
93+
this.router.get('/list', async (ctx) => {
94+
await this.handleStorageHTMLList(ctx, async () => this.storage.listPackages(ctx))
95+
})
9396
this.router.get('/package/:path+', async (ctx) => {
9497
await this.handleStorage(ctx, async () => this.storage.getPackage(ctx.params.path, ctx))
9598
})
@@ -175,4 +178,50 @@ export class PackageProxyServer {
175178
ctx.body = 'Internal server error'
176179
}
177180
}
181+
private async handleStorageHTMLList(
182+
ctx: CTX,
183+
storageFcn: () => Promise<{ packages: PackageInfo[] } | BadResponse>
184+
) {
185+
try {
186+
const result = await storageFcn()
187+
if (isBadResponse(result)) {
188+
ctx.response.status = result.code
189+
ctx.body = result.reason
190+
} else {
191+
const packages = result.packages
192+
193+
ctx.set('Content-Type', 'text/html')
194+
ctx.body = `<!DOCTYPE html>
195+
<html>
196+
<head>
197+
<style>
198+
body { font-family: Arial, sans-serif; }
199+
table { border-collapse: collapse; width: 100%; }
200+
th, td { border: 1px solid #ddd; padding: 8px; }
201+
202+
</style>
203+
</head>
204+
<body>
205+
<h1>Packages</h1>
206+
<table>
207+
${packages
208+
.map(
209+
(pkg) =>
210+
`<tr>
211+
<td><a href="/package/${pkg.path}">${pkg.path}</a></td>
212+
<td>${pkg.size}</td>
213+
<td>${pkg.modified}</td>
214+
</tr>`
215+
)
216+
.join('')}
217+
</table>
218+
</body>
219+
</html>`
220+
}
221+
} catch (err) {
222+
this.logger.error(`Error in handleStorage: ${stringifyError(err)} `)
223+
ctx.response.status = 500
224+
ctx.body = 'Internal server error'
225+
}
226+
}
178227
}

apps/http-server/packages/generic/src/storage/fileStorage.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,7 @@ export class FileStorage extends Storage {
4646
await fsMkDir(this._basePath, { recursive: true })
4747
}
4848

49-
async listPackages(ctx: CTX): Promise<true | BadResponse> {
50-
type PackageInfo = {
51-
path: string
52-
size: string
53-
modified: string
54-
}
49+
async listPackages(ctx: CTX): Promise<{ packages: PackageInfo[] } | BadResponse> {
5550
const packages: PackageInfo[] = []
5651

5752
const getAllFiles = async (basePath: string, dirPath: string) => {
@@ -84,9 +79,8 @@ export class FileStorage extends Storage {
8479
return 0
8580
})
8681

87-
ctx.body = { packages: packages }
88-
89-
return true
82+
ctx.body = { packages }
83+
return { packages }
9084
}
9185
private async getFileInfo(paramPath: string): Promise<
9286
| {

apps/http-server/packages/generic/src/storage/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CTX, CTXPost } from '../lib'
44
export abstract class Storage {
55
abstract init(): Promise<void>
66
abstract getInfo(): string
7-
abstract listPackages(ctx: CTX): Promise<true | BadResponse>
7+
abstract listPackages(ctx: CTX): Promise<{ packages: PackageInfo[] } | BadResponse>
88
abstract headPackage(path: string, ctx: CTX): Promise<true | BadResponse>
99
abstract getPackage(path: string, ctx: CTX): Promise<true | BadResponse>
1010
abstract postPackage(

0 commit comments

Comments
 (0)