Skip to content

Commit

Permalink
Healthcheck endpoint (#44)
Browse files Browse the repository at this point in the history
* Base healthcheck

* Readme

* Browser info

* Docstring

* bad browser status
  • Loading branch information
MatthewZMSU authored Jun 27, 2024
1 parent 02c0fd9 commit 3999149
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,28 @@ Also you can add extra http headers to each request that is made on page.
}
```

### **/health_check**

This GET method allows to get liveliness of the server.
You get such responses with 200 status code if the server is up:
```json5
{
"timestamp": 1719222396341, // seconds since the epoch
"uptime": 222.555, // seconds of uptime
"message": "OK", // good or error message
"browser": { // connected browser info
"connection": true, // is browser connected
"version": "Chrome/113.0.5672.63", // browser version
"contexts": 1, // number of contexts
"pages": 1, // number of pages
}
}
```
Otherwise, you get 503 status code.

### **/goto**

This method allows to goto a page with a specific url in puppeteer.
This POST method allows to goto a page with a specific url in puppeteer.

Params:

Expand Down Expand Up @@ -84,7 +103,7 @@ Example request body
```

### **/back** and **/forward**
These methods help to navigate back and forward to see previously seen pages.
These POST methods help to navigate back and forward to see previously seen pages.

Example request body
```json5
Expand All @@ -100,7 +119,7 @@ Example request body

### **/click**

This method allows to click on first element that is matched by selector and return page result.
This POST method allows to click on first element that is matched by selector and return page result.

Example request body:
```json5
Expand All @@ -122,7 +141,7 @@ Example request body:

### **/scroll**

This method allows to scroll page to the first element that is matched by selector and returns page result.
This POST method allows to scroll page to the first element that is matched by selector and returns page result.

Example request body:
```json5
Expand All @@ -136,7 +155,7 @@ Example request body:

### **/action**

Body of this request should be a js code that declares function action with at least page
Body of this POST request should be a js code that declares function action with at least page
parameter. The content type of request should be:
```http request
Content-Type: application/javascript
Expand All @@ -159,7 +178,7 @@ async function action(page, request) {

### **/screenshot**

This method returns screenshots of current page more.
This POST method returns screenshots of current page more.
Description of options you can see on [puppeteer GitHub](https://github.com/GoogleChrome/puppeteer/blob/v1.19.0/docs/api.md#pagescreenshotoptions).
The path options is omitted in options. Also the only possibly encoding is `base64`.

Expand All @@ -176,7 +195,7 @@ Example request body:

### **/recaptcha_solver**

This method implements recaptcha solving based on the [puppeteer-extra-plugin-recaptcha](https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-recaptcha).
This POST method implements recaptcha solving based on the [puppeteer-extra-plugin-recaptcha](https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-recaptcha).

Example request body:
```json5
Expand All @@ -190,7 +209,7 @@ Example request body:
```

### **/close_context**
This method close browser context and all its pages.
This POST method close browser context and all its pages.
Make sure you finished all your requests to this context.

## Environment variables
Expand Down
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const bodyParser = require('body-parser');
const AsyncLock = require('async-lock');

const indexRouter = require('./routes/index');
const healthCheckRouter = require('./routes/health_check');
const gotoRouter = require('./routes/goto');
const backRouter = require('./routes/goback');
const forwardRouter = require('./routes/goforward');
Expand Down Expand Up @@ -91,6 +92,7 @@ app.use(bodyParser.raw({inflate: true, limit: '200kb', type: 'application/javasc
app.use(cookieParser());

app.use('/', indexRouter);
app.use('/health_check', healthCheckRouter);
app.use('/goto', gotoRouter);
app.use('/back', backRouter);
app.use('/forward', forwardRouter);
Expand Down
1 change: 0 additions & 1 deletion routes/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ router.post('/', async function (req, res, next) {
} catch (e) {
next(e);
}

});

module.exports = router;
1 change: 0 additions & 1 deletion routes/har.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ router.post('/', async function (req, res, next) {
} catch (e) {
next(e);
}

});

module.exports = router;
50 changes: 50 additions & 0 deletions routes/health_check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const express = require('express');
const router = express.Router();

/**
* HealthCheck endpoint.
* Sends 2xx status code if the server is up,
* 5xx otherwise.
* As a response returns an object:
* {
* timestamp: timestamp,
* uptime: number,
* message: string,
* browser: { // browser info
* connection: boolean,
* version: string,
* contexts: number,
* pages: number,
* }
* }
**/
router.get('/', async (req, res, next) => {
const healthCheck = {
timestamp: Date.now(),
uptime: process.uptime(),
message: "OK",
};

const browser = req.app.get('browser');
if (browser) {
healthCheck.browser = {
connection: browser.isConnected(),
version: await browser.version(),
contexts: browser.browserContexts().length,
pages: (await browser.pages()).length,
}
} else {
res.status(503); // Service Unavailable
healthCheck.message = "Browser is undefined";
}

try {
res.send(healthCheck);
} catch (e) {
healthCheck.message = e.toString();
res.status(503); // Service Unavailable
res.send(healthCheck);
}
});

module.exports = router;

0 comments on commit 3999149

Please sign in to comment.