chore(tools): add biomejs, apply lint+formatting fixes

This commit is contained in:
Marc 2024-03-14 00:49:34 -04:00
parent 44a9c52c78
commit c63fa19035
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 174 additions and 54 deletions

22
biome.json Normal file
View file

@ -0,0 +1,22 @@
{
"$schema": "https://biomejs.dev/schemas/1.6.1/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"enabled": true,
"lineWidth": 120
},
"javascript": {
"formatter": {
"semicolons": "asNeeded",
"quoteStyle": "single"
}
}
}

View file

@ -1,18 +1,23 @@
{
"name": "healthcheck",
"version": "noversion",
"main": "index.js",
"repository": "git@github.com:mcataford/healthcheck.git",
"author": "Marc Cataford <mcat@riseup.net>",
"private": true,
"packageManager": "yarn@4.1.1",
"devDependencies": {
"@types/node": "^20.11.27",
"typescript": "^5.4.2"
},
"dependencies": {
"@netlify/blobs": "^7.0.1",
"@netlify/functions": "^2.6.0",
"axios": "^1.6.7"
}
"name": "healthcheck",
"version": "noversion",
"main": "index.js",
"repository": "git@github.com:mcataford/healthcheck.git",
"author": "Marc Cataford <mcat@riseup.net>",
"private": true,
"packageManager": "yarn@4.1.1",
"scripts": {
"lint": "biome lint .; biome format .",
"lint:fix": "biome lint . --apply; biome format . --write"
},
"devDependencies": {
"@biomejs/biome": "^1.6.1",
"@types/node": "^20.11.27",
"typescript": "^5.4.2"
},
"dependencies": {
"@netlify/blobs": "^7.0.1",
"@netlify/functions": "^2.6.0",
"axios": "^1.6.7"
}
}

View file

@ -1,22 +1,20 @@
import { readFile } from 'node:fs/promises'
import axios from 'axios'
import { getStore } from '@netlify/blobs'
import type { Context } from '@netlify/functions'
interface Endpoint {
name: string
url: string
name: string
url: string
}
interface Configuration {
endpoints: Endpoint[]
webhook_url: string
endpoints: Endpoint[]
webhook_url: string
}
interface EndpointReport extends Endpoint {
status: number
healthy: boolean
status: number
healthy: boolean
}
/*
@ -30,21 +28,21 @@ interface EndpointReport extends Endpoint {
* extra status information.
*/
async function pingEndpoint(endpoint: Endpoint): Promise<EndpointReport> {
try {
const response = await axios.get(endpoint.url)
try {
const response = await axios.get(endpoint.url)
return {
...endpoint,
status: response.status,
healthy: true
}
} catch(e) {
return {
...endpoint,
healthy: false,
status: e.response.status
}
}
return {
...endpoint,
status: response.status,
healthy: true,
}
} catch (e) {
return {
...endpoint,
healthy: false,
status: e.response.status,
}
}
}
/*
@ -53,14 +51,13 @@ async function pingEndpoint(endpoint: Endpoint): Promise<EndpointReport> {
* The report is assembled line-by-line and joined together as one.
*/
function formatReport(endpointReports: EndpointReport[]): string {
const endpointStatuses = endpointReports.map((report: EndpointReport): string => {
if (report.healthy)
return `${report.name} is healthy (${report.status})`
else
return `🔥 ${report.name} did not respond normally (${report.status})`
})
const endpointStatuses = endpointReports.map((report: EndpointReport): string => {
if (report.healthy) return `${report.name} is healthy (${report.status})`
return endpointStatuses.join('\n')
return `🔥 ${report.name} did not respond normally (${report.status})`
})
return endpointStatuses.join('\n')
}
/*
@ -71,15 +68,19 @@ function formatReport(endpointReports: EndpointReport[]): string {
* the available environment variables separately.
*/
export default async (request: Request, context: Context) => {
//const conf: Configuration = require("../config.json")
const configurationStore = getStore('functions')
const conf: Configuration = await configurationStore.get('config', { type: 'json' })
//const conf: Configuration = require("../config.json")
const configurationStore = getStore('functions')
const conf: Configuration = await configurationStore.get('config', {
type: 'json',
})
const pings = await Promise.all(conf.endpoints.map(pingEndpoint))
const pings = await Promise.all(conf.endpoints.map(pingEndpoint))
const report = formatReport(pings)
const webhook_url = conf.webhook_url.replace('$DISCORD_WEBHOOK_ID', process.env.DISCORD_WEBHOOK_ID).replace('$DISCORD_WEBHOOK_TOKEN', process.env.DISCORD_WEBHOOK_TOKEN)
await axios.post(webhook_url, { content: report })
const report = formatReport(pings)
const webhook_url = conf.webhook_url
.replace('$DISCORD_WEBHOOK_ID', process.env.DISCORD_WEBHOOK_ID)
.replace('$DISCORD_WEBHOOK_TOKEN', process.env.DISCORD_WEBHOOK_TOKEN)
await axios.post(webhook_url, { content: report })
return new Response()
return new Response()
}

