Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: get site from go:embed #1710

Merged
merged 1 commit into from
Aug 17, 2022
Merged

Conversation

vctt94
Copy link
Member

@vctt94 vctt94 commented Jul 13, 2022

This PR closes #1664

For testing can run go install in dexc directory and make sure that the folder site isn't in the go executable's directory or the working directory.

@chappjc chappjc added this to the 0.6/1.0 milestone Jul 14, 2022
@chappjc chappjc marked this pull request as draft July 31, 2022 18:55
@buck54321
Copy link
Member

This looks pretty slick. How do we get the tests to pass?

@vctt94 vctt94 force-pushed the get-site-from-goembed branch 2 times, most recently from edd8952 to f3fa5fe Compare August 5, 2022 02:10
@vctt94
Copy link
Member Author

vctt94 commented Aug 5, 2022

tests passing now.

With this PR we will need to run: npm run build before pushing to repo to make sure we have the last site build.

@chappjc
Copy link
Member

chappjc commented Aug 10, 2022

Just to reiterate the discussion in matrix, we need a way to allow CI to function without checking in the build output (dist). Perhaps just some placeholder file in the dist folder like touch dist/goembed so go build doesn't error. Possibly it could be solved by tweaking the embed line. It's 1.3 MB whenever js/css modified, and that the build outputs really don't belong in source control, just the source.

The lack of the expected files in either (a) embed.FS OR (b) the actual file system, should result in a runtime error or just a 404 on the webserver as it would now, not a compile-time error.

@chappjc
Copy link
Member

chappjc commented Aug 10, 2022

The other obvious solution is to merge the CI tasks so that the JS build and Go build are in the same runner. That would be a bummer though because the build matrix is unnecessarily large in that case.

Copy link
Member

@buck54321 buck54321 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few changes, please.

  1. Don't check in the dist files.
  2. Let's put some empty dummy files (entry.js and style.css) in client/webserver/ for the /dist files. That'll get us past the tests.
//go:embed entry.js
jsFile embed.FS

//go:embed style.css
cssFile embed.FS
  1. We can recognize the empty files in New, and use the old search.
  2. In the future, the packaging script can hot swap the compiled filed during the build. No need to do that in the PR.

Or maybe @chappjc's is right about just using an empty directory

@chappjc
Copy link
Member

chappjc commented Aug 10, 2022

Or maybe @chappjc's is right about just using an empty directory

What I meant was this:

$  pwd
<repo>/client/webserver
$  mkdir -p site/dist
$  touch site/dist/goembed

The Go binary will build now. It would just 404 in the file server handler, but it can fall back to disk instead. For releases, we can check in the final dist folder contents to people can do stuff like
go install decred.org/dcrdex@release-v0.5.0 and it will use the dist folder that is on that branch.

@chappjc
Copy link
Member

chappjc commented Aug 12, 2022

This is very close. I'm going to suggest a diff in a few.

@chappjc
Copy link
Member

chappjc commented Aug 12, 2022

Here's what I think we should change: d86dd40
That diff applies on top of this PR currently. The combined diff with master: https://github.com/decred/dcrdex/compare/master...chappjc:dcrdex:goembed-site?expand=1

When we start distributing Go binaries with these files embedded, when users upgrade they are likely to overwrite the old dexc(.exe) and leave the old site files. As such, by default dexc needs to only use the embedded files, not first try disk nor fallback to disk.

The --reload-html arg is the same, but it is now the negation of "use embedded", meaning it will not use the embedded files and it will instead use serve disk and always reload the html templates for each request.

After building, we can verify the embedded files with:

