Skip to content

Commit f93a08f

Browse files
committed
thing are starting to work
1 parent 7794959 commit f93a08f

File tree

9 files changed

+599
-66
lines changed

9 files changed

+599
-66
lines changed

package.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@
2323
"unique-filename": "^1.1.0"
2424
},
2525
"devDependencies": {
26-
"tsc-watch": "^1.0.21"
26+
"tsc-watch": "^1.0.21",
27+
"lint-staged": "^6.0.0",
28+
"prettier": "1.9.2"
29+
},
30+
"lint-staged": {
31+
"*.{ts,tsx}": [
32+
"prettier --write",
33+
"git add"
34+
],
35+
"*.scss": [
36+
"prettier --write",
37+
"git add"
38+
],
39+
"*pkg/**/*.go": [
40+
"gofmt -w -s",
41+
"git add"
42+
]
43+
},
44+
"prettier": {
45+
"trailingComma": "es5",
46+
"singleQuote": true,
47+
"printWidth": 120
2748
}
2849
}

proto/renderer.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ package models;
55

66
message RenderRequest {
77
string url = 1;
8+
int32 width = 2;
9+
int32 height = 3;
10+
int32 timeout = 4;
11+
string timezone = 5;
12+
string encoding = 6;
13+
string filePath = 7;
814
}
915

1016
message RenderResponse {
11-
string filePath = 1;
17+
string error = 1;
1218
}
1319

1420
service Renderer {

renderer.proto

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/app.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { startGrpcPlugin } from './grpc-plugin';
1+
import { GrpcPlugin } from './grpc-plugin';
22
import { HttpServer } from './http-server';
3-
import { Logger } from './logger';
3+
import { Logger, ConsoleLogger, PluginLogger } from './logger';
44
import { Browser } from './browser';
55
import * as minimist from 'minimist';
66

77

88
async function main() {
9-
let argv = minimist(process.argv.slice(2));
10-
let command = argv._[0];
9+
const argv = minimist(process.argv.slice(2));
10+
const command = argv._[0];
1111

1212
if (command === undefined) {
13-
startGrpcPlugin();
13+
const logger = new PluginLogger();
14+
const browser = new Browser(logger);
15+
await browser.start();
16+
17+
const plugin = new GrpcPlugin(logger, browser);
18+
plugin.start();
1419
}
1520

1621
if (command === 'server') {
@@ -19,11 +24,11 @@ async function main() {
1924
return;
2025
}
2126

22-
let logger = new Logger();
23-
let browser = new Browser(logger);
27+
const logger = new ConsoleLogger();
28+
const browser = new Browser(logger);
2429
await browser.start();
2530

26-
let server = new HttpServer({port: argv.port}, logger, browser);
31+
const server = new HttpServer({port: argv.port}, logger, browser);
2732
server.start();
2833

2934
} else {

src/browser.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,43 @@ export class Browser {
1919
});
2020
}
2121

22-
async render() {
22+
validateOptions(options) {
23+
options.width = parseInt(options.width) || 1000;
24+
options.height = parseInt(options.height) || 500;
25+
26+
if (options.width > 3000 || options.width < 10) {
27+
options.width = 2500;
28+
}
29+
30+
if (options.height > 3000 || options.height < 10) {
31+
options.height = 1500;
32+
}
33+
}
34+
35+
async render(options) {
2336
const page = await this.instance.newPage();
24-
await page.goto('http://google.com');
2537

26-
const imagePath = uniqueFilename(os.tmpdir()) + '.png';
27-
await page.screenshot({path: imagePath});
38+
this.validateOptions(options);
39+
40+
await page.setViewport({
41+
width: options.width,
42+
height: options.height,
43+
});
2844

29-
return { imagePath: imagePath };
45+
if (!options.url) {
46+
options.url = "http://localhost:3000/d-solo/000000088/testdata-graph-panel-last-1h?orgId=1&panelId=4&from=1526537735449&to=1526541335449&width=1000&height=500&tz=UTC%2B02:00";
47+
}
48+
49+
await page.goto(options.url);
50+
51+
if (!options.filePath) {
52+
options.filePath = uniqueFilename(os.tmpdir()) + '.png';
53+
}
54+
55+
await page.screenshot({path: options.filePath});
56+
page.close();
57+
58+
return { filePath: options.filePath };
3059
}
3160
}
61+

src/grpc-plugin.ts

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as grpc from 'grpc';
2+
import { Logger } from './logger';
3+
import { Browser } from './browser';
24

35
const SERVER_ADDRESS = '127.0.0.1:50051';
46
const RENDERER_PROTO_PATH = __dirname + '/../proto/renderer.proto';
@@ -7,30 +9,52 @@ const GRPC_HEALTH_PROTO_PATH = __dirname + '/../proto/health.proto';
79
export const RENDERER_PROTO = grpc.load(RENDERER_PROTO_PATH).models;
810
export const GRPC_HEALTH_PROTO = grpc.load(GRPC_HEALTH_PROTO_PATH).grpc.health.v1;
911

10-
/**
11-
* Implements the Health Check RPC method.
12-
*/
13-
function check(call, callback) {
14-
callback(null, {status: 'SERVING'});
15-
}
1612

17-
function render(call, callback) {
18-
console.log('render', call.request);
19-
callback(null, {filePath: call.request.url + 'resp'});
20-
}
13+
export class GrpcPlugin {
2114

22-
/**
23-
* Starts an RPC server that receives requests for the Greeter service at the
24-
* sample server port
25-
*/
26-
export function startGrpcPlugin() {
27-
var server = new grpc.Server();
28-
server.addService(GRPC_HEALTH_PROTO.Health.service, {check: check});
29-
server.addService(RENDERER_PROTO.Renderer.service, {render: render});
30-
server.bind(SERVER_ADDRESS, grpc.ServerCredentials.createInsecure());
31-
server.start();
32-
console.log(`1|1|tcp|${SERVER_ADDRESS}|grpc`);
33-
console.error('Renderer plugin started');
34-
}
15+
constructor(private log: Logger, private browser: Browser) {
16+
}
17+
18+
start() {
19+
var server = new grpc.Server();
20+
21+
server.addService(GRPC_HEALTH_PROTO.Health.service, {
22+
check: this.check.bind(this),
23+
});
24+
server.addService(RENDERER_PROTO.Renderer.service, {
25+
render: this.render.bind(this),
26+
});
27+
28+
server.bind(SERVER_ADDRESS, grpc.ServerCredentials.createInsecure());
29+
server.start();
3530

31+
console.log(`1|1|tcp|${SERVER_ADDRESS}|grpc`);
32+
this.log.info('Renderer plugin started');
33+
}
34+
35+
check(call, callback) {
36+
callback(null, {status: 'SERVING'});
37+
}
38+
39+
async render(call, callback) {
40+
let req = call.request;
41+
42+
this.log.info('render req', req);
43+
44+
let options = {
45+
url: req.url,
46+
width: req.width,
47+
height: req.height,
48+
filePath: req.filePath,
49+
};
50+
51+
try {
52+
let result = await this.browser.render(options);
53+
callback(null, {error: ''});
54+
} catch (err) {
55+
this.log.info("Error", err);
56+
callback(null, {error: err.toString()});
57+
}
58+
}
59+
}
3660

src/http-server.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,28 @@ export class HttpServer {
2020
});
2121

