Skip to content

Commit

Permalink
fix: Adopt shared storage class and consolidate usage
Browse files Browse the repository at this point in the history
Removes storage.ts from backend in favor of the version in the shared package.  Migrated AWS SDK v3 changes to the shared package.  Cleaned up some DI mess.
  • Loading branch information
baumandm committed Mar 20, 2023
1 parent f4f16e4 commit b677dfa
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 231 deletions.
2 changes: 0 additions & 2 deletions package-lock.json

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

1 change: 0 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
},
"dependencies": {
"@aws-sdk/client-s3": "3.292.0",
"@aws-sdk/client-sqs": "3.292.0",
"@elastic/elasticsearch": "7.13.0",
"@octokit/graphql": "4.8.0",
"@octokit/rest": "19.0.7",
Expand Down
6 changes: 4 additions & 2 deletions packages/backend/src/controllers/avatars.v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

import { NoSuchKey } from '@aws-sdk/client-s3';
import { getLogger } from '@iex/shared/logger';
import { Storage } from '@iex/shared/storage';
import { Response, Request } from 'express';
import Container from 'typedi';

import { streamFromS3 } from '../lib/storage';
import { getType } from '../shared/mime';

const logger = getLogger('avatars.v1');
Expand All @@ -32,7 +33,8 @@ export const getAvatar = async (req: Request, res: Response): Promise<void> => {
logger.debug(`Attempting to load avatar: ${path}`);

try {
const readable = await streamFromS3(path);
const storage = Container.get(Storage);
const readable = await storage.streamFile({ path });

if (req.query['content-type']) {
res.contentType(req.query['content-type'] as string);
Expand Down
7 changes: 4 additions & 3 deletions packages/backend/src/controllers/drafts.v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import { NoSuchKey } from '@aws-sdk/client-s3';
import { getLogger } from '@iex/shared/logger';
import { Storage } from '@iex/shared/storage';
import { Response, Request } from 'express';

import { streamFromS3 } from '../lib/storage';
import Container from 'typedi';

const logger = getLogger('drafts.v1');

Expand All @@ -30,7 +30,8 @@ export const getDraftAttachment = async (req: Request, res: Response): Promise<v
logger.debug(`Attempting to load draft attachment: ${draftKey}, ${req.params.attachmentKey}`);

try {
const readable = await streamFromS3(draftKey);
const storage = Container.get(Storage);
const readable = await storage.streamFile({ path: draftKey });

if (req.query['content-type']) {
res.contentType(req.query['content-type'] as string);
Expand Down
13 changes: 8 additions & 5 deletions packages/backend/src/controllers/insights.v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import { Readable } from 'node:stream';

import { NoSuchKey } from '@aws-sdk/client-s3';
import { getLogger } from '@iex/shared/logger';
import { Storage } from '@iex/shared/storage';
import { Response, Request } from 'express';
import Container from 'typedi';

import { getFromS3, headFromS3 } from '../lib/storage';
import { getType } from '../shared/mime';

const logger = getLogger('insights.v1');
Expand All @@ -30,11 +31,12 @@ const logger = getLogger('insights.v1');
*/
export const headInsightFile = async (req: Request, res: Response): Promise<void> => {
const filePath = req.params.filepath + req.params[0];
const key = `insights/${req.params.namespace}/${req.params.name}/files/${filePath}`;
const path = `insights/${req.params.namespace}/${req.params.name}/files/${filePath}`;

logger.debug(`Attempting to HEAD insight attachment: ${filePath}`);

const headObject = await headFromS3(key);
const storage = Container.get(Storage);
const headObject = await storage.rawHead({ path });

if (headObject === undefined) {
res.status(404);
Expand All @@ -55,14 +57,15 @@ export const headInsightFile = async (req: Request, res: Response): Promise<void
*/
export const getInsightFile = async (req: Request, res: Response): Promise<void> => {
const filePath = req.params.filepath + req.params[0];
const key = `insights/${req.params.namespace}/${req.params.name}/files/${filePath}`;
const path = `insights/${req.params.namespace}/${req.params.name}/files/${filePath}`;

logger.debug(`Attempting to load insight attachment: ${filePath}`);

const range = Array.isArray(req.headers['range']) ? req.headers['range'][0] : req.headers['range'];

try {
const response = await getFromS3(key, range);
const storage = Container.get(Storage);
const response = await storage.rawGet({ path, range });

if (response.AcceptRanges) {
res.set('Accept-Ranges', response.AcceptRanges);
Expand Down
5 changes: 5 additions & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import { deployMappings } from './lib/elasticsearch';
import { createServer } from './server';
import { syncExampleInsights } from './lib/init';
import { getLogger } from '@iex/shared/logger';
import { Storage } from '@iex/shared/storage';
import Container from 'typedi';

const logger = getLogger('index');

Expand All @@ -53,6 +55,9 @@ process.on('uncaughtException', (uncaughtException) => {
});

const startup = async (): Promise<Server> => {
// Add storage class to the Container since it doesn't use an annotation
Container.set(Storage, new Storage());

// Deploy Elasticsearch Index mappings
logger.debug('Deploying elasticsearch indices');
await pRetry(() => deployMappings(), {
Expand Down
7 changes: 5 additions & 2 deletions packages/backend/src/lib/backends/file-system.sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import { PersonType } from '@iex/models/person-type';
import { RepositoryType } from '@iex/models/repository-type';
import { MessageQueue } from '@iex/mq/message-queue';
import { getLogger } from '@iex/shared/logger';
import { Storage } from '@iex/shared/storage';
import { nanoid } from 'nanoid';
import pMap from 'p-map';
import readingTime from 'reading-time';
import Container from 'typedi';

import { InsightYaml } from '@iex/backend/models/insight-yaml';

Expand All @@ -33,7 +35,6 @@ import { ActivityService } from '../../services/activity.service';
import { UserService } from '../../services/user.service';
import { getTypeAsync } from '../../shared/mime';
import { GitInstance, INSIGHT_YAML_FILE } from '../git-instance';
import { writeToS3 } from '../storage';

import { BaseSync, INDEXABLE_MIME_TYPES, READONLY_FILES, THUMBNAIL_LOCATIONS } from './base.sync';

Expand Down Expand Up @@ -325,6 +326,8 @@ const syncFiles = async (
insight: IndexedInsight,
previousInsight: IndexedInsight | null
): Promise<void> => {
const storage = Container.get(Storage);

// Message Queue for converting files
const conversionMq = new MessageQueue({ region: process.env.S3_REGION, queueUrl: process.env.CONVERSION_SQS_URL });

Expand Down Expand Up @@ -362,7 +365,7 @@ const syncFiles = async (

const targetS3Path = `insights/${insight.fullName}/files/${wf.path}`;

await writeToS3(contents!, targetS3Path);
await storage.writeFile({ body: contents!, path: targetS3Path });

if (file.conversions !== undefined) {
// Submit a conversion request for this file
Expand Down
6 changes: 4 additions & 2 deletions packages/backend/src/lib/backends/github.sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import { PersonType } from '@iex/models/person-type';
import { RepositoryType } from '@iex/models/repository-type';
import { MessageQueue } from '@iex/mq/message-queue';
import { getLogger } from '@iex/shared/logger';
import { Storage } from '@iex/shared/storage';
import { nanoid } from 'nanoid';
import pMap from 'p-map';
import readingTime from 'reading-time';
import Container from 'typedi';

import { GitInstance, INSIGHT_YAML_FILE } from '../../lib/git-instance';
import { writeToS3 } from '../../lib/storage';
import { Insight } from '../../models/insight';
import { InsightFile, InsightFileConversion } from '../../models/insight-file';
import { InsightYaml } from '../../models/insight-yaml';
Expand Down Expand Up @@ -452,6 +452,8 @@ const syncFiles = async (
insight: IndexedInsight,
previousInsight: IndexedInsight | null
): Promise<void> => {
const storage = Container.get(Storage);

// Message Queue for converting files
const conversionMq = new MessageQueue({ region: process.env.S3_REGION, queueUrl: process.env.CONVERSION_SQS_URL });

Expand Down Expand Up @@ -489,7 +491,7 @@ const syncFiles = async (

const targetS3Path = `insights/${insight.fullName}/files/${wf.path}`;

await writeToS3(contents!, targetS3Path);
await storage.writeFile({ body: contents!, path: targetS3Path });

if (file.conversions !== undefined) {
// Submit a conversion request for this file
Expand Down
5 changes: 2 additions & 3 deletions packages/backend/src/lib/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import { nanoid } from 'nanoid';
import { parse as htmlParse, HTMLElement } from 'node-html-parser';
import TurndownService from 'turndown';
import * as turndownPluginGfm from 'turndown-plugin-gfm';
import Container from 'typedi';

import { DraftDataInput, DraftKey } from '../models/draft';
import { AttachmentService } from '../services/attachment.service';
import { DraftService } from '../services/draft.service';
import { TemplateService } from '../services/template.service';

const logger = getLogger('import');

Expand Down Expand Up @@ -194,7 +193,7 @@ export function convertToDraft(request: ImportRequest): DraftDataInput {
export async function importToNewDraft(request: ImportRequest): Promise<DraftKey> {
logger.info(`Importing web page ${request.url}`);

const draftService = new DraftService(new AttachmentService(), new TemplateService());
const draftService = Container.get(DraftService);
const draftKey = draftService.newDraftKey();

const draftData = convertToDraft(request);
Expand Down
163 changes: 0 additions & 163 deletions packages/backend/src/lib/storage.ts

This file was deleted.

Loading

0 comments on commit b677dfa

Please sign in to comment.