Skip to content

Commit

Permalink
Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
Boldizsar Mezei committed Oct 26, 2023
2 parents 07370b2 + 52cf7d5 commit ea5a7d1
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 103 deletions.
20 changes: 10 additions & 10 deletions packages/functions/.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ENVIRONMENT="emulator"
WEB3_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweEVFMjA3Qzk5YzA2NzkxMDg0QjU5NmU4NkEyMzVGNzFiNTc0NWNkMTIiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2Njg0Njc4MDQ3NDAsIm5hbWUiOiJERVZfVE9LRU4ifQ.TB2AbWZDMk4WhWIeWKsdEKUReXUh0WUgcHAU5ccb4FA"
JWT_SECRET="mysecret"
ALGOLIA_APPID="UZXKW1YS76"
ALGOLIA_KEY="8bf460848691fae9111b6159867d6bc1"
ENCRYPTION_SALT="sa#asda!2sasd##asad"
IP_INFO_TOKEN=""
XPTOKEN_ID="0x08f800d9e15c1da60c36cb0b2d4a02366ea3e200a65fc071a9e25f09b7fb9e951f0100000000"
XPTOKEN_UID="0xcef8ddcea97a5b82921d1cadbc8ccddcd69341da"
XPTOKEN_GUARDIANID="0x45f8379c44a73fc0ee6ad56acf51bd0f69240af5"
ENVIRONMENT=test
WEB3_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweEZCOTFiNTdEN2YzNmVhNjQ4NjQ3ODIyQjJGNGVEOEEyMDZCNERhNjQiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2Njc0MDYzNzE3NDksIm5hbWUiOiJ0ZXN0IGRldiJ9.88vd8ZmeEle2Xqyc8uEMBOXJqDrFcxxF8gyHuXIjXgk
JWT_SECRET=asdas#@#@xdas31sad
ALGOLIA_APPID=6MPUETJRDB
ALGOLIA_KEY=a75153c20ebe86d31e1fe5874f55dbac
ENCRYPTION_SALT=c4kWxCtNVQ5c2m
IP_INFO_TOKEN=''
XPTOKEN_ID=0x08fe43472f5968c4ccfa7154599e3d9c8df3f2e0b6396cafc2b038f15ee1974e050100000000
XPTOKEN_UID=0xe71439be7001d2311658ded364e4043a284d16f4
XPTOKEN_GUARDIANID=0x551fd2c7c7bf356bac194587dab2fcd46420054b
1 change: 1 addition & 0 deletions packages/functions/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ COPY packages/database packages/database
COPY packages/interfaces packages/interfaces
COPY packages/functions packages/functions
COPY package.json ./
COPY data.proto ./

RUN npm run build:functions

Expand Down
186 changes: 101 additions & 85 deletions packages/functions/deploy.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,53 @@ fs.appendFileSync(
"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),
...flattenObject(onScheduled),
...flattenObject(onStorage),
}).forEach(([name, value]) => {
const options = (value as CloudFunctions).runtimeOptions;

let command = `gcloud run deploy ${name} \\
const buildImage = () => {
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');
};

const deployServices = () => {
Object.entries({
...flattenObject(onRequests),
...flattenObject(onTriggers),
...flattenObject(onScheduled),
...flattenObject(onStorage),
}).forEach(([name, value]) => {
const options = (value as CloudFunctions).runtimeOptions;

let command = `gcloud run deploy ${name} \\
--image gcr.io/$GOOGLE_CLOUD_PROJECT/functions \\
--allow-unauthenticated \\
--ingress=internal-and-cloud-load-balancing \\
`;
if (options?.region) {
command += ` --region=${options.region} \\\n`;
}
if (options?.timeoutSeconds) {
command += ` --timeout=${options.timeoutSeconds} \\\n`;
}
if (options?.concurrency) {
command += ` --concurrency=${options.concurrency} \\\n`;
}
if (options?.memory) {
command += ` --memory=${options.memory.replace('B', '')} \\\n`;
}
if (options?.minInstances) {
command += ` --min-instances=${options.minInstances} \\\n`;
}
if (options?.cpu) {
command += ` --cpu=${options.cpu} \\\n`;
}
fs.appendFileSync(file, command + ' &\n\n');
});

fs.appendFileSync(file, 'wait\n\n');

Object.entries(flattenObject(onStorage)).forEach(([name, value]) => {
const options = (value as CloudFunctions).runtimeOptions;
let command = `if [ -z "$(gcloud eventarc triggers list --filter="name:${name}" --format="value(name)")" ]; then
if (options?.region) {
command += ` --region=${options.region} \\\n`;
}
if (options?.timeoutSeconds) {
command += ` --timeout=${options.timeoutSeconds} \\\n`;
}
if (options?.concurrency) {
command += ` --concurrency=${options.concurrency} \\\n`;
}
if (options?.memory) {
command += ` --memory=${options.memory.replace('B', '')} \\\n`;
}
if (options?.minInstances) {
command += ` --min-instances=${options.minInstances} \\\n`;
}
if (options?.cpu) {
command += ` --cpu=${options.cpu} \\\n`;
}
fs.appendFileSync(file, command + ' &\n\n');
});

