Skip to content

Commit

Permalink
Merge pull request #617 from dzcode-io/prepare-meilisearch
Browse files Browse the repository at this point in the history
Add MeiliSearch service
  • Loading branch information
omdxp authored Dec 19, 2024
2 parents 185ddec + 926fdde commit 4f20866
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ npm run dev:api
npm run dev:all
```

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

**Note**
Expand All @@ -70,6 +70,7 @@ In [`./api`](./api), keep in mind that you have limited calls to Github Api (60

```.env
GITHUB_TOKEN=Paste_You_Token_Here
NODE_ENV=development
```

### Run e2e locally
Expand Down
10 changes: 10 additions & 0 deletions api/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ services:
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_DB: db

meilisearch:
image: getmeili/meilisearch:latest
ports:
- "7700:7700"
volumes:
- ./meilisearch_db:/data.ms
environment:
MEILI_NO_ANALYTICS: true
MEILI_MASTER_KEY: "default"
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"helmet": "^7.1.0",
"lodash": "^4.17.21",
"make-fetch-happen": "^13.0.1",
"meilisearch": "^0.46.0",
"morgan": "^1.10.0",
"postgres": "^3.4.4",
"reflect-metadata": "^0.2.2",
Expand Down
19 changes: 13 additions & 6 deletions api/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
import "reflect-metadata";
import "src/_utils/setup-sentry";

import { fsConfig } from "@dzcode.io/utils/dist/config";
import * as Sentry from "@sentry/node";

import {
RoutingControllersOptions,
createExpressServer,
useContainer,
} from "routing-controllers";

import { Application } from "express";
import { createExpressServer, RoutingControllersOptions, useContainer } from "routing-controllers";
import { ConfigService } from "src/config/service";
import Container from "typedi";
import { ContributionController } from "src/contribution/controller";
import { ContributorController } from "src/contributor/controller";
import { DigestCron } from "src/digest/cron";
import { GithubController } from "src/github/controller";
import { LoggerMiddleware } from "./middlewares/logger";
import { LoggerService } from "src/logger/service";
import { MilestoneController } from "src/milestone/controller";
import { ProjectController } from "src/project/controller";
import { PostgresService } from "src/postgres/service";
import Container from "typedi";

import { LoggerMiddleware } from "./middlewares/logger";
import { ProjectController } from "src/project/controller";
import { RobotsController } from "./middlewares/robots";
import { SearchController } from "src/search/controller";
import { SecurityMiddleware } from "./middlewares/security";
import { fsConfig } from "@dzcode.io/utils/dist/config";

// Use typedi container
useContainer(Container); // eslint-disable-line react-hooks/rules-of-hooks
Expand All @@ -45,6 +51,7 @@ useContainer(Container); // eslint-disable-line react-hooks/rules-of-hooks
ProjectController,
ContributorController,
RobotsController,
SearchController,
],
middlewares: [SecurityMiddleware, LoggerMiddleware],
cors: Container.get(SecurityMiddleware).cors(),
Expand Down
17 changes: 15 additions & 2 deletions api/src/config/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Environment, environments } from "@dzcode.io/utils/dist/config/environment";
import { Expose } from "class-transformer";
import {
Environment,
environments,
} from "@dzcode.io/utils/dist/config/environment";
import { IsOptional, IsString, Matches } from "class-validator";

import { Expose } from "class-transformer";
import { readFileSync } from "fs-extra";

let bundleInfo = { version: require("../../package.json").version }; // eslint-disable-line @typescript-eslint/no-require-imports
Expand Down Expand Up @@ -32,4 +36,13 @@ export class EnvRecord {

@IsOptional()
BUNDLE_INFO: { version: string } = bundleInfo;

@Expose()
get MEILISEARCH_URL() {
return this.NODE_ENV === "development"
? "http://localhost:7700/"
: "http://meilisearch:7700/";
}

MEILISEARCH_MASTER_KEY = "default";
}
19 changes: 19 additions & 0 deletions api/src/search/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Controller, Get } from "routing-controllers";

import { GetSearchResponse } from "./types";
import { SearchService } from "./service";
import { Service } from "typedi";

@Service()
@Controller("/Search")
export class SearchController {
constructor(private readonly searchService: SearchService) {}

@Get("/")
public async search(): Promise<GetSearchResponse> {
const searchResults = await this.searchService.search("test");
return {
searchResults,
};
}
}
27 changes: 27 additions & 0 deletions api/src/search/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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()
export class SearchService {
private readonly meilisearch: MeiliSearch;
constructor(
private readonly configService: ConfigService,
private readonly logger: LoggerService,
) {
const { MEILISEARCH_URL, MEILISEARCH_MASTER_KEY } =
this.configService.env();

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

public search = async (query: string): Promise<SearchItem[]> => {
this.logger.info({ message: `Searching for ${query}` });
return [];
};
}
13 changes: 13 additions & 0 deletions api/src/search/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GeneralResponse } from "src/app/types";

export interface GetSearchResponse extends GeneralResponse {
searchResults: Array<SearchItem>;
}

export interface SearchItem {
id: string;
title: string;
type: SearchType;
}

type SearchType = "project" | "contribution" | "contributor";
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4f20866

Please sign in to comment.