Skip to content

Commit

Permalink
Merge branch 'dev' into live-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gvelez17 authored Nov 25, 2024
2 parents d1c6c04 + 0f1b05f commit 37340ae
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 41 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,12 @@ Value for `ACCESS_SECRET` and `REFRESH_SECRET` can be anything.

<a name="Review"></a>

## To review the server files
## Prod deployment is manual

SSH into the server with the private key. If you don't have the key, ask for it in slack.

```
Check vault for ssh creds, url is inserted above
```

cd into the project

```
cd /data/trust_claim_backend
Check vault for ssh creds to live.linkedtrust.us
```

inspect the running file
Expand All @@ -218,13 +212,17 @@ pm2 status index
pm2 logs index
```

Run this command to check for possible changes in packages, and install changed packages.
### Update from git and install dependencies

```
cd /data/trust_claim_backend
git pull
npm i
```

If there is any database migration, it is a good idea to backup the database.
### If required, database migration

If there is any database migration, it is a good idea to backup the database, otherwise you may skip this step.

```
sudo su postgres
Expand All @@ -238,12 +236,16 @@ npx prisma generate
npx prisma migrate deploy
```

### Rebuild with changes

Then, building the project is enough, because `pm2` is watching for changes.

```
npm run build
```

### DONE. Troubleshooting:

NOTE: Run this ONLY when the server is down

```
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@glazed/datamodel": "^0.3.0",
"@glazed/devtools": "^0.2.0",
"@glazed/did-datastore": "^0.3.1",
"@prisma/client": "^5.17.0",
"@prisma/client": "5.22.0",
"axios": "^0.27.2",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
Expand Down Expand Up @@ -77,7 +77,7 @@
"jest-mock-extended": "^2.0.4",
"nodemon": "^2.0.16",
"prettier": "^3.3.3",
"prisma": "^5.17.0",
"prisma": "5.22.0",
"renamer": "^4.0.0",
"supertest": "^6.3.3",
"swagger-jsdoc": "^6.2.8",
Expand Down
98 changes: 73 additions & 25 deletions src/utils/aws-s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,89 @@ import {
PutObjectCommand,
PutObjectCommandInput,
S3Client,
S3ServiceException,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { config } from "../config";
import { optimizeImage } from "./images";

export const s3Client = new S3Client({
region: config.s3.region,
credentials: {
accessKeyId: config.s3.accessKeyId,
secretAccessKey: config.s3.secretAccessKey,
},
});
// Helper to check if S3 configuration is complete
function isS3Configured(): boolean {
return !!(
config?.s3 &&
config.s3?.region &&
config.s3?.accessKeyId &&
config.s3?.secretAccessKey &&
config.s3?.bucketName
);
}

// Initialize S3 client only if properly configured
export const s3Client: S3Client | null = isS3Configured()
? new S3Client({
region: config.s3.region,
credentials: {
accessKeyId: config.s3.accessKeyId,
secretAccessKey: config.s3.secretAccessKey,
},
})
: null;

export async function uploadImageToS3(filename: string, file: Express.Multer.File) {
const optimizedImage = await optimizeImage(file.buffer);
const params: PutObjectCommandInput = {
Key: filename,
Body: optimizedImage,
ContentType: file.mimetype,
Bucket: config.s3.bucketName,
};
const command = new PutObjectCommand(params);
return s3Client.send(command);
export async function uploadImageToS3(
filename: string,
file: Express.Multer.File
): Promise<void> {
// If S3 is not configured, return early
if (!s3Client || !isS3Configured()) {
console.warn('S3 is not configured. Skipping upload.');
return;
}

try {
const optimizedImage = await optimizeImage(file.buffer);
const params: PutObjectCommandInput = {
Key: filename,
Body: optimizedImage,
ContentType: file.mimetype,
Bucket: config.s3.bucketName,
};
const command = new PutObjectCommand(params);
await s3Client.send(command);
} catch (error) {
console.error('Error uploading to S3:', error);
if (error instanceof S3ServiceException) {
throw new Error(`Failed to upload image to S3: ${error.message}`);
}
throw new Error('Failed to upload image to S3');
}
}

export function getS3SignedUrl(filename: string): Promise<string> {
const params: GetObjectCommandInput = {
Key: filename,
Bucket: config.s3.bucketName,
};
const command = new GetObjectCommand(params);
return getSignedUrl(s3Client, command, { expiresIn: 3600 });
export async function getS3SignedUrl(filename: string): Promise<string | null> {
// If S3 is not configured, return null
if (!s3Client || !isS3Configured()) {
console.warn('S3 is not configured. Cannot generate signed URL.');
return null;
}

try {
const params: GetObjectCommandInput = {
Key: filename,
Bucket: config.s3.bucketName,
};
const command = new GetObjectCommand(params);
return await getSignedUrl(s3Client, command, { expiresIn: 3600 });
} catch (error) {
console.error('Error generating signed URL:', error);
if (error instanceof S3ServiceException) {
throw new Error(`Failed to generate signed URL: ${error.message}`);
}
throw new Error('Failed to generate signed URL');
}
}

export async function getS3SignedUrlIfExisted(filename?: string | null): Promise<string | null> {
export async function getS3SignedUrlIfExisted(
filename?: string | null
): Promise<string | null> {
if (!filename) return null;
return getS3SignedUrl(filename);
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5717,10 +5717,10 @@ printj@^1.3.0:
resolved "https://registry.npmjs.org/printj/-/printj-1.3.1.tgz"
integrity sha512-GA3TdL8szPK4AQ2YnOe/b+Y1jUFwmmGMMK/qbY7VcE3Z7FU8JstbKiKRzO6CIiAKPhTO8m01NoQ0V5f3jc4OGg==

prisma@*, prisma@^5.17.0:
version "5.21.1"
resolved "https://registry.npmjs.org/prisma/-/prisma-5.21.1.tgz"
integrity sha512-PB+Iqzld/uQBPaaw2UVIk84kb0ITsLajzsxzsadxxl54eaU5Gyl2/L02ysivHxK89t7YrfQJm+Ggk37uvM70oQ==
prisma@5.22.0:
version "5.22.0"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-5.22.0.tgz#1f6717ff487cdef5f5799cc1010459920e2e6197"
integrity sha512-vtpjW3XuYCSnMsNVBjLMNkTj6OZbudcPPTPYHqX0CJfpcdWciI1dM8uHETwmDxxiqEwCIE6WvXucWUetJgfu/A==
dependencies:
"@prisma/engines" "5.21.1"
optionalDependencies:
Expand Down

0 comments on commit 37340ae

Please sign in to comment.