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

closing client connection #9

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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
20 changes: 11 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import "dotenv/config";
import env from "../util/validateEnv";
import express from "express";
import { OpensearchClient, Initialize } from "./client";
import { Initialize } from "./client";
import QueryBody from "./querybody";
import { collectDefaultMetrics, register } from "prom-client";
import createHttpError from "http-errors";
import { errorHandler } from "./middleware/errorHandler";
import { Config } from "./config";

const app = express();
const client = OpensearchClient;
Initialize(client);

collectDefaultMetrics({ register });

register.setDefaultLabels({ app: 'opensearch-api' });


app.get('/', (req, res) =>
{
res.send('Express + TypeScript Server');
});

app.get('/api/search', async (req, res, next) =>
{
//initialize client
const client = await Initialize();
try {
//check if we recieved the query header
if (!req.headers.query || typeof req.headers.query !== 'string') {
Expand All @@ -33,17 +31,20 @@ app.get('/api/search', async (req, res, next) =>
//structure the opensearch query
const queryBody: QueryBody = new QueryBody(queryString, 5, 0);
//actually use the query
const searchRes = await client.search({
const searchRes = await client?.search({
index: env.INDEX_NAME,
body: queryBody
});
//respond with opensearch response
res.status(200).json(searchRes.body);
res.status(200).json(searchRes?.body);
}
catch (error) {
console.error(error);
next(error);
}
finally {
await client?.close();
}
});

app.get('/api/health', async (req, res, next) =>
Expand All @@ -60,7 +61,8 @@ app.get('/api/health', async (req, res, next) =>
}
});

app.get('/api/config', async (req, res, next) => {
app.get('/api/config', async (req, res, next) =>
{
try {
const config = new Config();
res.status(200).json(config);
Expand All @@ -69,7 +71,7 @@ app.get('/api/config', async (req, res, next) => {
console.error(error);
next(error);
}
})
});

app.use(errorHandler);

Expand Down
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Client } from "@opensearch-project/opensearch";
import env from "../util/validateEnv";

export const OpensearchClient = new Client({ node: env.CONNECTION_STRING });

export async function Initialize(opensearchClient: Client)
export async function Initialize()
{
try {
const opensearchClient = new Client({ node: env.CONNECTION_STRING });
console.log("Testing connection to OpensearchDB...");
await opensearchClient.ping();
console.log("Opensearch server is reachable");
return opensearchClient;
} catch (error) {
console.log("Error connecting to opensearch database");
console.error(error);
Expand Down
Loading