Some tips about this project
Host a simple Go web application in Netlify and Vercel
Live Preview
Write a function
The Vercel docs and Netlify docs for golang are a great starting point.
The functions directory
deployed to Vercel
is /api.
The functions directory
deployed to Netlify
is /cmd.
The shared static pages directory
is /public.
Build the binary
Push code to GitHub and instruct the Vercel
and Netlify
CI on how to use the source code to build.
Deploy to Vercel
- Configure the settings in a vercel.json file in the root of project.
For example:
{
"version": 2,
"build": {
"env": {
"GO_BUILD_FLAGS": "-ldflags '-s -w'"
}
},
"builds": [
{
"src": "public/**",
"use": "@vercel/static"
},
{
"src": "api/**/*.go",
"use": "@vercel/go"
}
],
"routes": [
{
"src": "/(.*\\..*)",
"dest": "/public/$1"
},
{
"src": "/([^.]*)",
"dest": "/api/$1.go"
}
]
}
- Configure the settings in a .vercelignore file in the root of project.
For example:
# Golang components used in Netlify build
build.sh
.go-version
go.mod
go.sum
- You can specify the Ignored Build Step that need to be ignored in the project
Settings -> Git
of Vercel
.
For example, specifying only the submission of /api
and /public
folder changes for deployment.
git diff --quiet HEAD^ HEAD ./api ./public
Deploy to Netlify
- Configure the settings in a netlify.toml file in the root of project.
For example:
[build]
command = "chmod a+x build.sh && ./build.sh"
functions = "functions"
publish = "public"
ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ./cmd ./public"
[build.environment]
GO_IMPORT_PATH = "github.com/DavidDengHui/depoly-go"
GO111MODULE = "on"
[[redirects]]
from = "/api/*"
to = "/.netlify/functions/gateway/:splat"
status = 200
- You can customize the
Netlify
build step in the build.sh.
For example:
set -euxo pipefail
mkdir -p "$(pwd)/functions"
rm -rf "$(pwd)/api"
GOBIN=$(pwd)/functions go install ./...
chmod +x "$(pwd)"/functions/*
go env