View file

@ -5,6 +5,97 @@ __metadata:
version: 8
cacheKey: 10c0
"@biomejs/biome@npm:^1.6.1":
version: 1.6.1
resolution: "@biomejs/biome@npm:1.6.1"
dependencies:
"@biomejs/cli-darwin-arm64": "npm:1.6.1"
"@biomejs/cli-darwin-x64": "npm:1.6.1"
"@biomejs/cli-linux-arm64": "npm:1.6.1"
"@biomejs/cli-linux-arm64-musl": "npm:1.6.1"
"@biomejs/cli-linux-x64": "npm:1.6.1"
"@biomejs/cli-linux-x64-musl": "npm:1.6.1"
"@biomejs/cli-win32-arm64": "npm:1.6.1"
"@biomejs/cli-win32-x64": "npm:1.6.1"
dependenciesMeta:
"@biomejs/cli-darwin-arm64":
optional: true
"@biomejs/cli-darwin-x64":
optional: true
"@biomejs/cli-linux-arm64":
optional: true
"@biomejs/cli-linux-arm64-musl":
optional: true
"@biomejs/cli-linux-x64":
optional: true
"@biomejs/cli-linux-x64-musl":
optional: true
"@biomejs/cli-win32-arm64":
optional: true
"@biomejs/cli-win32-x64":
optional: true
bin:
biome: bin/biome
checksum: 10c0/789580c9aa7e0661a643e82de85f15bcc4ef0c79a099204f234b3769cface5c604b137260a4a60767ab3a692bd7ac19e2de1fc75537ec14e7698f0b6a133cfef
languageName: node
linkType: hard
"@biomejs/cli-darwin-arm64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-darwin-arm64@npm:1.6.1"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard
"@biomejs/cli-darwin-x64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-darwin-x64@npm:1.6.1"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard
"@biomejs/cli-linux-arm64-musl@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-linux-arm64-musl@npm:1.6.1"
conditions: os=linux & cpu=arm64 & libc=musl
languageName: node
linkType: hard
"@biomejs/cli-linux-arm64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-linux-arm64@npm:1.6.1"
conditions: os=linux & cpu=arm64 & libc=glibc
languageName: node
linkType: hard
"@biomejs/cli-linux-x64-musl@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-linux-x64-musl@npm:1.6.1"
conditions: os=linux & cpu=x64 & libc=musl
languageName: node
linkType: hard
"@biomejs/cli-linux-x64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-linux-x64@npm:1.6.1"
conditions: os=linux & cpu=x64 & libc=glibc
languageName: node
linkType: hard
"@biomejs/cli-win32-arm64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-win32-arm64@npm:1.6.1"
conditions: os=win32 & cpu=arm64
languageName: node
linkType: hard
"@biomejs/cli-win32-x64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-win32-x64@npm:1.6.1"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard
"@netlify/blobs@npm:^7.0.1":
version: 7.0.1
resolution: "@netlify/blobs@npm:7.0.1"
@ -106,6 +197,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "healthcheck@workspace:."
dependencies:
"@biomejs/biome": "npm:^1.6.1"
"@netlify/blobs": "npm:^7.0.1"
"@netlify/functions": "npm:^2.6.0"
"@types/node": "npm:^20.11.27"