Skip to content

Commit 4f20866

Browse files
authored
Merge pull request #617 from dzcode-io/prepare-meilisearch
Add MeiliSearch service
2 parents 185ddec + 926fdde commit 4f20866

File tree

9 files changed

+106
-9
lines changed

9 files changed

+106
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ npm run dev:api
6161
npm run dev:all
6262
```
6363

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

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

7171
```.env
7272
GITHUB_TOKEN=Paste_You_Token_Here
73+
NODE_ENV=development
7374
```
7475

7576
### Run e2e locally

api/docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ services:
99
environment:
1010
POSTGRES_HOST_AUTH_METHOD: trust
1111
POSTGRES_DB: db
12+
13+
meilisearch:
14+
image: getmeili/meilisearch:latest
15+
ports:
16+
- "7700:7700"
17+
volumes:
18+
- ./meilisearch_db:/data.ms
19+
environment:
20+
MEILI_NO_ANALYTICS: true
21+
MEILI_MASTER_KEY: "default"

api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"helmet": "^7.1.0",
2626
"lodash": "^4.17.21",
2727
"make-fetch-happen": "^13.0.1",
28+
"meilisearch": "^0.46.0",
2829
"morgan": "^1.10.0",
2930
"postgres": "^3.4.4",
3031
"reflect-metadata": "^0.2.2",

api/src/app/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@
33
import "reflect-metadata";
44
import "src/_utils/setup-sentry";
55

6-
import { fsConfig } from "@dzcode.io/utils/dist/config";
76
import * as Sentry from "@sentry/node";
7+
8+
import {
9+
RoutingControllersOptions,
10+
createExpressServer,
11+
useContainer,
12+
} from "routing-controllers";
13+
814
import { Application } from "express";
9-
import { createExpressServer, RoutingControllersOptions, useContainer } from "routing-controllers";
1015
import { ConfigService } from "src/config/service";
16+
import Container from "typedi";
1117
import { ContributionController } from "src/contribution/controller";
1218
import { ContributorController } from "src/contributor/controller";
1319
import { DigestCron } from "src/digest/cron";
1420
import { GithubController } from "src/github/controller";
21+
import { LoggerMiddleware } from "./middlewares/logger";
1522
import { LoggerService } from "src/logger/service";
1623
import { MilestoneController } from "src/milestone/controller";
17-
import { ProjectController } from "src/project/controller";
1824
import { PostgresService } from "src/postgres/service";
19-
import Container from "typedi";
20-
21-
import { LoggerMiddleware } from "./middlewares/logger";
25+
import { ProjectController } from "src/project/controller";
2226
import { RobotsController } from "./middlewares/robots";
27+
import { SearchController } from "src/search/controller";
2328
import { SecurityMiddleware } from "./middlewares/security";
29+
import { fsConfig } from "@dzcode.io/utils/dist/config";
2430

2531
// Use typedi container
2632
useContainer(Container); // eslint-disable-line react-hooks/rules-of-hooks
@@ -45,6 +51,7 @@ useContainer(Container); // eslint-disable-line react-hooks/rules-of-hooks
4551
ProjectController,
4652
ContributorController,
4753
RobotsController,
54+
SearchController,
4855
],
4956
middlewares: [SecurityMiddleware, LoggerMiddleware],
5057
cors: Container.get(SecurityMiddleware).cors(),

api/src/config/types.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { Environment, environments } from "@dzcode.io/utils/dist/config/environment";
2-
import { Expose } from "class-transformer";
1+
import {
2+
Environment,
3+
environments,
4+
} from "@dzcode.io/utils/dist/config/environment";
35
import { IsOptional, IsString, Matches } from "class-validator";
6+
7+
import { Expose } from "class-transformer";
48
import { readFileSync } from "fs-extra";
59

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

3337
@IsOptional()
3438
BUNDLE_INFO: { version: string } = bundleInfo;
39+
40+
@Expose()
41+
get MEILISEARCH_URL() {
42+
return this.NODE_ENV === "development"
43+
? "http://localhost:7700/"
44+
: "http://meilisearch:7700/";
45+
}
46+
47+
MEILISEARCH_MASTER_KEY = "default";
3548
}

api/src/search/controller.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Controller, Get } from "routing-controllers";
2+
3+
import { GetSearchResponse } from "./types";
4+
import { SearchService } from "./service";
5+
import { Service } from "typedi";
6+
7+
@Service()
8+
@Controller("/Search")
9+
export class SearchController {
10+
constructor(private readonly searchService: SearchService) {}
11+
12+
@Get("/")
13+
public async search(): Promise<GetSearchResponse> {
14+
const searchResults = await this.searchService.search("test");
15+
return {
16+
searchResults,
17+
};
18+
}
19+
}

api/src/search/service.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ConfigService } from "src/config/service";
2+
import { LoggerService } from "src/logger/service";
3+
import { MeiliSearch } from "meilisearch";
4+
import { SearchItem } from "./types";
5+
import { Service } from "typedi";
6+
7+
@Service()
8+
export class SearchService {
9+
private readonly meilisearch: MeiliSearch;
10+
constructor(
11+
private readonly configService: ConfigService,
12+
private readonly logger: LoggerService,
13+
) {
14+
const { MEILISEARCH_URL, MEILISEARCH_MASTER_KEY } =
15+
this.configService.env();
16+
17+
this.meilisearch = new MeiliSearch({
18+
host: MEILISEARCH_URL,
19+
apiKey: MEILISEARCH_MASTER_KEY,
20+
});
21+
}
22+
23+
public search = async (query: string): Promise<SearchItem[]> => {
24+
this.logger.info({ message: `Searching for ${query}` });
25+
return [];
26+
};
27+
}

api/src/search/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { GeneralResponse } from "src/app/types";
2+
3+
export interface GetSearchResponse extends GeneralResponse {
4+
searchResults: Array<SearchItem>;
5+
}
6+
7+
export interface SearchItem {
8+
id: string;
9+
title: string;
10+
type: SearchType;
11+
}
12+
13+
type SearchType = "project" | "contribution" | "contributor";

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)