Skip to content

Commit

Permalink
Switch to go:embed, at the cost of 10MB extra binary size..
Browse files Browse the repository at this point in the history
  • Loading branch information
gzuidhof committed Apr 1, 2021
1 parent 3934383 commit a1bccc9
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 97 deletions.
3 changes: 0 additions & 3 deletions starboard/.goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
before:
hooks:
# you may remove this if you don't need go generate
- go generate
builds:
- binary: starboard
Expand All @@ -11,7 +10,5 @@ builds:
goarch:
- amd64
- arm64
# Custom ldflags templates.
# Default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser`.
ldflags:
- -s -w -X github.com/gzuidhof/starboard-cli/starboard/cmd.version={{.Version}} -X github.com/gzuidhof/starboard-cli/starboard/cmd.commit={{.Commit}} -X github.com/gzuidhof/starboard-cli/starboard/cmd.date={{.CommitDate}} -X github.com/gzuidhof/starboard-cli/starboard/cmd.target={{.Env.GOOS}}
8 changes: 2 additions & 6 deletions starboard/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# starboard (cli)

To download a new runtime, specify its version and the target folder in the `go:generate` in main.go:

```bash
go run scripts/download_runtime/main.go starboard-notebook 0.7.1 web/static/vendor/
```

To download a new runtime, specify its version and the target folder in the `go:generate` in main.go.

