Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerized #2

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .env.docker.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
NODE_ENV=development

# ---------------------------------------
# The port to run the batch API endpoints
# ---------------------------------------
PORT=9090

# -----------------------------
# The electrs json-rpc endpoint
# -----------------------------
ELECTRS_URL=http://localhost:3000


# -------------------------------------
# The allowed batch request concurrency
# -------------------------------------
CONCURRENCY=10


# ------------------------------------
# The Sentry DSN (for error reporting)
# ------------------------------------
SENTRY_DSN=
45 changes: 45 additions & 0 deletions .github/workflows/docker-deploy-to-github.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: 'Docker deploy to GitHub'

on:
push:
branches:
- dockerized

env:
# GitHub settings
REGISTRY: ghcr.io/liquality

# AWS settings
AWS_REGION: 'us-east-1'

# Docker image settings
API_IMAGE_NAME: 'electrs-batch-api'
API_IMAGE_TAG: 'latest'

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Build, tag, and push Docker image
- name: Build, tag, push image to GitHub
id: build-api-image
run: |
echo "Building and tagging Docker image ($API_IMAGE_NAME)"
docker build -t $API_IMAGE_NAME . --no-cache
docker tag $API_IMAGE_NAME:$API_IMAGE_TAG $REGISTRY/$API_IMAGE_NAME:$API_IMAGE_TAG
echo "Pushing image to registry as: $REGISTRY/$API_IMAGE_NAME"
docker push $REGISTRY/$API_IMAGE_NAME:$API_IMAGE_TAG
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules/
yarn.lock
.env
.env.docker.local
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"printWidth" : 120,
"semi":false,
"trailingComma":"none"
}
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM node:16-alpine

# -------------------
# Build app directory
# -------------------
WORKDIR /app

# Build dependencies
COPY package*.json ./
RUN npm ci

# Bundle app source
COPY . .

# -------------
# Start Service
# -------------
EXPOSE 9090

CMD ["node", "./bin/electrs-batch-server"]
174 changes: 101 additions & 73 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,127 @@
const _ = require('lodash')
const Sentry = require('@sentry/node')

const {
PORT,
NODE_ENV,
ELECTRS_URL,
CONCURRENCY,
SENTRY_DSN
} = process.env

if (NODE_ENV === 'production' && SENTRY_DSN) {
Sentry.init({ dsn: SENTRY_DSN })
}

const Bluebird = require('bluebird')
const axios = require('axios')
const express = require('express')
const helmet = require('helmet')
const compression = require('compression')
const bodyParser = require('body-parser')
const asyncHandler = require('express-async-handler')
const cors = require('cors')

const httpError = require('./http-error')
const systemDefaults = require('./systemDefaults')

if (!PORT) throw new Error('Invalid PORT')
if (!ELECTRS_URL) throw new Error('Invalid ELECTRS_URL')
if (!CONCURRENCY) throw new Error('Invalid CONCURRENCY')
const { NODE_ENV, ELECTRS_URL } = process.env
const PORT = process.env.PORT || systemDefaults.port
const CONCURRENCY = process.env.CONCURRENCY || systemDefaults.concurrency

const app = express()
const electrs = axios.create({ baseURL: ELECTRS_URL })

if (NODE_ENV === 'production') {
app.use(Sentry.Handlers.requestHandler())
}
if (!ELECTRS_URL) throw new Error('Invalid ELECTRS_URL')

const electrs = axios.create({ baseURL: ELECTRS_URL })

app.use(
cors({
maxAge: 86400 // 1 day
})
)
app.use(helmet())
app.use(compression())
app.use(bodyParser.json({ limit: '5mb' }))
app.set('etag', false)

app.post('/addresses', asyncHandler(async (req, res, next) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
}

addresses = _.uniq(addresses)

const response = await Bluebird.map(addresses, address => {
return electrs.get(`/address/${address}`).then(response => response.data)
}, { concurrency: Number(CONCURRENCY) })

res.json(response)
}))

app.post('/addresses/utxo', asyncHandler(async (req, res, next) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
}

addresses = _.uniq(addresses)

const response = await Bluebird.map(addresses, address => {
return electrs.get(`/address/${address}/utxo`).then(response => ({
address,
utxo: response.data
}))
}, { concurrency: Number(CONCURRENCY) })

res.json(response)
}))
app.get('/', function (req, res) {
res.setHeader('Content-Type', 'text/html')
res.send('Electrs Batch API is running')
})

app.post('/addresses/transactions', asyncHandler(async (req, res, next) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
// GET /status
// A status endpoint for monitoring the batch API
// (on success) Returns status 200 and the latest indexed block
// (on error) Returns the underlying error message with error status
app.get('/status', async (req, res) => {
try {
const payload = await electrs.get('/blocks/tip/height')
const data = payload && payload.data ? payload.data : 'no data'
return res.status(200).json(data)
} catch (err) {
const message = err.response && err.response.data ? err.response.data : err.message
const status = err.status || 500
return res.status(status).json(`${status}: ${message}`)
}
})

addresses = _.uniq(addresses)

const response = await Bluebird.map(addresses, address => {
return electrs.get(`/address/${address}/txs/chain`).then(response => ({
address,
transaction: response.data
}))
}, { concurrency: Number(CONCURRENCY) })
res.json(response)
}))

app.all('/*', function (req, res) {
res.status(404).json({
error: '404'
app.post(
'/addresses',
asyncHandler(async (req, res) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
}

addresses = _.uniq(addresses)

const response = await Bluebird.map(
addresses,
(address) => {
return electrs.get(`/address/${address}`).then((response) => response.data)
},
{ concurrency: Number(CONCURRENCY) }
)

res.json(response)
})
})
)

app.post(
'/addresses/utxo',
asyncHandler(async (req, res) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
}

addresses = _.uniq(addresses)

const response = await Bluebird.map(
addresses,
(address) => {
return electrs.get(`/address/${address}/utxo`).then((response) => ({
address,
utxo: response.data
}))
},
{ concurrency: Number(CONCURRENCY) }
)

res.json(response)
})
)

app.post(
'/addresses/transactions',
asyncHandler(async (req, res) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
}

addresses = _.uniq(addresses)

const response = await Bluebird.map(
addresses,
(address) => {
return electrs.get(`/address/${address}/txs/chain`).then((response) => ({
address,
transaction: response.data
}))
},
{ concurrency: Number(CONCURRENCY) }
)
res.json(response)
})
)

app.use((err, req, res, next) => {
const status = err.statusCode || 500
Expand All @@ -106,4 +134,4 @@ app.use((err, req, res, next) => {
return httpError(req, res, status, message)
})

app.listen(PORT, () => console.log(`API is running on ${PORT}`))
app.listen(PORT, () => console.log(`Electrs Batch API is running on ${PORT}`))
Loading