Skip to content

Commit

Permalink
feat(search-searvice): adding /sitemap REST endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushsatyam146 authored and hybridx committed Mar 27, 2023
1 parent 2e808a8 commit 05c44ad
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/search-service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SearchMapResolver } from './src/search-mapping/resolver';
import searchMapSchema from './src/search-mapping/typedef.graphql';
import database from './src/setup/database';
import { NODE_ENV, PORT } from './src/setup/env';
import { getSiteMap } from './src/search-sitemap/resolver';

(async function setConfig() {
/* Setup database connection */
Expand All @@ -21,6 +22,11 @@ import { NODE_ENV, PORT } from './src/setup/env';

const app = express();

app.get('/sitemap', async (req, res) => {
const data = await getSiteMap(req);
res.send(data);
});

app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));
const schema = mergeSchemas({
typeDefs: [searchConfigSchema, searchMapSchema],
Expand Down
21 changes: 21 additions & 0 deletions packages/search-service/src/search-sitemap/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Request } from 'express';
import { SiteMaps } from './schema';

export const getSiteMap = async (req: Request) => {
const { contentType } = req.query;

const sitemapData = (await SiteMaps.find()).map((item) => ({
url: item.url,
contentType: item.contentType,
lastModified: new Date().toISOString(),
}));

const filteredSitemapData = contentType
? sitemapData.filter((item) => item.contentType === contentType)
: sitemapData;

return {
count: filteredSitemapData.length,
data: filteredSitemapData,
};
};
14 changes: 14 additions & 0 deletions packages/search-service/src/search-sitemap/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
Document, Model, model, Schema,
} from 'mongoose';

export const SiteMapSchema: Schema = new Schema({
url: { type: String },
contentType: { type: String },
});

interface SiteMapModel extends Sitemap, Document {}

interface SiteMapStatic extends Model<SiteMapModel> {}

export const SiteMaps: Model<SiteMapModel> = model<SiteMapModel, SiteMapStatic>('SiteMap', SiteMapSchema);
4 changes: 4 additions & 0 deletions packages/search-service/src/search-sitemap/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare type Sitemap = {
url: string;
contentType: string;
};

0 comments on commit 05c44ad

Please sign in to comment.