2222
this.app.get('/render', asyncMiddleware(this.render));
23+
this.app.use((err, req, res, next) => {
24+
return res.status(err.output.statusCode).json(err.output.payload);
25+
});
2326

2427
this.app.listen(this.options.port);
2528
this.log.info(`HTTP Server started, listening on ${this.options.port}`);
2629
}
2730

2831
render = async (req: express.Request, res: express.Response) => {
29-
let result = await this.browser.render();
32+
if (!req.query.url) {
33+
throw boom.badRequest('Missing url parameter');
34+
}
35+
36+
let options = {
37+
url: req.query.url,
38+
width: req.query.width,
39+
height: req.query.height,
40+
};
3041

31-
res.sendFile(result.imagePath);
42+
let result = await this.browser.render(options);
3243

33-
this.log.info(req.query);
44+
res.sendFile(result.filePath);
3445
}
3546
}
3647

src/logger.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
export class Logger {
21

3-
info(msg) {
4-
console.log(msg);
2+
export interface Logger {
3+
info(...optionalParams: any[]);
4+
}
5+
6+
7+
export class ConsoleLogger {
8+
9+
info(...optionalParams: any[]) {
10+
console.log(...optionalParams);
11+
}
12+
13+
}
14+
15+
export class PluginLogger {
16+
17+
info(...optionalParams: any[]) {
18+
console.error(...optionalParams);
519
}
620

721
}

0 commit comments

Comments
 (0)