Skip to content

Commit 4dc6e35

Browse files
authored
feat: Bundle UI into coderd; add ./develop.sh script (#28)
This change bundles the static assets like we have for v1 - using the [`embed`](https://pkg.go.dev/embed) go package. Fixes #22 In addition, it sets up a development script that runs `coderd` locally and serves the front-end, with hot-reloading. The script used is `./develop.sh`: ![2022-01-14 17 30 14](https://user-images.githubusercontent.com/88213859/149603926-f673d3d3-ba12-4eda-bcdd-427252405480.gif) > NOTE: The UI is still placeholder, of course. Need to start testing out a simple, placeholder flow for the new v2 world as a next step Summary of changes: - Add build steps for `go` in the `Makefile` - Add a step for production build, in which we use the `embed` tag - Add a step for development, which doesn't need the `embed` tag - so we don't need to build the front-end twice - Add `next export` build step to output front-end artifacts in `out` - Add a `site` package for `go` - Add `embed_static.go` and `embed.go`. This is mostly brought in as-is from v1, except removing some intercom/sentry CSP entries that we aren't using. - Add a [next development server](https://nextjs.org/docs/advanced-features/custom-server) - Add a `v2-dev` script, that runs `coderd` and the `next` dev server side-by-side - Use the `site` package as the fallback handler. - Add `.gitignore` entries for additional build collateral
1 parent 5c49f1f commit 4dc6e35

File tree

15 files changed

+909
-13
lines changed

15 files changed

+909
-13
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ yarn-error.log
1717
# Front-end ignore
1818
.next/
1919
site/.next/
20-
coverage/
20+
coverage/
21+
22+
# Build
23+
bin/
24+
site/out/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ yarn-error.log
1313
# Front-end ignore
1414
.next/
1515
site/.next/
16+
site/out/
1617
coverage/

Makefile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
bin/coderd:
2+
mkdir -p bin
3+
go build -o bin/coderd cmd/coderd/main.go
4+
.PHONY: bin/coderd
5+
6+
build: site/out bin/coderd
7+
.PHONY: build
8+
19
# Runs migrations to output a dump of the database.
210
database/dump.sql: $(wildcard database/migrations/*.sql)
311
go run database/dump/main.go
@@ -43,4 +51,9 @@ provisionersdk/proto: provisionersdk/proto/provisioner.proto
4351
--go-drpc_out=. \
4452
--go-drpc_opt=paths=source_relative \
4553
./provisioner.proto
46-
.PHONY: provisionersdk/proto
54+
.PHONY: provisionersdk/proto
55+
56+
site/out:
57+
yarn build
58+
yarn export
59+
.PHONY: site/out

coderd/coderd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"cdr.dev/slog"
77
"github.com/coder/coder/database"
8+
"github.com/coder/coder/site"
89
"github.com/go-chi/chi"
910
"github.com/go-chi/render"
1011
)
@@ -27,5 +28,6 @@ func New(options *Options) http.Handler {
2728
})
2829
})
2930
})
31+
r.NotFound(site.Handler().ServeHTTP)
3032
return r
3133
}

develop.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
PROJECT_ROOT="$(git rev-parse --show-toplevel)"
6+
cd "${PROJECT_ROOT}"
7+
8+
# Do initial build - a dev build for coderd.
9+
# It's OK that we don't build the front-end before - because the front-end
10+
# assets are handled by the `yarn dev` devserver.
11+
make bin/coderd
12+
13+
# This is a way to run multiple processes in parallel, and have Ctrl-C work correctly
14+
# to kill both at the same time. For more details, see:
15+
# https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
16+
(trap 'kill 0' SIGINT; CODERV2_HOST=http://127.0.0.1:3000 yarn dev & ./bin/coderd)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ require (
6565
github.com/hashicorp/terraform-json v0.13.0 // indirect
6666
github.com/imdario/mergo v0.3.12 // indirect
6767
github.com/inconshreveable/mousetrap v1.0.0 // indirect
68+
github.com/justinas/nosurf v1.1.1 // indirect
6869
github.com/mattn/go-colorable v0.1.12 // indirect
6970
github.com/mattn/go-isatty v0.0.14 // indirect
7071
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
@@ -90,6 +91,7 @@ require (
9091
github.com/pmezard/go-difflib v1.0.0 // indirect
9192
github.com/sirupsen/logrus v1.8.1 // indirect
9293
github.com/spf13/pflag v1.0.5 // indirect
94+
github.com/unrolled/secure v1.0.9 // indirect
9395
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
9496
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
9597
github.com/xeipuuv/gojsonschema v1.2.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,8 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
773773
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
774774
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
775775
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
776+
github.com/justinas/nosurf v1.1.1 h1:92Aw44hjSK4MxJeMSyDa7jwuI9GR2J/JCQiaKvXXSlk=
777+
github.com/justinas/nosurf v1.1.1/go.mod h1:ALpWdSbuNGy2lZWtyXdjkYv4edL23oSEgfBT1gPJ5BQ=
776778
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
777779
github.com/k0kubun/pp v2.3.0+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
778780
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
@@ -1151,10 +1153,13 @@ github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqri
11511153
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
11521154
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
11531155
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
1156+
github.com/unrolled/secure v1.0.9 h1:BWRuEb1vDrBFFDdbCnKkof3gZ35I/bnHGyt0LB0TNyQ=
1157+
github.com/unrolled/secure v1.0.9/go.mod h1:fO+mEan+FLB0CdEnHf6Q4ZZVNqG+5fuLFnP8p0BXDPI=
11541158
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
11551159
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
11561160
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
11571161
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
1162+
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
11581163
github.com/vishvananda/netlink v0.0.0-20181108222139-023a6dafdcdf/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
11591164
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
11601165
github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho=

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
"<rootDir>/site/**/*.tsx",
2121
"!<rootDir>/site/**/*.stories.tsx",
2222
"!<rootDir>/site/.next/**/*.*",
23+
"!<rootDir>/site/dev.ts",
2324
"!<rootDir>/site/next-env.d.ts",
2425
"!<rootDir>/site/next.config.js",
2526
],

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"build": "NODE_ENV=production next build site",
88
"build:dev": "next build site",
9-
"dev": "next dev site",
9+
"dev": "ts-node site/dev.ts",
10+
"export": "next export site",
1011
"format:check": "prettier --check '**/*.{css,html,js,json,jsx,md,ts,tsx,yaml,yml}'",
1112
"format:write": "prettier --write '**/*.{css,html,js,json,jsx,md,ts,tsx,yaml,yml}'",
1213
"test": "jest --selectProjects test",
@@ -17,18 +18,23 @@
1718
"@material-ui/icons": "4.5.1",
1819
"@material-ui/lab": "4.0.0-alpha.42",
1920
"@testing-library/react": "12.1.2",
21+
"@types/express": "4.17.13",
2022
"@types/jest": "27.4.0",
2123
"@types/node": "14.18.4",
2224
"@types/react": "17.0.38",
2325
"@types/react-dom": "17.0.11",
2426
"@types/superagent": "4.1.14",
27+
"express": "4.17.2",
28+
"http-proxy-middleware": "2.0.1",
2529
"jest": "27.4.7",
2630
"next": "12.0.7",
2731
"prettier": "2.5.1",
2832
"react": "17.0.2",
2933
"react-dom": "17.0.2",
3034
"ts-jest": "27.1.2",
3135
"ts-loader": "9.2.6",
36+
"ts-node": "10.4.0",
3237
"typescript": "4.5.4"
33-
}
38+
},
39+
"dependencies": {}
3440
}

