pinax-network / substreams-sink

Substreams Sink library
MIT License
4 stars 1 forks source link

Provide `/health` check endpoint #14

Closed DenisCarriere closed 1 year ago

DenisCarriere commented 1 year ago

Reference

https://docs.railway.app/deploy/healthchecks

""" First, make sure your webserver has an endpoint (e.g. /health) that will return a response with HTTP status code 200 when your application is live and ready. If your application needs to do some initialization on startup (running database migrations, populating cache, etc.), it's a good idea to return a non-200 status code from your endpoint until the initialization completes. """

Example

export function health() {
    http.server.on("request", async (req, res) => {
        if (!req.url) return;
        const params = new URLSearchParams(req.url.split("?")[1] ?? "");
        try {
            if (req.method == "GET") {
                if ( req.url === "/") return toText(res, banner());
                if ( req.url === "/health" ) {
                    const messages = await getSingleMetric("substreams_sink_data_message")
                    if ( messages ) return toText(res, "OK");
                    return toText(res, "no messages received yet", 503);
                }
            }
            // throw new Error(`invalid request`);
        } catch (err: any) {
            res.statusCode = 400;
            return res.end(err.message);
        }
    });
}
chamorin commented 1 year ago

Added in commit: https://github.com/pinax-network/substreams-sink/commit/588865b71ea20593e3359b502c20f4f7c013294e

DenisCarriere commented 1 year ago

Implemented