Skip to content

Commit

Permalink
Deploy fix
Browse files Browse the repository at this point in the history
Fixes

Fixes

Fixes

Fixes

Fixes

Fixes
  • Loading branch information
Boldizsar Mezei committed Oct 23, 2023
1 parent 2b07b03 commit 2efefb5
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/action_deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ jobs:
credentials_json: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_SOONAVERSE_TEST }}'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
- name: Set env vars
working-directory: packages/functions
run: echo "$FUNCTIONS_ENV_VARS" >> .env
env:
FUNCTIONS_ENV_VARS: ${{ secrets.FUNCTIONS_ENV_VARS_TEST }}
- name: Build and deploy
run: |
npm run build:functions
npm run create-deploy-script
chmod 777 ./deploy.sh
./deploy.sh
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/action_deploy-wen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ jobs:
credentials_json: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_SOONAVERSE_TEST }}'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
- name: Set env vars
working-directory: packages/functions
run: echo "$FUNCTIONS_ENV_VARS" >> .env
env:
FUNCTIONS_ENV_VARS: ${{ secrets.FUNCTIONS_ENV_VARS_TEST }}
- name: Build and deploy
run: |
npm run build:functions
npm run create-deploy-script
chmod 777 ./deploy.sh
./deploy.sh
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,4 @@ dist
package-lock.json
packages/functions/scripts/*.json

/Dockerfile
deploy.sh
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"prettier:api": "npx prettier --write ./packages/api",
"prettier": "npm run prettier:functions; npm run prettier:interfaces; npm run prettier:indexes; npm run prettier:lib; npm run prettier:database; npm run prettier:api",
"joi-to-types": "ts-node ./scripts/joi-generator-post.ts && ts-node ./scripts/joi-generator-tangle.ts && npm run prettier",
"rebuild": "npm run clean && npm run build",
"rebuild": "npm run clean || npm run build",
"create-deploy-script": "ts-node packages/functions/deploy.script.ts"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/functions/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:18

WORKDIR api
WORKDIR functions

RUN apt-get -y update
RUN apt-get -y install libudev-dev
Expand Down
9 changes: 5 additions & 4 deletions packages/functions/deploy.script.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config({ path: __dirname + '/.env' });
import fs from 'fs';
import { flattenObject } from './src/common';
import { CloudFunctions } from './src/runtime/common';
Expand All @@ -9,17 +10,17 @@ import * as onTriggers from './src/runtime/trigger/index';
import { TriggeredFunction, TriggeredFunctionType } from './src/runtime/trigger/trigger';

const file = './deploy.sh';
fs.writeFileSync(file, 'cp packages/functions/Dockerfile ./Dockerfile\n\n');

fs.appendFileSync(file, 'gcloud builds submit --tag gcr.io/$GOOGLE_CLOUD_PROJECT/functions\n\n');

fs.appendFileSync(file, `export GOOGLE_CLOUD_PROJECT=$(gcloud config get-value project)\n\n`);
fs.writeFileSync(file, `export GOOGLE_CLOUD_PROJECT=$(gcloud config get-value project)\n\n`);

fs.appendFileSync(
file,
"export PROJECT_NUMBER=$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')\n\n",
);

fs.appendFileSync(file, 'cp packages/functions/Dockerfile ./Dockerfile\n\n');
fs.appendFileSync(file, 'gcloud builds submit --tag gcr.io/$GOOGLE_CLOUD_PROJECT/functions\n\n');

Object.entries({
...flattenObject(onRequests),
...flattenObject(onTriggers),
Expand Down
2 changes: 1 addition & 1 deletion packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"scripts": {
"lint": "eslint --ext .js,.ts src",
"build": "tsc",
"build": "tsc || cp .env lib/.env",
"emulators": "tsc --watch & firebase -c ../../firebase.json emulators:start --only functions,firestore,storage,ui,auth",
"milestone-sync": "npx ts-node ./test/milestone.sync.ts",
"serve": "run-p \"milestone-sync\" \"emulators\" ",
Expand Down
1 change: 1 addition & 0 deletions packages/functions/src/controls/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Context<T = undefined> {
owner: string;
params: T;
headers: any;
rawBody: any;
}

export interface UidSchemaObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { CommonJoi } from '../../services/joi/common';
export const fileUploadSchema = Joi.object({
member: CommonJoi.uid().description('Build5 id of the member.'),
uid: Joi.string().alphanum().max(100).required().description('Id for the file.'),
mimeType: Joi.string()
.regex(/^(image|video)/)
.required()
.description('Mime type of the file'),
})
.description('Request object to upload a file.')
.meta({
Expand Down
16 changes: 9 additions & 7 deletions packages/functions/src/controls/file/file.upload.control.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { build5Storage } from '@build-5/database';
import { Bucket, FileUploadRequest, WenError, generateRandomFileName } from '@build-5/interfaces';
import busboy from 'busboy';
import fs from 'fs';
import mime from 'mime-types';
import os from 'os';
import path from 'path';
import { build5Storage } from '@build-5/database';
import { getBucket } from '../../utils/config.utils';
import { invalidArgument } from '../../utils/error.utils';
import { assertValidationAsync } from '../../utils/schema.utils';
Expand All @@ -15,12 +15,12 @@ import { fileUploadSchema } from './FileUploadRequestSchema';

const MAX_FILE_SIZE_BYTES = 104857600; // 100 MB

export const uploadFileControl = async ({ headers }: Context<FileUploadRequest>) => {
export const uploadFileControl = async ({ headers, rawBody }: Context<FileUploadRequest>) => {
const workdir = `${os.tmpdir()}/${getRandomEthAddress()}`;
const params = await getParams(headers, workdir);
const params = await getParams(headers, rawBody, workdir);

const { member, uid, ext, mimeType } = params;
await assertValidationAsync(fileUploadSchema, { member, uid, mimeType });
const { member, uid, ext } = params;
await assertValidationAsync(fileUploadSchema, { member, uid });

const destination = `${member}/${uid}/${generateRandomFileName()}.${ext}`;
const bucketName = getBucket();
Expand All @@ -34,7 +34,7 @@ export const uploadFileControl = async ({ headers }: Context<FileUploadRequest>)
return { url: idDevBucket ? dowloadUrl : `https://${bucket.getName()}/${destination}` };
};

const getParams = (headers: any, workdir: string) =>
const getParams = (headers: any, rawBody: any, workdir: string) =>
new Promise<Record<string, unknown>>((res, rej) => {
const bb = busboy({ headers, limits: { fileSize: MAX_FILE_SIZE_BYTES } });
const params: Record<string, unknown> = {};
Expand Down Expand Up @@ -66,7 +66,9 @@ const getParams = (headers: any, workdir: string) =>
rej(invalidArgument(WenError.invalid_params));
return;
}
bb.end();

res(params);
});

bb.end(rawBody);
});
13 changes: 11 additions & 2 deletions packages/functions/src/index.express.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable import/namespace */

