Skip to content

Commit cefe23d

Browse files
committed
Add custom live domain support
1 parent 33bc96b commit cefe23d

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

app.yaml

Lines changed: 0 additions & 4 deletions
This file was deleted.

example/fileset.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
google_cloud_project: <AppId>
22
site: <SiteId>
33
schedule:
4-
default: master
4+
default: master

example/server/app.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
service: fileset
2-
runtime: nodejs10
2+
runtime: nodejs10
3+
env_variables:
4+
FILESET_SITE: default
5+
FILESET_LIVE_DOMAIN: example.com
6+
FILESET_STAGING_DOMAIN: {ref}.staging.example.com

src/server.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,28 @@ import {ExecutionContext} from 'ava';
44
import test from 'ava';
55

66
test('Test parseHostname', (t: ExecutionContext) => {
7+
// App Engine wildcard domain.
78
t.deepEqual(
89
server.parseHostname('sitename-refname-dot-fileset2-dot-appid.appspot.com'),
910
{
1011
siteId: 'sitename',
1112
branchOrRef: 'refname',
1213
}
1314
);
15+
// Custom live domain.
16+
t.deepEqual(
17+
server.parseHostname('example.com', 'example', 'example.com'),
18+
{
19+
siteId: 'example',
20+
branchOrRef: 'master',
21+
}
22+
);
23+
// Some other domain.
24+
t.deepEqual(
25+
server.parseHostname('something.com', 'example', 'example.com'),
26+
{
27+
siteId: 'example',
28+
branchOrRef: '',
29+
}
30+
);
1431
});

src/server.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,24 @@ const getManifest = async (siteId: string, branchOrRef: string) => {
5151
// }
5252
};
5353

54-
export function parseHostname(hostname: string) {
55-
// TODO: Make this more robust
56-
if (hostname.includes('-dot-')) {
57-
const prefix = hostname.split('-dot-fileset2-')[0];
58-
const parts = prefix.split('-'); // Either site-ref or site.
59-
return {
60-
siteId: parts[0],
61-
branchOrRef: parts.length > 1 ? parts[1].slice(0, 7) : 'master',
62-
};
63-
} else {
64-
return {
65-
siteId: '',
66-
branchOrRef: '',
67-
};
54+
export function parseHostname(hostname: string, defaultSiteId?: string, defaultLiveDomain?: string) {
55+
let siteId = defaultSiteId || 'default';
56+
let branchOrRef = '';
57+
if (hostname == defaultLiveDomain) {
58+
// Hostname is the "live" or "prod" domain. Use the master branch.
59+
branchOrRef = 'master';
60+
} else if (hostname.includes('-dot-')) {
61+
// Use "-dot-" as a sentinel for App Engine wildcard domains.
62+
const prefix = hostname.split('-dot-')[0];
63+
const parts = prefix.split('-'); // Either <Site>-<Ref> or <Site>.
64+
siteId = parts[0];
65+
branchOrRef = parts.length > 1 ? parts[1].slice(0, 7) : 'master';
6866
}
67+
// TODO: Implement defaultStagingDomain (custom staging domain) support.
68+
return {
69+
siteId: siteId,
70+
branchOrRef: branchOrRef,
71+
};
6972
}
7073

7174
export function createApp(siteId: string, branchOrRef: string) {
@@ -74,7 +77,7 @@ export function createApp(siteId: string, branchOrRef: string) {
7477
const app = express();
7578
app.disable('x-powered-by');
7679
app.all('/*', async (req: express.Request, res: express.Response) => {
77-
const envFromHostname = parseHostname(req.hostname);
80+
const envFromHostname = parseHostname(req.hostname, process.env.FILESET_SITE, process.env.FILESET_LIVE_DOMAIN);
7881
const requestSiteId = envFromHostname.siteId || siteId;
7982
const requestBranchOrRef = envFromHostname.branchOrRef || branchOrRef;
8083

0 commit comments

Comments
 (0)