Skip to content

Commit ced9495

Browse files
authored
Revert "Revert "[go] Implement startDevServer() function (vercel#4662)" (vercel#4899)" (vercel#4906)
This reverts commit 8d1afc0.
1 parent fadc3f2 commit ced9495

File tree

12 files changed

+316
-58
lines changed

12 files changed

+316
-58
lines changed

.editorconfig

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ indent_style = space
1919
[*.py]
2020
indent_size = 4
2121

22+
[*.go]
23+
indent_style = tab
24+
indent_size = 4
25+
tab_width = 4
26+
2227
[*.asm]
2328
indent_size = 8
2429

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vercel
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func Handler(w http.ResponseWriter, r *http.Request) {
9+
fmt.Fprintf(w, "Req Path: %s", r.URL.Path)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package another
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func Another(w http.ResponseWriter, r *http.Request) {
9+
fmt.Fprintf(w, "This is another page")
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func Handler(w http.ResponseWriter, r *http.Request) {
9+
fmt.Fprintf(w, "This is the index page")
10+
}

packages/now-cli/test/dev/integration.js

+46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ms from 'ms';
22
import os from 'os';
33
import fs from 'fs-extra';
44
import test from 'ava';
5+
import { isIP } from 'net';
56
import { join, resolve, delimiter } from 'path';
67
import _execa from 'execa';
78
import fetch from 'node-fetch';
@@ -1600,3 +1601,48 @@ test(
16001601
await testPath(200, '/index.css', 'This is index.css');
16011602
})
16021603
);
1604+
1605+
test(
1606+
'[vercel dev] Should support `*.go` API serverless functions',
1607+
testFixtureStdio('go', async testPath => {
1608+
await testPath(200, `/api`, 'This is the index page');
1609+
await testPath(200, `/api/index`, 'This is the index page');
1610+
await testPath(200, `/api/index.go`, 'This is the index page');
1611+
await testPath(200, `/api/another`, 'This is another page');
1612+
await testPath(200, '/api/another.go', 'This is another page');
1613+
await testPath(200, `/api/foo`, 'Req Path: /api/foo');
1614+
await testPath(200, `/api/bar`, 'Req Path: /api/bar');
1615+
})
1616+
);
1617+
1618+
test(
1619+
'[vercel dev] Should set the `ts-node` "target" to match Node.js version',
1620+
testFixtureStdio('node-ts-node-target', async testPath => {
1621+
await testPath(200, `/api/subclass`, '{"ok":true}');
1622+
await testPath(
1623+
200,
1624+
`/api/array`,
1625+
'{"months":[1,2,3,4,5,6,7,8,9,10,11,12]}'
1626+
);
1627+
1628+
await testPath(200, `/api/dump`, (t, body, res, isDev) => {
1629+
const { host } = new URL(res.url);
1630+
const { env, headers } = JSON.parse(body);
1631+
1632+
// Test that the API endpoint receives the Vercel proxy request headers
1633+
t.is(headers['x-forwarded-host'], host);
1634+
t.is(headers['x-vercel-deployment-url'], host);
1635+
t.truthy(isIP(headers['x-real-ip']));
1636+
t.truthy(isIP(headers['x-forwarded-for']));
1637+
t.truthy(isIP(headers['x-vercel-forwarded-for']));
1638+
1639+
// Test that the API endpoint has the Vercel platform env vars defined.
1640+
t.regex(env.NOW_REGION, /^[a-z]{3}\d$/);
1641+
if (isDev) {
1642+
// Only dev is tested because in production these are opt-in.
1643+
t.is(env.VERCEL_URL, host);
1644+
t.is(env.VERCEL_REGION, 'dev1');
1645+
}
1646+
});
1647+
})
1648+
);

packages/now-go/build.sh

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Start fresh
5+
rm -rf dist
6+
7+
# Build with `ncc`
18
ncc build index.ts -e @vercel/build-utils -e @now/build-utils -o dist
29
ncc build install.ts -e @vercel/build-utils -e @now/build-utils -o dist/install
10+
11+
# Move `install.js` to dist
312
mv dist/install/index.js dist/install.js
413
rm -rf dist/install

packages/now-go/go-helpers.ts

+27-46
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ import execa from 'execa';
33
import fetch from 'node-fetch';
44
import { mkdirp, pathExists } from 'fs-extra';
55
import { dirname, join } from 'path';
6-
import { homedir } from 'os';
76
import buildUtils from './build-utils';
87
import stringArgv from 'string-argv';
98
const { debug } = buildUtils;
10-
const archMap = new Map([
11-
['x64', 'amd64'],
12-
['x86', '386'],
13-
]);
9+
const archMap = new Map([['x64', 'amd64'], ['x86', '386']]);
1410
const platformMap = new Map([['win32', 'windows']]);
1511

1612
// Location where the `go` binary will be installed after `postinstall`
@@ -130,50 +126,35 @@ export async function downloadGo(
130126
platform = process.platform,
131127
arch = process.arch
132128
) {
133-
// Check default `Go` in user machine
134-
const isUserGo = await pathExists(join(homedir(), 'go'));
129+
// Check if `go` is already installed in user's `$PATH`
130+
const { failed, stdout } = await execa('go', ['version'], { reject: false });
135131

136-
// If we found GOPATH in ENV, or default `Go` path exists
137-
// asssume that user have `Go` installed
138-
if (isUserGo || process.env.GOPATH !== undefined) {
139-
const { stdout } = await execa('go', ['version']);
132+
if (!failed && parseInt(stdout.split('.')[1]) >= 11) {
133+
debug('Using system installed version of `go`: %o', stdout.trim());
134+
return createGo(dir, platform, arch);
135+
}
140136

141-
if (parseInt(stdout.split('.')[1]) >= 11) {
142-
return createGo(dir, platform, arch);
143-
}
137+
// Check `go` bin in builder CWD
138+
const isGoExist = await pathExists(join(dir, 'bin'));
139+
if (!isGoExist) {
140+
debug('Installing `go` v%s to %o for %s %s', version, dir, platform, arch);
141+
const url = getGoUrl(version, platform, arch);
142+
debug('Downloading `go` URL: %o', url);
143+
const res = await fetch(url);
144144

145-
throw new Error(
146-
`Your current ${stdout} doesn't support Go Modules. Please update.`
147-
);
148-
} else {
149-
// Check `Go` bin in builder CWD
150-
const isGoExist = await pathExists(join(dir, 'bin'));
151-
if (!isGoExist) {
152-
debug(
153-
'Installing `go` v%s to %o for %s %s',
154-
version,
155-
dir,
156-
platform,
157-
arch
158-
);
159-
const url = getGoUrl(version, platform, arch);
160-
debug('Downloading `go` URL: %o', url);
161-
const res = await fetch(url);
162-
163-
if (!res.ok) {
164-
throw new Error(`Failed to download: ${url} (${res.status})`);
165-
}
166-
167-
// TODO: use a zip extractor when `ext === "zip"`
168-
await mkdirp(dir);
169-
await new Promise((resolve, reject) => {
170-
res.body
171-
.on('error', reject)
172-
.pipe(tar.extract({ cwd: dir, strip: 1 }))
173-
.on('error', reject)
174-
.on('finish', resolve);
175-
});
145+
if (!res.ok) {
146+
throw new Error(`Failed to download: ${url} (${res.status})`);
176147
}
177-
return createGo(dir, platform, arch);
148+
149+
// TODO: use a zip extractor when `ext === "zip"`
150+
await mkdirp(dir);
151+
await new Promise((resolve, reject) => {
152+
res.body
153+
.on('error', reject)
154+
.pipe(tar.extract({ cwd: dir, strip: 1 }))
155+
.on('error', reject)
156+
.on('finish', resolve);
157+
});
178158
}
159+
return createGo(dir, platform, arch);
179160
}

0 commit comments

Comments
 (0)