fs.appendFileSync(file, 'wait\n\n');
};

const deployStorageTriggers = () => {
Object.entries(flattenObject(onStorage)).forEach(([name, value]) => {
const options = (value as CloudFunctions).runtimeOptions;
let command = `if [ -z "$(gcloud eventarc triggers list --filter="name:${name}" --format="value(name)")" ]; then
gcloud eventarc triggers create ${name} \\
--destination-run-service=${name} \\
--destination-run-path="/${name}" \\
Expand All @@ -68,48 +73,52 @@ Object.entries(flattenObject(onStorage)).forEach(([name, value]) => {
--event-filters="type=google.cloud.storage.object.v1.finalized" \\
--event-filters="bucket=${options.bucket}" \\
--service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com\nfi &\n\n`;
fs.appendFileSync(file, command);
});

const getTriggerType = (type: TriggeredFunctionType) => {
switch (type) {
case TriggeredFunctionType.ON_CREATE:
return 'google.cloud.firestore.document.v1.created';
case TriggeredFunctionType.ON_UPDATE:
return 'google.cloud.firestore.document.v1.updated';
case TriggeredFunctionType.ON_WRITE:
return 'google.cloud.firestore.document.v1.written';
}
fs.appendFileSync(file, command);
});
};

Object.entries(flattenObject(onTriggers)).forEach(([name, value]) => {
const options = (value as CloudFunctions).runtimeOptions;
const type = (value as TriggeredFunction).type;
const document = (value as TriggeredFunction).document;
let command = `if [ -z "$(gcloud eventarc triggers list --filter="name:${name}" --format="value(name)")" ]; then
gcloud eventarc triggers create ${name} \\
--location=nam5 \\
--service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com \\
--destination-run-service=${name} \\
--destination-run-region=${options.region} \\
--destination-run-path="/${name}" \\
--event-filters="database=(default)" \\
--event-filters="document=${document}" \\
--event-filters="namespace=(default)" \\
--event-filters="type=${getTriggerType(type)}" \\
--event-data-content-type="application/protobuf"\nfi &\n\n`;
fs.appendFileSync(file, command + '');
});

Object.entries(flattenObject(onScheduled)).forEach(([name, value]) => {
const command = `if ! gcloud pubsub topics list --format="value(name)" | grep -q "${name}"; then
const deployFirestoreTriggers = () => {
const getTriggerType = (type: TriggeredFunctionType) => {
switch (type) {
case TriggeredFunctionType.ON_CREATE:
return 'google.cloud.firestore.document.v1.created';
case TriggeredFunctionType.ON_UPDATE:
return 'google.cloud.firestore.document.v1.updated';
case TriggeredFunctionType.ON_WRITE:
return 'google.cloud.firestore.document.v1.written';
}
};

Object.entries(flattenObject(onTriggers)).forEach(([name, value]) => {
const options = (value as CloudFunctions).runtimeOptions;
const type = (value as TriggeredFunction).type;
const document = (value as TriggeredFunction).document;
let command = `if [ -z "$(gcloud eventarc triggers list --filter="name:${name}" --format="value(name)")" ]; then
gcloud eventarc triggers create ${name} \\
--location=nam5 \\
--service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com \\
--destination-run-service=${name} \\
--destination-run-region=${options.region} \\
--destination-run-path="/${name}" \\
--event-filters="database=(default)" \\
--event-filters-path-pattern="document=${document}" \\
--event-filters="namespace=(default)" \\
--event-filters="type=${getTriggerType(type)}" \\
--event-data-content-type="application/protobuf"\nfi &\n\n`;
fs.appendFileSync(file, command + '');
});
};

const deployCronTriggers = () => {
Object.entries(flattenObject(onScheduled)).forEach(([name, value]) => {
const command = `if ! gcloud pubsub topics list --format="value(name)" | grep -q "${name}"; then
gcloud pubsub topics create "${name}"\nfi\n\n`;
fs.appendFileSync(file, command);
});
fs.appendFileSync(file, command);
});

Object.entries(flattenObject(onScheduled)).forEach(([name, value]) => {
const options = (value as CloudFunctions).runtimeOptions;
const command = `if [ -z "$(gcloud eventarc triggers list --filter="name:${name}" --format="value(name)")" ]; then
Object.entries(flattenObject(onScheduled)).forEach(([name, value]) => {
const options = (value as CloudFunctions).runtimeOptions;
const command = `if [ -z "$(gcloud eventarc triggers list --filter="name:${name}" --format="value(name)")" ]; then
gcloud eventarc triggers create ${name} \\
--location=us-central1 \\
--service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com \\
Expand All @@ -118,19 +127,26 @@ Object.entries(flattenObject(onScheduled)).forEach(([name, value]) => {
--destination-run-region=${options.region} \\
--destination-run-path="/${name}" \\
--event-filters="type=google.cloud.pubsub.topic.v1.messagePublished"\nfi &\n\n`;
fs.appendFileSync(file, command);
});
fs.appendFileSync(file, command);
});