/* eslint-disable @typescript-eslint/no-var-requires */
require('dotenv').config({ path: __dirname + '/.env' });
import { WEN_FUNC } from '@build-5/interfaces';
import { HTTP } from 'cloudevents';
import cors from 'cors';
import express from 'express';
import { get } from 'lodash';
import { loadSync } from 'protobufjs';
Expand All @@ -18,9 +21,15 @@ import { TriggeredFunction } from './runtime/trigger/trigger';

const app = express();

app.use(cors());

// HTTPS
const rawParser = express.raw({ type: '*/*' });
const jsonParser = express.json();
Object.entries(flattenObject(onRequests)).forEach(([name, config]) => {
app.post(`/${name}`, express.json(), (req, res) => (config as HttpsFunction).func(req, res));
app.post(`/${name}`, name === WEN_FUNC.uploadFile ? rawParser : jsonParser, (req, res) =>
(config as HttpsFunction).func(req, res),
);
});

// TRIGGERS
Expand Down
1 change: 0 additions & 1 deletion packages/functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable import/namespace */

import * as functions from 'firebase-functions/v2';
import { flattenObject } from './common';
import { CloudFunctions, pathToParts } from './runtime/common';
Expand Down
5 changes: 3 additions & 2 deletions packages/functions/src/runtime/https/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ import { voteSchema } from '../../controls/vote/VoteRequestSchema';
import { voteControl } from '../../controls/vote/vote.control';
import { CommonJoi, toJoiObject } from '../../services/joi/common';
import { onRequest } from './https';
import { memberCreate } from './middlewares';
import { noAuth } from './middlewares';

exports[WEN_FUNC.createMember] = onRequest({
name: WEN_FUNC.updateMember,
schema: Joi.object({}),
middleware: memberCreate,
middleware: noAuth,
handler: createMemberControl,
});

Expand Down Expand Up @@ -497,5 +497,6 @@ exports[WEN_FUNC.uploadFile] = onRequest({
name: WEN_FUNC.uploadFile,
schema: Joi.object({}),
schemaOptions: { allowUnknown: true },
middleware: noAuth,
handler: uploadFileControl,
});
11 changes: 9 additions & 2 deletions packages/functions/src/runtime/https/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ export const auth = async (
const decoded = await decodeAuth(req.body.data, func);
const owner = decoded.address.toLowerCase();
const params = await assertValidationAsync(schema, decoded.body, options);
return { ip: req.ip || '', owner, params, headers: req.headers };
return {
ip: req.ip || '',
owner,
params,
headers: req.headers,
rawBody: req.body,
};
};

export const memberCreate = async (req: express.Request): Promise<Context<any>> => {
export const noAuth = async (req: express.Request): Promise<Context<any>> => {
return {
ip: req.ip || '',
owner: req.body.data,
params: {},
headers: req.headers,
rawBody: req.body,
};
};
11 changes: 4 additions & 7 deletions packages/functions/src/utils/config.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ import {
WEN_FUNC,
WenError,
} from '@build-5/interfaces';
import { get, isEmpty } from 'lodash';
import { isEmpty } from 'lodash';
import { invalidArgument } from './error.utils';

const getProjectId = () =>
get(JSON.parse(process.env.FIREBASE_CONFIG || '{}'), 'projectId', 'soonaverse-dev');
export const isProdEnv = () => getProjectId() === 'soonaverse';
export const isTestEnv = () => getProjectId() === 'soonaverse-test';
export const isEmulatorEnv = () => getProjectId() === 'soonaverse-dev';
export const isProdOrTestEnv = () => isProdEnv() || isTestEnv();
export const isProdEnv = () => process.env.ENVIRONMENT === 'prod';
export const isTestEnv = () => process.env.ENVIRONMENT === 'test';
export const isEmulatorEnv = () => !isProdEnv() && !isTestEnv();

export const getTokenSaleConfig = isProdEnv() ? TOKEN_SALE : TOKEN_SALE_TEST;

Expand Down

0 comments on commit 2efefb5

Please sign in to comment.