Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ npm run dev:all

- For api server go to <http://localhost:7070>
- For web server go to <http://localhost:8080>
- For search server go to <http://localhost:7700>

**Note**

Expand Down
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"lint:prettier": "prettier --config ../packages/tooling/.prettierrc --ignore-path ../packages/tooling/.prettierignore --log-level warn",
"lint:ts-prune": "tsx ../packages/tooling/setup-ts-prune.ts && ts-prune --error",
"lint:tsc": "tspc --noEmit",
"start": "wait-port postgres:5432 && delay 2 && node dist/app/index.js",
"start": "wait-port postgres:5432 && wait-port meilisearch:7700 && delay 2 && node dist/app/index.js",
"start:dev": "tsx ../packages/tooling/nodemon.ts \"@dzcode.io/api\" && npm-run-all --parallel start:nodemon db:server",
"start:nodemon": "wait-port localhost:5432 && delay 2 && nodemon dist/app/index.js",
"test": "npm run build && npm run test:alone",
Expand Down
4 changes: 4 additions & 0 deletions api/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PostgresService } from "src/postgres/service";
import { ProjectController } from "src/project/controller";
import { RobotsController } from "./middlewares/robots";
import { SearchController } from "src/search/controller";
import { SearchService } from "src/search/service";
import { SecurityMiddleware } from "./middlewares/security";
import { fsConfig } from "@dzcode.io/utils/dist/config";

Expand All @@ -38,6 +39,9 @@ useContainer(Container); // eslint-disable-line react-hooks/rules-of-hooks

const { NODE_ENV, PORT } = Container.get(ConfigService).env();

// Initialize Search Service
Container.get(SearchService);

// Add crons to DI container
const CronServices = [DigestCron];
CronServices.forEach((service) => Container.get(service));
Expand Down
61 changes: 60 additions & 1 deletion api/src/search/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SearchItem, SearchType } from "./types";

import { ConfigService } from "src/config/service";
import { LoggerService } from "src/logger/service";
import { MeiliSearch } from "meilisearch";
import { SearchItem } from "./types";
import { Service } from "typedi";

@Service()
Expand All @@ -11,17 +12,75 @@ export class SearchService {
private readonly configService: ConfigService,
private readonly logger: LoggerService,
) {
this.logger.info({ message: "Initializing MeiliSearch client" });
const { MEILISEARCH_URL, MEILISEARCH_MASTER_KEY } =
this.configService.env();

this.meilisearch = new MeiliSearch({
host: MEILISEARCH_URL,
apiKey: MEILISEARCH_MASTER_KEY,
});

this.logger.info({
message: `MeiliSearch client initialized with url ${MEILISEARCH_URL}`,
});

this.meilisearch
.createIndex("project")
.then(() => {
this.logger.info({ message: "project index created" });
})
.catch((error) => {
this.logger.error({
message: `failed to create project index: ${error.message}`,
});
});

this.meilisearch
.createIndex("contribution")
.then(() => {
this.logger.info({ message: "contribution index created" });
})
.catch((error) => {
this.logger.error({
message: `failed to create contribution index: ${error.message}`,
});
});

this.meilisearch
.createIndex("contributor")
.then(() => {
this.logger.info({ message: "contributor index created" });
})
.catch((error) => {
this.logger.error({
message: `failed to create contributor index: ${error.message}`,
});
});
}

public search = async (query: string): Promise<SearchItem[]> => {
this.logger.info({ message: `Searching for ${query}` });
return [];
};

public index = async (
index: SearchType,
data: SearchItem[],
): Promise<void> => {
this.logger.info({ message: `Indexing ${data.length} items in ${index}` });
this.meilisearch
.index(index)
.addDocuments(data)
.then(() => {
this.logger.info({
message: `Indexed ${data.length} items in ${index}`,
});
})
.catch((error) => {
this.logger.error({
message: `failed to index ${data.length} items in ${index}: ${error.message}`,
});
});
};
}
2 changes: 1 addition & 1 deletion api/src/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export interface SearchItem {
type: SearchType;
}

type SearchType = "project" | "contribution" | "contributor";
export type SearchType = "project" | "contribution" | "contributor";
Loading