Initial app + gitea action #1
48
src/http.ts
48
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"
|
||||
|
@ -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();
|
||||
|
||||
|
12
src/zap.ts
12
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)
|
||||
);
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user