diff --git a/src/http.ts b/src/http.ts index 882ba0a..d4f7710 100644 --- a/src/http.ts +++ b/src/http.ts @@ -8,15 +8,42 @@ export default class HttpServer { private httpServer: http.Server; private port: number; private root: string; - public constructor(port: number, root: string, tune: (ch: string, adp?: number) => void) { + public constructor(port: number, root: string, tune: (ch: string, adp?: number) => void, getChannels: ()=>string[]) { this.port = port; this.root = root; this.httpServer = http.createServer((req, res) => { let status: number = 404; - let body: any; + let body: any = ""; const url = new URL(req.url, `http://${req.headers.host}`); const pathname = path.normalize(url.pathname); - if (req.method === 'GET') { + + if (pathname.startsWith('/api/')) { + const query = pathname.split('/'); + const api = query[2]; + switch (req.method) { + case "GET": + switch (api) { + case "list": + body = JSON.stringify(getChannels()); + status = 200; + break; + + } + break; + case "PUT": + switch (api) { + case "tune": + const channel = query[3]; + const adapter = parseInt(url.searchParams.get('adapter')); + tune(channel, adapter); + status = 202; + break; + + } + + } + } + else if (req.method === 'GET') { const filePath = path.join(root, path.extname(pathname) === '' ? pathname + "/index.html" : pathname); try { body = fs.readFileSync(filePath); @@ -25,21 +52,6 @@ export default class HttpServer { catch (err) { body = "Invalid File" } - - } - else if (req.method === 'PUT' && pathname.startsWith('/api/')) { - const query = pathname.split('/'); - const api = query[2]; - switch (api) { - case "channel": - const channel = query[3]; - const adapter = parseInt(url.searchParams.get('adapter')); - tune(channel, adapter); - break; - default: - body = "Invalid API Endpoint" - break; - } } else { body = "Invalid Request" diff --git a/src/server.ts b/src/server.ts index 0582ac4..1bb12a8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -22,7 +22,10 @@ const tune = (reqChannel: string, reqAdapter?: number) => { }); } -const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, tune); +const getChannels = () => + zap.getChannels(); + +const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, tune, getChannels); const tvWebSocket = new TVWebSocket(WS_PORT); httpServer.start(); diff --git a/src/zap.ts b/src/zap.ts index d912b73..686ebad 100644 --- a/src/zap.ts +++ b/src/zap.ts @@ -33,7 +33,11 @@ export default class Zap { } - private nextChannel(channel: string) { + public getChannels(): string[] { + return this.channelNameList; + } + + private nextChannel(channel: string) :string { const size = this.channelNameList.length; const currentIndex = this.channelNameList.indexOf(channel); if (currentIndex >= 0) { @@ -44,11 +48,11 @@ export default class Zap { } } - private mod(n: number, m: number) { + private mod(n: number, m: number) :number{ return ((n % m) + m) % m; } - private previousChannel(channel: string) { + private previousChannel(channel: string):string { const size = this.channelNameList.length; const currentIndex = this.channelNameList.indexOf(channel); if (currentIndex >= 0) { @@ -65,7 +69,7 @@ export default class Zap { proc.once('exit', () => resolve() ) - proc.on("error", (err:Error) => + proc.on("error", (err: Error) => reject(err) ); })