build asar file for frontend only

This commit is contained in:
Cadey Ratio 2017-01-09 14:58:07 -08:00
parent b288844193
commit 5c6d5d13ae
7 changed files with 46 additions and 64 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.asar

View File

@ -14,6 +14,7 @@ import (
"github.com/Xe/asarfs"
"github.com/gernest/front"
"github.com/urfave/negroni"
)
// Post is a single post summary for the menu.
@ -129,21 +130,23 @@ func main() {
})
if os.Getenv("USE_ASAR") == "yes" {
fe, err := asarfs.New("./frontend.asar", http.HandlerFunc(writeIndexHTML))
log.Println("serving site frontend from asar file")
do404 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not found", http.StatusNotFound)
})
fe, err := asarfs.New("./frontend.asar", do404)
if err != nil {
log.Fatal(err)
}
st, err := asarfs.New("./static.asar", http.HandlerFunc(writeIndexHTML))
if err != nil {
log.Fatal(err)
log.Fatal("frontend: ", err)
}
http.Handle("/dist/", fe)
http.Handle("/static", st)
} else {
log.Println("serving site frontend from filesystem")
http.Handle("/dist/", http.FileServer(http.Dir("./frontend/static/")))
http.Handle("/static/", http.FileServer(http.Dir(".")))
}
http.Handle("/static/", http.FileServer(http.Dir(".")))
http.HandleFunc("/", writeIndexHTML)
port := os.Getenv("PORT")
@ -151,7 +154,10 @@ func main() {
port = "9090"
}
log.Fatal(http.ListenAndServe(":"+port, nil))
n := negroni.Classic()
n.UseHandler(http.DefaultServeMux)
log.Fatal(http.ListenAndServe(":"+port, n))
}
func writeBlogPosts(w http.ResponseWriter, r *http.Request) {

39
box.rb
View File

@ -1,22 +1,4 @@
from "phusion/baseimage:0.9.19"
copy "./runit/", "/etc/system/"
run %q[ curl -o backplane-stable-linux-amd64.tgz https://bin.equinox.io/c/jWahGASjoRq/backplane-stable-linux-amd64.tgz \
&& tar xf backplane-stable-linux-amd64.tgz \
&& mv backplane /usr/bin/backplane \
&& rm backplane-stable-linux-amd64.tgz ]
run %q[ cd /usr/local && curl -o go.tar.gz https://storage.googleapis.com/golang/go1.7.4.linux-amd64.tar.gz \
&& tar xf go.tar.gz && rm go.tar.gz && mkdir -p /go/src ]
env "GOPATH" => "/go"
run %q[ apt-get update && apt-get install -y git-core \
&& /usr/local/go/bin/go get github.com/gernest/front && /usr/local/go/bin/go get gopkg.in/yaml.v2 ]
run %q[ apt-get update && apt-get install xz-utils bzip2 ]
run %q[ cd /usr/local && curl -o node.tar.xz https://nodejs.org/dist/v6.9.2/node-v6.9.2-linux-x64.tar.xz \
&& tar xf node.tar.xz && mv node-v6.9.2-linux-x64 node && rm node.tar.xz ]
from "xena/go"
### Copy files
run "mkdir -p /site"
@ -28,26 +10,27 @@ end
files = [
"backend",
"blog",
"frontend/package.json",
"frontend/bower.json",
"frontend/webpack.production.config.js",
"frontend/src",
"frontend.asar",
"static",
"build.sh",
"run.sh",
# This file is packaged in the asar file, but the go app relies on being
# able to read it so it can cache the contents in ram.
"frontend/static/dist/index.html",
]
files.each { |x| put(x) }
files.each { |x| put x }
### Build
run %q[ cd /site && bash ./build.sh ]
run "apk add --no-cache git"
run %q[ cd /site && sh ./build.sh ]
### Cleanup
run %q[ rm -rf /usr/local/go /usr/local/node /site/frontend/node_modules /site/frontend/bower_components /go /site/backend /tmp/phantomjs /site/frontend /site/static ]
run %q[ apt-get remove -y xz-utils bzip2 git-core && apt-get -y autoremove && apt-get clean ]
run %q[ rm -rf /usr/local/go /usr/local/node /site/frontend/node_modules /site/frontend/bower_components /go /site/backend /tmp/phantomjs ]
run %q[ apk del go git ]
### Runtime
entrypoint "/sbin/my_init"
cmd "/site/run.sh"
env "USE_ASAR" => "yes"

View File

@ -1,27 +1,19 @@
#!/bin/bash
#!/bin/sh
set -e
set -x
export PATH="$PATH:/usr/local/go/bin:/usr/local/node/bin"
export PATH="$PATH:/usr/local/go/bin"
export CI="true"
npm install -g asar
(cd /site/frontend
yes | npm install
npm install -g bower
yes 2 | bower install --allow-root
npm run build
asar pack static ../frontend.asar
rm -rf bower_components node_modules) &
(cd /site/backend/christine.website
go build
go get github.com/Xe/asarfs
go get github.com/gernest/front
go get layeh.com/asar
go get gopkg.in/yaml.v2
go get github.com/urfave/negroni
go build -v
mv christine.website /usr/bin) &
(cd /site
asar pack static ./static.asar) &
wait
rm -rf /usr/lib/node_modules

8
containermake.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
set -e
set -x
(cd frontend && rm -rf node_modules bower_components && npm install && npm run build && asar pack static ../frontend.asar)
box box.rb

2
run.sh
View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
cd /site
/usr/bin/christine.website

View File

@ -1,8 +0,0 @@
#!/bin/sh
if [ -n "$BACKPLANE_TOKEN" ]
then
/usr/bin/backplane connect
else
sleep 999d
fi