site/dev.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import express from "express"
2+
import { createProxyMiddleware } from "http-proxy-middleware"
3+
import next from "next"
4+
5+
const port = process.env.PORT || 8080
6+
const dev = process.env.NODE_ENV !== "production"
7+
8+
let coderV2Host = "http://127.0.0.1:3000"
9+
10+
if (process.env.CODERV2_HOST) {
11+
if (!/^http(s)?:\/\//.test(process.env.CODERV2_HOST)) {
12+
throw new Error("CODERV2_HOST must be http(s)")
13+
} else {
14+
coderV2Host = process.env.CODERV2_HOST
15+
}
16+
}
17+
18+
console.log(`Using CODERV2_HOST: ${coderV2Host}`)
19+
20+
const app = next({ dev, dir: "./site" })
21+
const handle = app.getRequestHandler()
22+
23+
app
24+
.prepare()
25+
.then(() => {
26+
const server = express()
27+
server.use(
28+
"/api",
29+
createProxyMiddleware("/api", {
30+
target: coderV2Host,
31+
ws: true,
32+
secure: false,
33+
changeOrigin: true,
34+
}),
35+
)
36+
server.all("*", (req, res) => handle(req, res))
37+
server.listen(port)
38+
})
39+
.catch((err) => {
40+
console.error(err)
41+
process.exit(1)
42+
})

0 commit comments

Comments
 (0)