Skip to content

Commit 4fb48ce

Browse files
committed
Get it working with either branches or commit shas
1 parent f9826b7 commit 4fb48ce

File tree

4 files changed

+50
-37
lines changed

4 files changed

+50
-37
lines changed

src/commands/serve.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import {getGitData} from '../gitdata';
21
import {createApp} from '../server';
32

43
interface ServeOptions {
54
site: string;
6-
ref: string;
7-
branch: string;
5+
ref?: string;
86
}
97

108
export class ServeCommand {
119
constructor(private readonly options: ServeOptions) {
1210
this.options = options;
1311
}
1412

15-
run() {
13+
async run() {
1614
const app = createApp(
1715
this.options.site || 'default',
18-
this.options.ref || '',
19-
this.options.branch || ''
16+
this.options.ref || ''
2017
);
2118
const PORT = process.env.PORT || 8080;
2219
app.listen(PORT, () => {

src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ program
2020
});
2121

2222
program
23-
.command('serve [dir]')
23+
.command('serve')
2424
.description('Runs the server')
25-
.option('-r, --ref <ref>', 'ref', '')
2625
.option('-s, --site <site>', 'site', '')
27-
.option('-b, --branch <branch>', 'branch', '')
28-
.action((path, options) => {
26+
.option('-r, --ref <ref>', 'ref', '')
27+
.action(options => {
2928
const cmd = new ServeCommand(options);
3029
cmd.run();
3130
});

src/server.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {GoogleAuth} from 'google-auth-library';
33
import * as fsPath from 'path';
44
import express = require('express');
55
import httpProxy = require('http-proxy');
6+
import { Manifest } from './manifest';
67

78
const URL = 'https://storage.googleapis.com';
89
const datastore = new Datastore();
@@ -15,23 +16,27 @@ interface ManifestCache {
1516
[requestSha: string]: string;
1617
}
1718

18-
const downloadManifest = async (shortsha: string) => {
19-
const key = datastore.key(['Fileset2Manifest', shortsha]);
20-
const entity = await datastore.get(key);
21-
if (!entity || !entity[0]) {
19+
const downloadManifest = async (branchOrRef: string) => {
20+
const keys = [
21+
datastore.key(['Fileset2Manifest', branchOrRef]),
22+
datastore.key(['Fileset2Manifest', `branch:${branchOrRef}`]),
23+
];
24+
const resp = await datastore.get(keys);
25+
if (!resp || !resp[0]) {
2226
return;
2327
}
24-
const paths = entity[0].paths;
25-
return paths;
28+
const entities = resp[0];
29+
return entities[0] || entities[1];
2630
};
2731

2832
const parseHostname = (hostname: string) => {
2933
// TODO: Make this more robust
3034
if (hostname.includes('-dot-')) {
31-
const parts = hostname.split('-dot-');
35+
const prefix = hostname.split('-dot-fileset2-')[0];
36+
const parts = prefix.split('-'); // Either site-ref or site.
3237
return {
3338
siteId: parts[0],
34-
branchOrRef: parts[1].slice(0, 7),
39+
branchOrRef: parts.length > 1 ? parts[1].slice(0, 7) : 'master',
3540
};
3641
} else {
3742
return {
@@ -41,30 +46,30 @@ const parseHostname = (hostname: string) => {
4146
}
4247
};
4348

44-
export function createApp(siteId: string, shortsha: string, branch: string) {
45-
// const startupManifest = await downloadManifest(shortsha);
46-
console.log(`Starting server for site: ${siteId} @ ${branch}`);
49+
export function createApp(siteId: string, branchOrRef: string) {
50+
// const startupManifest = await downloadManifest(branchOrRef);
51+
console.log(`Starting server for site: ${siteId} @ ${branchOrRef}`);
4752

4853
const app = express();
4954
app.disable('x-powered-by');
5055
app.all('/*', async (req: express.Request, res: express.Response) => {
5156
const envFromHostname = parseHostname(req.hostname);
5257
const requestSiteId = envFromHostname.siteId || siteId;
53-
const requestSha = envFromHostname.branchOrRef || shortsha;
54-
const requestBranch = envFromHostname.branchOrRef || branch;
58+
const requestBranchOrRef = envFromHostname.branchOrRef || branchOrRef;
5559

5660
let blobPath = decodeURIComponent(req.path);
5761
if (blobPath.endsWith('/')) {
5862
blobPath += 'index.html';
5963
}
6064

61-
const manifest = await downloadManifest(requestSha);
65+
const manifest = await downloadManifest(requestBranchOrRef);
66+
const manifestPaths = manifest.paths;
6267

63-
if (!manifest) {
68+
if (!manifestPaths) {
6469
res.sendStatus(404);
6570
return;
6671
}
67-
const blobKey = manifest[blobPath];
72+
const blobKey = manifestPaths[blobPath];
6873
const updatedUrl = `/wing-prod.appspot.com/fileset/sites/${requestSiteId}/blobs/${blobKey}`;
6974

7075
// TODO: Add custom 404 support based on site config.
@@ -100,7 +105,7 @@ export function createApp(siteId: string, shortsha: string, branch: string) {
100105
// This also can't be a very small value, as it kills perf. 0036 seems to work correctly.
101106
proxyRes.headers['cache-control'] = 'public, max-age=0036'; // The padded 0036 keeps the content length the same per upload.ts.
102107
proxyRes.headers['x-fileset-blob'] = blobKey;
103-
proxyRes.headers['x-fileset-ref'] = requestSha;
108+
proxyRes.headers['x-fileset-ref'] = manifest.ref;
104109
proxyRes.headers['x-fileset-site'] = siteId;
105110
res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);
106111
});

src/upload.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import _colors = require('colors');
55

66
import {Storage} from '@google-cloud/storage';
77
import {Datastore} from '@google-cloud/datastore';
8+
import {entity} from '@google-cloud/datastore/build/src/entity';
89

910
const datastore = new Datastore();
1011

@@ -128,26 +129,37 @@ export async function uploadManifest(
128129
}
129130
}
130131

131-
async function finalize(manifest: Manifest) {
132-
const key = datastore.key(['Fileset2Manifest', manifest.shortSha]);
133-
const pathsToHashes = manifest.toJSON();
134-
const entity = {
132+
function createEntity(key: entity.Key, manifest: Manifest) {
133+
return {
135134
key: key,
136135
excludeFromIndexes: ['paths'],
137136
data: {
138137
created: new Date(),
139138
ref: manifest.ref,
140-
paths: pathsToHashes,
139+
branch: manifest.branch,
140+
paths: manifest.toJSON(),
141141
},
142142
};
143-
await datastore.save(entity);
143+
}
144+
145+
async function finalize(manifest: Manifest) {
146+
const key = datastore.key(['Fileset2Manifest', manifest.shortSha]);
147+
const ent = createEntity(key, manifest);
148+
await datastore.save(ent);
149+
150+
if (manifest.branch) {
151+
const branchKey = datastore.key([
152+
'Fileset2Manifest',
153+
`branch:${manifest.branch}`,
154+
]);
155+
const branchEnt = createEntity(branchKey, manifest);
156+
await datastore.save(branchEnt);
157+
}
158+
144159
console.log(
145160
`Finalized upload for site: ${manifest.site} -> ${manifest.branch} @ ${manifest.shortSha}`
146161
);
147162
console.log(
148-
`Staged: https://${manifest.site}-dot-${manifest.shortSha}-dot-fileset2-dot-${process.env.GCLOUD_PROJECT}.appspot.com`
163+
`Staged: https://${manifest.site}-${manifest.shortSha}-dot-fileset2-dot-${process.env.GCLOUD_PROJECT}.appspot.com`
149164
);
150-
151-
if (manifest.branch) {
152-
}
153165
}

0 commit comments

Comments
 (0)