## Development
To run the serve command with the latest static assets and templates without having to `go generate`, use:
Expand All @@ -14,6 +9,7 @@ To run the serve command with the latest static assets and templates without hav
go run main.go serve --static_folder web/static --templates_folder web/templates
```

Consider it live-reload as long as you are only changing template files :).

## Releases

Expand Down
Binary file removed starboard/assets/images/starboard-cli-logo.png
Binary file not shown.
2 changes: 1 addition & 1 deletion starboard/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
serveCmd.Flags().StringP("port", "p", "8585", "Port to serve files on")
serveCmd.Flags().String("port_secondary", "15742", "Port used as secondary origin (for additional sandboxing)")
serveCmd.Flags().String("port_secondary", "9959", "Port used as secondary origin (for additional sandboxing)")

serveCmd.Flags().String("static_folder", "", "Override where static assets are served from, it uses the embedded assets if not set")
serveCmd.Flags().String("templates_folder", "", "Override where templates are loaded from, it uses the embedded assets if not set")
Expand Down
3 changes: 1 addition & 2 deletions starboard/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module github.com/gzuidhof/starboard-cli/starboard

go 1.15
go 1.16

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
github.com/spf13/afero v1.4.1
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.1
Expand Down
4 changes: 0 additions & 4 deletions starboard/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,13 @@ github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJ
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0 h1:mj/nMDAwTBiaCqMEs4cYCqF7pO6Np7vhy1D1wcQGz+E=
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0/go.mod h1:919LwcH0M7/W4fcZ0/jy0qGght1GIhqyS/EgWGH2j5Q=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 h1:pXY9qYc/MP5zdvqWEUH6SjNiu7VhSjuVFTFiTcphaLU=
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.4.1 h1:asw9sl74539yqavKaglDM5hFpdJVK0Y5Dr/JOgQ89nQ=
github.com/spf13/afero v1.4.1/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
Expand Down Expand Up @@ -263,7 +260,6 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
20 changes: 10 additions & 10 deletions starboard/internal/nbserver/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/http"
"strings"

"github.com/gzuidhof/starboard-cli/starboard/assets/web_static"
"github.com/gzuidhof/starboard-cli/starboard/assets/web_templates"
"github.com/gzuidhof/starboard-cli/starboard/web/static"
"github.com/gzuidhof/starboard-cli/starboard/web/templates"
"github.com/spf13/viper"
)

Expand All @@ -21,24 +21,24 @@ type serveFS struct {
}

func getFileSystems() serveFS {
var static http.FileSystem
var templates http.FileSystem
var staticFS http.FileSystem
var templatesFS http.FileSystem

if viper.GetString("static_folder") != "" {
static = http.Dir(viper.GetString("static_folder"))
staticFS = http.Dir(viper.GetString("static_folder"))
} else {
static = web_static.StaticAssets
staticFS = http.FS(static.FS)
}

if viper.GetString("templates_folder") != "" {
templates = http.Dir(viper.GetString("templates_folder"))
templatesFS = http.Dir(viper.GetString("templates_folder"))
} else {
templates = web_templates.TemplateAssets
templatesFS = http.FS(templates.FS)
}

return serveFS{
static: static,
templates: templates,
static: staticFS,
templates: templatesFS,
}
}

Expand Down
5 changes: 2 additions & 3 deletions starboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

package main

//go:generate go run web/assets_generate.go
//go:generate go run scripts/download_runtime/main.go starboard-notebook 0.7.1 web/static/vendor/
//go:generate go run scripts/download_runtime/main.go iframe-resizer 4.2.11 web/static/vendor/
//go:generate go run scripts/download_runtime/main.go starboard-notebook 0.8.2 web/static/vendor/
//go:generate go run scripts/download_runtime/main.go starboard-wrap 0.2.3 web/static/vendor/

import (
"github.com/gzuidhof/starboard-cli/starboard/cmd"
Expand Down
30 changes: 0 additions & 30 deletions starboard/web/assets_generate.go

This file was deleted.

8 changes: 8 additions & 0 deletions starboard/web/static/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package static

import (
"embed"
)

//go:embed *
var FS embed.FS
54 changes: 16 additions & 38 deletions starboard/web/templates/editor.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@
</div>
</r-cell>
</r-grid>
<iframe
title="Starboard Notebook Sandbox iFrame"
id="notebook-iframe"
class="full-page-width"
src="{{.iframeHost}}/static/vendor/[email protected]/dist/index.html"
sandbox="allow-scripts allow-modals allow-same-origin allow-pointer-lock allow-top-navigation-by-user-activation allow-forms allow-downloads"
frameborder="0"
></iframe>
<div id="notebook-mount"></div>
<hr style="margin-bottom: 0;">
<details>
<summary><span style="font-weight: 700; font-size: 0.8em; color: #333" >Current notebook content</span></summary>
Expand All @@ -30,8 +23,8 @@

{{template "footer"}}

<script src="/static/vendor/[email protected]/js/iframeResizer.min.js"></script>
<script type="module">
import {StarboardNotebookIFrame} from "/static/vendor/[email protected]/dist/index.js"
const initialNotebookContent = {{.notebookContent}};

let currentNotebookContent;
Expand Down Expand Up @@ -74,34 +67,19 @@
updateDirtyIndicators(false);
}

window.iFrameComponent = window.iFrameResize({ // Check the iframeResizer docs&code for the options here
autoResize: true,
checkOrigin: [
"{{.iframeHost}}",
],
onMessage: (messageData) => {
// This message is sent when the notebook is ready
// Respond to this message with the initial content of the notebook.
//
// The iFrame will send this message multiple times until you set the content.
// Note that you don't have to reply synchronously: you can wait for the content to be loaded from say a remote server
if (messageData.message.type === "SIGNAL_READY") {
window.iFrameComponent[0].iFrameResizer.sendMessage({
type: "SET_NOTEBOOK_CONTENT", data: initialNotebookContent
})

// Whenever the notebook content gets changed (e.g. a character is typed)
// the entire content is sent to the parent website.
} else if (messageData.message.type === "NOTEBOOK_CONTENT_UPDATE") {
updateContent(messageData.message.data);

// This signal is sent when a save shortcut (e.g. cmd+s on mac) is pressed.
} else if (messageData.message.type === "SAVE") {
updateContent(messageData.message.data);
save(messageData.message.data); // Implement your own save function..
}
const mount = document.querySelector("#notebook-mount");
const el = new StarboardNotebookIFrame({
notebookContent: currentNotebookContent,
src: "{{.iframeHost}}/static/vendor/[email protected]/dist/index.html",
onSaveMessage(payload) {
updateContent(payload.content);
save(payload.content); // Implement your own save function..
},
onReady: () => {},
inPageLinks: true,
}, document.querySelector("#notebook-iframe"));
onContentUpdateMessage(payload) {
updateContent(payload.content);
},

});
el.style.width = "100%";
mount.appendChild(el);
</script>
8 changes: 8 additions & 0 deletions starboard/web/templates/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package templates

import (
"embed"
)

//go:embed *
var FS embed.FS

0 comments on commit a1bccc9

Please sign in to comment.