Skip to content

Commit

Permalink
switch to statik pkg to serve dev ui
Browse files Browse the repository at this point in the history
packr2 was not able to pack templates/ui properly, so had to switch to statik.
  • Loading branch information
ilgooz committed Aug 8, 2020
1 parent 0cfba78 commit 712b531
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 23 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/karrick/godirwalk v1.15.6 // indirect
github.com/pkg/errors v0.9.1
github.com/radovskyb/watcher v1.0.7
github.com/rakyll/statik v0.1.7
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect
github.com/sirupsen/logrus v1.6.0 // indirect
Expand All @@ -29,6 +30,5 @@ require (
golang.org/x/mod v0.3.0
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82 // indirect
golang.org/x/tools v0.0.0-20200806234136-990129eca547 // indirect
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9 // indirect
)
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ github.com/gobuffalo/logger v1.0.3/go.mod h1:SoeejUwldiS7ZsyCBphOGURmWdwUFXs0J7T
github.com/gobuffalo/packd v0.3.0/go.mod h1:zC7QkmNkYVGKPw4tHpBQ+ml7W/3tIebgeo1b36chA3Q=
github.com/gobuffalo/packd v1.0.0 h1:6ERZvJHfe24rfFmA9OaoKBdC7+c9sydrytMg8SdFGBM=
github.com/gobuffalo/packd v1.0.0/go.mod h1:6VTc4htmJRFB7u1m/4LeMTWjFoYrUiBkU9Fdec9hrhI=
github.com/gobuffalo/packr v1.30.1 h1:hu1fuVR3fXEZR7rXNW3h8rqSML8EVAf6KNm0NKO/wKg=
github.com/gobuffalo/packr/v2 v2.7.1/go.mod h1:qYEvAazPaVxy7Y7KR0W8qYEE+RymX74kETFqjFoFlOc=
github.com/gobuffalo/packr/v2 v2.8.0 h1:IULGd15bQL59ijXLxEvA5wlMxsmx/ZkQv9T282zNVIY=
github.com/gobuffalo/packr/v2 v2.8.0/go.mod h1:PDk2k3vGevNE3SwVyVRgQCCXETC9SaONCNSXT1Q8M1g=
Expand Down Expand Up @@ -202,6 +201,8 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down Expand Up @@ -259,7 +260,6 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
Expand Down Expand Up @@ -318,8 +318,8 @@ golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82 h1:6cBnXxYO+CiRVrChvCosSv7magqTPbyAgz1M8iOv5wM=
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9 h1:yi1hN8dcqI9l8klZfy4B8mJvFmmAxJEePIQQFNSd7Cs=
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand All @@ -334,8 +334,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191224055732-dd894d0a8a40/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200308013534-11ec41452d41 h1:9Di9iYgOt9ThCipBxChBVhgNipDoE5mxO84rQV7D0FE=
golang.org/x/tools v0.0.0-20200308013534-11ec41452d41/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
golang.org/x/tools v0.0.0-20200806234136-990129eca547 h1:HjPd370wmFEFsBBdDEikoje9tDY3OHE7UB2erLJBmss=
golang.org/x/tools v0.0.0-20200806234136-990129eca547/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
3 changes: 3 additions & 0 deletions scripts/gen-ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

statik -src=starport/ui/dist -dest=starport/ui/dist-go
8 changes: 8 additions & 0 deletions starport/internal/tools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build tools

package tools

// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
import (
_ "github.com/rakyll/statik"
)
29 changes: 20 additions & 9 deletions starport/services/serve/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package starportserve
import (
"net/http"

"github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/tendermint/starport/starport/pkg/httpstatuschecker"
"github.com/tendermint/starport/starport/pkg/xexec"
"github.com/tendermint/starport/starport/pkg/xhttp"
"golang.org/x/sync/errgroup"

// register dev ui for statik filesystem.
_ "github.com/tendermint/starport/starport/ui/dist-go/statik"
)

const (
Expand Down Expand Up @@ -39,27 +42,35 @@ type env struct {
type development struct {
app App
conf Config
uifs http.FileSystem
}

// Config used to configure development handler.
type Config struct {
EngineAddr string
AppBackendAddr string
AppFrontendAddr string
DevFrontendAssetsPath string
EngineAddr string
AppBackendAddr string
AppFrontendAddr string
}

// newDevHandler creates a new development server handler for app by given conf.
func newDevHandler(app App, conf Config) http.Handler {
dev := &development{app, conf}
func newDevHandler(app App, conf Config) (http.Handler, error) {
uifs, err := fs.New()
if err != nil {
return nil, err
}
dev := &development{
app: app,
conf: conf,
uifs: uifs,
}
router := mux.NewRouter()
router.Handle("/status", dev.statusHandler()).Methods(http.MethodGet)
router.PathPrefix("/").Handler(dev.devAssetsHandler()).Methods(http.MethodGet)
return router
return router, nil
}

func (d *development) devAssetsHandler() http.Handler {
return http.FileServer(packr.New("ui/dist", d.conf.DevFrontendAssetsPath))
return http.FileServer(d.uifs)
}

func (d *development) statusHandler() http.Handler {
Expand Down
15 changes: 9 additions & 6 deletions starport/services/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,22 +434,25 @@ func (s *starportServe) watchAppFrontend(ctx context.Context) error {

func (s *starportServe) runDevServer(ctx context.Context) error {
conf := Config{
EngineAddr: "http://localhost:26657",
AppBackendAddr: "http://localhost:1317",
AppFrontendAddr: "http://localhost:8080",
DevFrontendAssetsPath: "../../ui/dist",
EngineAddr: "http://localhost:26657",
AppBackendAddr: "http://localhost:1317",
AppFrontendAddr: "http://localhost:8080",
} // TODO get vals from const
handler, err := newDevHandler(s.app, conf)
if err != nil {
return err
}
sv := &http.Server{
Addr: ":12345",
Handler: newDevHandler(s.app, conf),
Handler: handler,
}
go func() {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
sv.Shutdown(shutdownCtx)
}()
err := sv.ListenAndServe()
err = sv.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
return nil
}
Expand Down
14 changes: 14 additions & 0 deletions starport/ui/dist-go/statik/statik.go

Large diffs are not rendered by default.

0 comments on commit 712b531

Please sign in to comment.