fs.appendFileSync(file, 'wait\n\n');
fs.appendFileSync(file, 'wait\n\n');

Object.entries(flattenObject(onScheduled)).forEach(([name, value]) => {
const schedule = (value as ScheduledFunction).schedule;
const command = `if [ -z "$(gcloud scheduler jobs list --filter="name:${name}" --format="value(name)")" ]; then
Object.entries(flattenObject(onScheduled)).forEach(([name, value]) => {
const schedule = (value as ScheduledFunction).schedule;
const command = `if [ -z "$(gcloud scheduler jobs list --filter="name:${name}" --format="value(name)")" ]; then
gcloud scheduler jobs create pubsub ${name} \\
--schedule="${schedule}" \\
--topic=projects/$GOOGLE_CLOUD_PROJECT/topics/${name} \\
--message-body="{}"\nfi &\n\n`;
fs.appendFileSync(file, command);
});
fs.appendFileSync(file, command);
});

fs.appendFileSync(file, 'wait\n\n');
};

fs.appendFileSync(file, 'wait\n\n');
buildImage();
deployServices();
deployStorageTriggers();
deployFirestoreTriggers();
deployCronTriggers();
8 changes: 4 additions & 4 deletions packages/functions/src/index.express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Object.entries(flattenObject(onRequests)).forEach(([name, config]) => {

// TRIGGERS
Object.entries(flattenObject(onTriggers)).forEach(([name, config]) => {
app.post(`/${name}`, app.use(express.raw({ type: 'application/protobuf' })), async (req, res) => {
app.post(`/${name}`, express.raw({ type: 'application/protobuf' }), async (req, res) => {
const root = loadSync('./data.proto');
const type = root.lookupType('DocumentEventData');
const decoded = type.decode(req.body);
Expand All @@ -50,15 +50,15 @@ Object.entries(flattenObject(onTriggers)).forEach(([name, config]) => {
...pathToParts(get(cloudEvent, 'document', '')),
};
await (config as TriggeredFunction).handler(event);
res.send(200);
res.sendStatus(200);
});
});

// CRON
Object.entries(flattenObject(onScheduled)).forEach(([name, config]) => {
app.post(`/${name}`, async (_, res) => {
await (config as ScheduledFunction).func();
res.send(200);
res.sendStatus(200);
});
});

Expand All @@ -75,7 +75,7 @@ Object.entries(flattenObject(onStorage)).forEach(([name, config]) => {
bucket: get(event, 'data.bucket', ''),
contentType: get(event, 'data.contentType'),
});
res.send(200);
res.sendStatus(200);
});
});

Expand Down
6 changes: 4 additions & 2 deletions packages/functions/src/runtime/proto/protoToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const protoJsonToJson = (protoJson: { [key: string]: any }) =>
const valueToJson = (data: { [key: string]: any }): any => {
const [type, value] = Object.entries(data)[0];
switch (type) {
case 'nullValue':
return null;
case 'booleanValue':
case 'integerValue':
case 'doubleValue':
Expand All @@ -34,8 +36,8 @@ const valueToJson = (data: { [key: string]: any }): any => {
case 'referenceValue':
return value;
case 'arrayValue':
return (value.values as any[]).map(valueToJson);
return ((value.values as any[]) || []).map(valueToJson);
case 'mapValue':
return protoJsonToJson(value.fields);
return protoJsonToJson(value.fields || {});
}
};
2 changes: 1 addition & 1 deletion packages/interfaces/src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export enum PublicCollections {
PROPOSAL = 'proposal',
NOTIFICATION = 'notification',
MILESTONE = 'milestone',
MILESTONE_RMS = 'milestone_rms',
MILESTONE_RMS = 'milestone_rms_t2',
MILESTONE_SMR = 'milestone_smr',
TRANSACTION = 'transaction',
BADGES = 'badges',
Expand Down
2 changes: 1 addition & 1 deletion packages/interfaces/src/models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export enum COL {
PROPOSAL = 'proposal',
NOTIFICATION = 'notification',
MILESTONE = 'milestone',
MILESTONE_RMS = 'milestone_rms',
MILESTONE_RMS = 'milestone_rms_t2',
MILESTONE_SMR = 'milestone_smr',
TRANSACTION = 'transaction',
BADGES = 'badges',
Expand Down

0 comments on commit ea5a7d1

Please sign in to comment.