Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add rudimentary web control panel (#183)
- Loading branch information
Showing
9 changed files
with
395 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,4 @@ TWITTER_CONSUMER_KEY= | |
TWITTER_CONSUMER_SECRET= | ||
TWITTER_TWEET_TAGS= | ||
USER_AGENT= | ||
WEB_PORT= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,13 +42,10 @@ import {ProshopDE} from './proshop-de'; | |
import {ProshopDK} from './proshop-dk'; | ||
import {Saturn} from './saturn'; | ||
import {Scan} from './scan'; | ||
import {Store} from './store'; | ||
import {Very} from './very'; | ||
import {Zotac} from './zotac'; | ||
import {config} from '../../config'; | ||
import {logger} from '../../logger'; | ||
|
||
const masterList = new Map([ | ||
export const storeList = new Map([ | ||
[Adorama.name, Adorama], | ||
[Alternate.name, Alternate], | ||
[AlternateNL.name, AlternateNL], | ||
|
@@ -97,33 +94,27 @@ const masterList = new Map([ | |
[Zotac.name, Zotac] | ||
]); | ||
|
||
const list = new Map(); | ||
|
||
for (const storeData of config.store.stores) { | ||
if (masterList.has(storeData.name)) { | ||
list.set(storeData.name, {...masterList.get(storeData.name), ...storeData}); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
} else { | ||
const logString = `No store named ${storeData.name}, skipping.`; | ||
logger.warn(logString); | ||
const brands = new Set(); | ||
const series = new Set(); | ||
const models = new Set(); | ||
for (const store of storeList.values()) { | ||
for (const link of store.links) { | ||
brands.add(link.brand); | ||
series.add(link.series); | ||
models.add(link.model); | ||
} | ||
} | ||
|
||
logger.info(`ℹ selected stores: ${Array.from(list.keys()).join(', ')}`); | ||
|
||
if (config.store.showOnlyBrands.length > 0) { | ||
logger.info(`ℹ selected brands: ${config.store.showOnlyBrands.join(', ')}`); | ||
This comment has been minimized.
Sorry, something went wrong.
SesioN
Contributor
|
||
export function getAllBrands() { | ||
return Array.from(brands); | ||
} | ||
|
||
if (config.store.showOnlyModels.length > 0) { | ||
logger.info(`ℹ selected models: ${config.store.showOnlyModels.map(entry => { | ||
return entry.series ? entry.name + ' (' + entry.series + ')' : entry.name; | ||
}).join(', ')}`); | ||
export function getAllSeries() { | ||
return Array.from(series); | ||
} | ||
|
||
if (config.store.showOnlySeries.length > 0) { | ||
logger.info(`ℹ selected series: ${config.store.showOnlySeries.join(', ')}`); | ||
export function getAllModels() { | ||
return Array.from(models); | ||
} | ||
|
||
export const Stores = Array.from(list.values()) as Store[]; | ||
|
||
export * from './store'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import {IncomingMessage, Server, ServerResponse, createServer} from 'http'; | ||
import {config, setConfig} from '../config'; | ||
import {createReadStream, readdir} from 'fs'; | ||
import {getAllBrands, getAllModels, getAllSeries, storeList} from '../store/model'; | ||
import {join, normalize} from 'path'; | ||
|
||
const approot = join(__dirname, '../../'); | ||
const webroot = join(approot, './web'); | ||
|
||
const contentTypeMap: { [key: string]: string } = { | ||
css: 'text/css', | ||
htm: 'text/html', | ||
html: 'text/html', | ||
jpeg: 'image/jpeg', | ||
jpg: 'image/jpeg', | ||
js: 'application/javascript', | ||
json: 'application/json', | ||
png: 'image/png', | ||
txt: 'text/plain' | ||
}; | ||
|
||
function sendFile(response: ServerResponse, path: string, relativeTo: string = webroot) { | ||
path = normalize(`./${path}`); | ||
|
||
const fsPath = join(relativeTo, path); | ||
try { | ||
const stream = createReadStream(fsPath); | ||
stream.on('error', error => { | ||
sendError(response, error.message); | ||
}); | ||
|
||
const pathSplit = path.split('.'); | ||
const ext = pathSplit[pathSplit.length - 1].toLowerCase(); | ||
|
||
response.setHeader('Content-Type', contentTypeMap[ext] ?? contentTypeMap.txt); | ||
|
||
stream.on('end', () => response.end()); | ||
stream.pipe(response); | ||
} catch (error) { | ||
sendError(response, error); | ||
} | ||
} | ||
|
||
function sendError(response: ServerResponse, data: string, statusCode = 500) { | ||
response.statusCode = statusCode; | ||
response.setHeader('Content-Type', contentTypeMap.txt); | ||
response.write(data); | ||
response.end(); | ||
} | ||
|
||
function sendJSON(response: ServerResponse, data: any) { | ||
response.setHeader('Content-Type', contentTypeMap.json); | ||
response.write(JSON.stringify(data)); | ||
response.end(); | ||
} | ||
|
||
function sendConfig(response: ServerResponse) { | ||
sendJSON(response, config); | ||
} | ||
|
||
function handleAPI(request: IncomingMessage, response: ServerResponse, urlComponents: string[]) { | ||
if (urlComponents.length < 2) { | ||
sendError(response, 'No API route specified', 400); | ||
return; | ||
} | ||
|
||
switch (urlComponents[1]) { | ||
case 'config': | ||
if (request.method === 'PUT') { | ||
const data: string[] = []; | ||
request.on('data', chunk => { | ||
data.push(chunk); | ||
}); | ||
request.on('end', () => { | ||
// We ignore errors, client just sent wrong data... | ||
try { | ||
setConfig(JSON.parse(data.join(''))); | ||
} catch { } | ||
|
||
sendConfig(response); | ||
}); | ||
return; | ||
} | ||
|
||
sendConfig(response); | ||
return; | ||
case 'stores': | ||
sendJSON(response, Array.from(storeList.keys())); | ||
return; | ||
case 'brands': | ||
sendJSON(response, getAllBrands()); | ||
return; | ||
case 'series': | ||
sendJSON(response, getAllSeries()); | ||
return; | ||
case 'models': | ||
sendJSON(response, getAllModels()); | ||
return; | ||
case 'screenshots': | ||
if (urlComponents.length >= 3) { | ||
const timeStamp = urlComponents[2]; | ||
if (/\D/.test(timeStamp)) { | ||
sendError(response, 'Invalid screenshot timestamp', 400); | ||
return; | ||
} | ||
|
||
sendFile(response, `../success-${timeStamp}.png`); | ||
return; | ||
} | ||
|
||
readdir(approot, (error, files) => { | ||
if (error) { | ||
sendError(response, error.message); | ||
return; | ||
} | ||
|
||
const result = []; | ||
for (const file of files) { | ||
const match = /^success-(\d+)\.png$/.exec(file); | ||
if (!match) { | ||
continue; | ||
} | ||
|
||
result.push(match[1]); | ||
} | ||
|
||
sendJSON(response, result); | ||
}); | ||
return; | ||
default: | ||
sendError(response, 'No API route found for path', 400); | ||
} | ||
} | ||
|
||
function requestListener(request: IncomingMessage, response: ServerResponse) { | ||
const url = request.url!; | ||
const urlComponents = url.slice(1).split('/'); // Remove the leading / | ||
|
||
switch (urlComponents[0]) { | ||
case 'api': | ||
handleAPI(request, response, urlComponents); | ||
break; | ||
default: | ||
sendFile(response, url === '/' ? '/index.html' : url); | ||
break; | ||
} | ||
} | ||
|
||
let server: Server | undefined; | ||
|
||
export async function startAPIServer() { | ||
await stopAPIServer(); | ||
if (process.env.WEB_PORT) { | ||
server = createServer(requestListener); | ||
server.listen(Number(process.env.WEB_PORT)); | ||
} | ||
} | ||
|
||
export async function stopAPIServer() { | ||
return new Promise(resolve => { | ||
if (server) { | ||
server.close(resolve); | ||
server = undefined; | ||
return; | ||
} | ||
|
||
resolve(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
This is required for #576 to work and the new implementation seems to not consider it at all. It breaks #576 completely.