diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4df6974 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.asar diff --git a/backend/christine.website/main.go b/backend/christine.website/main.go index 4da8d64..d4063a4 100644 --- a/backend/christine.website/main.go +++ b/backend/christine.website/main.go @@ -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) { diff --git a/box.rb b/box.rb index b0a3e30..9d4e6ac 100644 --- a/box.rb +++ b/box.rb @@ -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" diff --git a/build.sh b/build.sh index 1f8c51c..0a2656f 100755 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/containermake.sh b/containermake.sh new file mode 100755 index 0000000..0777702 --- /dev/null +++ b/containermake.sh @@ -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 diff --git a/run.sh b/run.sh index a89bb2a..d3fb7be 100755 --- a/run.sh +++ b/run.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh cd /site /usr/bin/christine.website diff --git a/runit/backplane/run b/runit/backplane/run deleted file mode 100755 index 0394912..0000000 --- a/runit/backplane/run +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -if [ -n "$BACKPLANE_TOKEN" ] -then - /usr/bin/backplane connect -else - sleep 999d -fi