-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8e3d11
commit 3b4e9d2
Showing
7 changed files
with
62 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { CORS_HEADERS } from "./cors.js"; | ||
|
||
export default async function (req: Request) { | ||
return new Response('Departed', {headers: CORS_HEADERS}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { BunFile } from "bun"; | ||
|
||
export const BadRequest = toText('Bad Request', 400); | ||
export const NotFound = toText('Not Found', 404); | ||
export const InternalServerError = toText("Internal Server Error", 500); | ||
|
||
export const CORS_HEADERS = new Headers({ | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", | ||
"Access-Control-Allow-Headers": "Content-Type, WWW-Authenticate", | ||
}); | ||
export const JSON_HEADERS = new Headers({"Content-Type": "application/json"}); | ||
export const TEXT_HEADERS = new Headers({"Content-Type": "text/plain; version=0.0.4; charset=utf-8"}); | ||
|
||
export function appendHeaders(...args: Headers[]) { | ||
const headers = new Headers(CORS_HEADERS); // CORS as default headers | ||
for (const arg of args) { | ||
for (const [key, value] of arg.entries()) { | ||
headers.set(key, value); | ||
} | ||
} | ||
return headers; | ||
}; | ||
|
||
export function toJSON(body: any, status = 200, headers = new Headers()) { | ||
const data = typeof body == "string" ? body : JSON.stringify(body, null, 2); | ||
return new Response(data, { status, headers: appendHeaders(JSON_HEADERS, headers) }); | ||
} | ||
|
||
export function toText(body: string, status = 200, headers = new Headers()) { | ||
return new Response(body, { status, headers: appendHeaders(TEXT_HEADERS, headers) }); | ||
} | ||
|
||
export function toFile(body: BunFile, status = 200, headers = new Headers()) { | ||
const fileHeaders = new Headers({"Content-Type": body.type}); | ||
return new Response(body, { status, headers: appendHeaders(fileHeaders, headers) }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import client from "../clickhouse/createClient.js"; | ||
import { logger } from "../logger.js"; | ||
import * as prometheus from "../prometheus.js"; | ||
import { InternalServerError, toText } from "./cors.js"; | ||
|
||
export default async function (req: Request) { | ||
try { | ||
const response = await client.ping(); | ||
if (response.success === false) throw new Error(response.error.message); | ||
if (response.success === true ) return new Response("OK"); | ||
return new Response("Unknown response from ClickHouse"); | ||
if (response.success === true ) return toText("OK"); | ||
return InternalServerError; | ||
} catch (e: any) { | ||
logger.error(e); | ||
prometheus.request_error.inc({ pathname: "/health", status: 500}); | ||
return new Response(e.message, { status: 500 }); | ||
return InternalServerError; | ||
} | ||
} |