@http4t/bidi-eg
Advanced tools
Comparing version 0.0.131 to 0.0.137
89
App.ts
@@ -1,39 +0,66 @@ | ||
import { HttpHandler, HttpRequest } from "@http4t/core/contract"; | ||
import { httpInfoLogger } from "./log/HttpInfoLogger"; | ||
import { CumulativeLogger } from "./Logger"; | ||
import { handleError } from "./filters/errors"; | ||
import { inTransaction } from "./filters/transaction"; | ||
import { PostgresStore } from "./Store"; | ||
import { TransactionPool } from "./TransactionPool"; | ||
import { middlewares } from "./utils/Filter"; | ||
import { ExampleRouter } from "./routes"; | ||
import { migrate } from "./Db"; | ||
import {buildRouter} from "@http4t/bidi/router"; | ||
import {HttpHandler, HttpRequest, HttpResponse} from "@http4t/core/contract"; | ||
import {Closeable} from "@http4t/core/server"; | ||
import {Api, routes} from "./api"; | ||
import {handleError} from "./filters/errors"; | ||
import {wrapTransaction} from "./filters/wrapTransaction"; | ||
import {httpInfoLogger} from "./log/HttpInfoLogger"; | ||
import {CumulativeLogger} from "./Logger"; | ||
import {migrate} from "./migrations"; | ||
import {Doc, PostgresStore} from "./Store"; | ||
import {Transaction, TransactionPool} from "./TransactionPool"; | ||
import {middlewares} from "./utils/Filter"; | ||
export class App implements HttpHandler { | ||
function behaviour(transaction: Transaction, logger: CumulativeLogger): Api { | ||
const store = new PostgresStore(transaction); | ||
return { | ||
async live(): Promise<undefined> { | ||
logger.info('probed live'); | ||
return undefined; | ||
}, | ||
async ready(): Promise<undefined> { | ||
logger.info('probed ready'); | ||
return undefined; | ||
}, | ||
async get(request: { id: string }): Promise<Doc | undefined> { | ||
const document = await store.get(request.id); | ||
logger.info(`retrieved json: "${JSON.stringify(document)}"`); | ||
return document | ||
}, | ||
async post(doc: Doc): Promise<{ id: string }> { | ||
logger.info('storing json'); | ||
await store.save(doc); | ||
return {id: doc.id}; | ||
}, | ||
async test(doc: Doc): Promise<undefined> { | ||
logger.info('throwing an exception'); | ||
await store.save(doc); | ||
throw new Error("Deliberate error"); | ||
}, | ||
}; | ||
} | ||
constructor(private transactionPool: TransactionPool) {} | ||
function router(transaction: Transaction, logger: CumulativeLogger): HttpHandler { | ||
return buildRouter(routes, behaviour(transaction, logger)); | ||
} | ||
async handle(request: HttpRequest) { | ||
const transaction = await this.transactionPool.getTransaction(); | ||
const store = new PostgresStore(transaction); | ||
const logger = new CumulativeLogger(); | ||
export async function startApp(transactionPool: TransactionPool): Promise<HttpHandler & Closeable> { | ||
await migrate(transactionPool); | ||
const logger = new CumulativeLogger(); | ||
const middleware = middlewares( | ||
inTransaction(transaction), | ||
httpInfoLogger(logger), | ||
handleError(logger)); | ||
return { | ||
async handle(request: HttpRequest): Promise<HttpResponse> { | ||
const transaction = await transactionPool.getTransaction(); | ||
const handler = middleware(new ExampleRouter(store, logger)); | ||
const middleware = middlewares( | ||
wrapTransaction(transaction), | ||
httpInfoLogger(logger), | ||
handleError(logger)); | ||
return handler.handle(request); | ||
return middleware(router(transaction, logger)).handle(request); | ||
}, | ||
async close(): Promise<void> { | ||
await transactionPool.stop(); | ||
} | ||
}; | ||
async start() { | ||
await migrate(this.transactionPool); | ||
} | ||
async stop() { | ||
await this.transactionPool.stop(); | ||
} | ||
} | ||
import {ServerHandler} from "@http4t/node/server"; | ||
import {Pool} from "pg"; | ||
import {App} from "./app"; | ||
import {testDatabase} from "../test/db"; | ||
import {startApp} from "./App"; | ||
import {PostgresTransactionPool} from "./TransactionPool"; | ||
(async function main() { | ||
const pool = new PostgresTransactionPool(new Pool({})); | ||
const app = new App(pool); | ||
await app.start(); | ||
const server = new ServerHandler(app); | ||
const router = await startApp(new PostgresTransactionPool(new Pool(testDatabase))); | ||
const server = new ServerHandler(router); | ||
console.log('Running on port', (await server.url()).authority); | ||
})(); |
{ | ||
"name": "@http4t/bidi-eg", | ||
"version": "0.0.131", | ||
"version": "0.0.137", | ||
"license": "Apache-2.0" | ||
} |
19
Store.ts
@@ -1,8 +0,15 @@ | ||
import { Transaction} from "./TransactionPool"; | ||
import {Transaction} from "./TransactionPool"; | ||
export type Doc = { | ||
id: string; | ||
document: any; | ||
} | ||
export interface Store { | ||
get(id: string): any; | ||
save(id: string, document: object): Promise<any>; | ||
save(doc: Doc): Promise<void>; | ||
} | ||
export class PostgresStore implements Store { | ||
@@ -12,9 +19,7 @@ constructor(private transaction: Transaction) { | ||
public async save(id: string, document: object): Promise<void> { | ||
if (!this.transaction) throw new Error('No transaction.'); | ||
await this.transaction.query('INSERT INTO store values($1, $2) returning *', [id, document]); | ||
async save(doc: Doc): Promise<void> { | ||
await this.transaction.query('INSERT INTO store values($1, $2) returning *', [doc.id, doc.document]); | ||
} | ||
public async get(id: string): Promise<any> { | ||
if (!this.transaction) throw new Error('No transaction.'); | ||
async get(id: string): Promise<Doc | undefined> { | ||
const query = await this.transaction.query('SELECT * FROM store t WHERE t.id = $1', [id]); | ||
@@ -21,0 +26,0 @@ return query.rows[0]; |
@@ -39,3 +39,2 @@ import {Pool, PoolClient} from "pg"; | ||
} | ||
} |
@@ -1,5 +0,4 @@ | ||
import { HttpHandler, HttpRequest, HttpResponse } from "@http4t/core/contract"; | ||
import { HttpHandlerFun } from "../router"; | ||
import {HttpHandler, HttpRequest, HttpResponse} from "@http4t/core/contract"; | ||
export function toHttpHandler(handle: HttpHandlerFun): HttpHandler { | ||
export function toHttpHandler(handle: HttpHandler['handle']): HttpHandler { | ||
return new class implements HttpHandler { | ||
@@ -6,0 +5,0 @@ handle(request: HttpRequest): Promise<HttpResponse> { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74446
54
1125
1