$  go list -f '{{ .EmbedFiles }}' decred.org/dcrdex/client/webserver
[site/dist/entry.js site/dist/entry.js.map site/dist/style.css site/dist/style.css.map site/src/font/SourceSansPro-Light.svg site/src/font/SourceSansPro-Light.ttf site/src/font/SourceSansPro-Light.woff site/src/font/SourceSansPro-Light.woff2 site/src/font/icomoon.svg site/src/font/icomoon.ttf site/src/font/icomoon.woff site/src/font/inconsolata-v15-latin-regular.svg site/src/font/inconsolata-v15-latin-regular.ttf site/src/font/inconsolata-v15-latin-regular.woff site/src/font/inconsolata-v15-latin-regular.woff2 site/src/font/source-sans-pro-semibold.svg site/src/font/source-sans-pro-semibold.ttf site/src/font/source-sans-pro-semibold.woff site/src/font/source-sans-pro-semibold.woff2 site/src/font/source-sans-pro-v9-latin-regular.svg site/src/font/source-sans-pro-v9-latin-regular.ttf site/src/font/source-sans-pro-v9-latin-regular.woff site/src/font/source-sans-pro-v9-latin-regular.woff2 site/src/img/candlestick_bttn.png site/src/img/coins/a.png site/src/img/coins/b.png site/src/img/coins/bch.png site/src/img/coins/btc.png site/src/img/coins/c.png site/src/img/coins/d.png site/src/img/coins/dcr.png site/src/img/coins/dextt.eth.png site/src/img/coins/doge.png site/src/img/coins/e.png site/src/img/coins/eth.png site/src/img/coins/f.png site/src/img/coins/g.png site/src/img/coins/h.png site/src/img/coins/i.png site/src/img/coins/j.png site/src/img/coins/k.png site/src/img/coins/l.png site/src/img/coins/ltc.png site/src/img/coins/m.png site/src/img/coins/mona.png site/src/img/coins/n.png site/src/img/coins/o.png site/src/img/coins/p.png site/src/img/coins/q.png site/src/img/coins/r.png site/src/img/coins/s.png site/src/img/coins/t.png site/src/img/coins/u.png site/src/img/coins/v.png site/src/img/coins/vtc.png site/src/img/coins/w.png site/src/img/coins/x.png site/src/img/coins/y.png site/src/img/coins/z.png site/src/img/coins/zec.png site/src/img/depth_bttn.png site/src/img/favicon.png site/src/img/logo_wide_dark_v1.svg site/src/img/logo_wide_v1.svg site/src/localized_html/en-US/bodybuilder.tmpl site/src/localized_html/en-US/dexsettings.tmpl site/src/localized_html/en-US/forms.tmpl site/src/localized_html/en-US/login.tmpl site/src/localized_html/en-US/markets.tmpl site/src/localized_html/en-US/order.tmpl site/src/localized_html/en-US/orders.tmpl site/src/localized_html/en-US/register.tmpl site/src/localized_html/en-US/settings.tmpl site/src/localized_html/en-US/wallets.tmpl site/src/localized_html/pl-PL/bodybuilder.tmpl site/src/localized_html/pl-PL/dexsettings.tmpl site/src/localized_html/pl-PL/forms.tmpl site/src/localized_html/pl-PL/login.tmpl site/src/localized_html/pl-PL/markets.tmpl site/src/localized_html/pl-PL/order.tmpl site/src/localized_html/pl-PL/orders.tmpl site/src/localized_html/pl-PL/register.tmpl site/src/localized_html/pl-PL/settings.tmpl site/src/localized_html/pl-PL/wallets.tmpl site/src/localized_html/pt-BR/bodybuilder.tmpl site/src/localized_html/pt-BR/dexsettings.tmpl site/src/localized_html/pt-BR/forms.tmpl site/src/localized_html/pt-BR/login.tmpl site/src/localized_html/pt-BR/markets.tmpl site/src/localized_html/pt-BR/order.tmpl site/src/localized_html/pt-BR/orders.tmpl site/src/localized_html/pt-BR/register.tmpl site/src/localized_html/pt-BR/settings.tmpl site/src/localized_html/pt-BR/wallets.tmpl site/src/localized_html/zh-CN/bodybuilder.tmpl site/src/localized_html/zh-CN/dexsettings.tmpl site/src/localized_html/zh-CN/forms.tmpl site/src/localized_html/zh-CN/login.tmpl site/src/localized_html/zh-CN/markets.tmpl site/src/localized_html/zh-CN/order.tmpl site/src/localized_html/zh-CN/orders.tmpl site/src/localized_html/zh-CN/register.tmpl site/src/localized_html/zh-CN/settings.tmpl site/src/localized_html/zh-CN/wallets.tmpl]

Default:

[DBG] WEB: Using embedded site resources.
[INF] WEB: Using localized HTML templates in <embedded>/site/src/localized_html/en-US

With --reload-html:

[DBG] WEB: Looking for site in /home/jon/github/decred/dcrdex/client/cmd/dexc/site
[DBG] WEB: Looking for site in /home/jon/github/decred/dcrdex/client/cmd/dexc/site
[DBG] WEB: Looking for site in /home/jon/github/decred/dcrdex/client/webserver/site
[INF] WEB: Located "site" folder at /home/jon/github/decred/dcrdex/client/webserver/site
[INF] WEB: Using localized HTML templates in /home/jon/github/decred/dcrdex/client/webserver/site/src/localized_html/en-US

With --lang pt:

[DBG] WEB: Using embedded site resources.
[INF] WEB: Using localized HTML templates in <embedded>/site/src/localized_html/pt-BR

With --lang pt --reload-html:

[DBG] WEB: Looking for site in /home/jon/github/decred/dcrdex/client/cmd/dexc/site
[DBG] WEB: Looking for site in /home/jon/github/decred/dcrdex/client/cmd/dexc/site
[DBG] WEB: Looking for site in /home/jon/github/decred/dcrdex/client/webserver/site
[INF] WEB: Located "site" folder at /home/jon/github/decred/dcrdex/client/webserver/site
[INF] WEB: Using localized HTML templates in /home/jon/github/decred/dcrdex/client/webserver/site/src/localized_html/pt-BR

Subsequent work should look at removing the localized_html folder from source control and instead generate the localized html templates at runtime.

Comment on lines +44 to +45
echo "Files embedded in the Go webserver package:"
go list -f '{{ .EmbedFiles }}' decred.org/dcrdex/client/webserver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's how we'll verify that the binaries have the intended site files embedded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm mistaken about the go list command. e.g. I make the build with the dist dir and it embeds, then I can do the go list command and it shows the files. Then I rm -r dist and do the list command again:

$  go list -f '{{ .EmbedFiles }}' decred.org/dcrdex/client/webserver
../webserver.go:81:13: pattern site/dist: no matching files found

So clearly that go list is not just checking the compiled webserver package in the go package cache or whatever, but something based on the current files.

In this pkg script it probably is doing what we expect, but in general it's not.

Copy link
Member

@JoeGruffins JoeGruffins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working well.

client/cmd/dexc/config.go Outdated Show resolved Hide resolved
Copy link
Contributor

@martonp martonp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

# Generate the localized_html and build the webpack bundle prior to building the
# webserver package, which embeds the files.
pushd client/webserver/site
go generate # should be a no-op
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, what's the reason for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure the templates in localized_html correspond to the sources. CI makes us check them in presently so it's likely a no-op in this script if it's being run on an unmodified git revision.

client/webserver/template.go Outdated Show resolved Hide resolved
Copy link
Member

@buck54321 buck54321 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I see after 1) switch to this branch, 2) go build -tags lgpl, 3) ./dex --simnet --rpc --log=debug

image

No asset logos, but I can see e.g. the dcrdex logo (from a different directory). If I

go list -f '{{ .EmbedFiles }}' decred.org/dcrdex/client/webserver

I can see all of the image files.

On the front side, I'm seeing status 200 on the images requests, but the files appear to be empty.

I'm running with the cache disabled.

client/webserver/webserver.go Outdated Show resolved Hide resolved
@chappjc
Copy link
Member

chappjc commented Aug 16, 2022

I managed to repro the "empty" files. For me it randomly made the big logo (logo_wide_dark_v1.svg) blank.

image

Point the browser directly at it:

image

The reason seems to be a MIME issue:

image

(png instead of svg).

I downloaded the file and the contents were correct.

Not clear why it only sometimes happens, but it's gotta be related to content type so we know what do debug I think.

Co-authored-by: Jonathan Chappelow <chappjc@gmail.com>
Copy link
Member Author

@vctt94 vctt94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working as properly now.

Thanks for the help with this work @chappjc.

@chappjc
Copy link
Member

chappjc commented Aug 17, 2022

Np. Sorry for rushing it as a late addition to 0.5

@chappjc chappjc merged commit 2921979 into decred:master Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

site: look into go:embed for the site resources
5 participants