diff --git a/main.go b/main.go index 5c0addcc..0b854b22 100644 --- a/main.go +++ b/main.go @@ -113,17 +113,24 @@ type serverOptions struct { ServiceOptions svc.Options Debug bool svc api.ServerServer + staticBox *packr.Box + templatesBox *packr.Box } func serverOptionsFromCliContext(c *cli.Context) serverOptions { if c.Int("soundcloud-user-id") == 0 || c.String("soundcloud-client-id") == "" { zap.L().Warn("SoundCloud is not configured") } + staticBox := packr.NewBox("./static") + templatesBox := packr.NewBox("./templates") return serverOptions{ - GRPCBind: c.String("grpc-bind"), - HTTPBind: c.String("http-bind"), - Debug: c.Bool("debug"), + GRPCBind: c.String("grpc-bind"), + HTTPBind: c.String("http-bind"), + Debug: c.Bool("debug"), + staticBox: &staticBox, + templatesBox: &templatesBox, ServiceOptions: svc.Options{ + StaticBox: &staticBox, SoundcloudUserID: c.Int("soundcloud-user-id"), SoundcloudClientID: c.String("soundcloud-client-id"), }, @@ -166,19 +173,17 @@ func startHTTPServer(ctx context.Context, opts *serverOptions) error { } // configure HTTP server - staticBox := packr.NewBox("./static") - templatesBox := packr.NewBox("./templates") router := mux.NewRouter() if err := views.Setup(&views.Options{ Router: router, Debug: opts.Debug, Svc: opts.svc, - StaticBox: staticBox, - TemplatesBox: templatesBox, + StaticBox: opts.staticBox, + TemplatesBox: opts.templatesBox, }); err != nil { return errors.Wrap(err, "failed to setup views") } - router.PathPrefix("/").Handler(http.FileServer(staticBox)) + router.PathPrefix("/").Handler(http.FileServer(opts.staticBox)) var routerHandler http.Handler = router if !opts.Debug { diff --git a/svc/svc.go b/svc/svc.go index 8ea93e05..2c40e2b8 100644 --- a/svc/svc.go +++ b/svc/svc.go @@ -6,6 +6,8 @@ import ( "math/rand" "time" + "github.com/gobuffalo/packd" + "github.com/gobuffalo/packr" tpyo "github.com/tpyolang/tpyo-cli" "go.uber.org/zap" @@ -22,6 +24,7 @@ import ( type Options struct { SoundcloudUserID int SoundcloudClientID string + StaticBox *packr.Box } type svc struct { @@ -217,10 +220,15 @@ func (svc *svc) SoundcloudTrack(_ context.Context, input *api.SoundcloudTrackInp } func (svc *svc) Metrics(_ context.Context, input *api.Void) (*api.MetricsOutput, error) { + staticBoxSize := 0 + svc.opts.StaticBox.Walk(func(filepath string, file packd.File) error { + staticBoxSize++ + return nil + }) out := &api.MetricsOutput{ - StaticBoxSize: 0, // FIXME: todo - ServerStartTime: svc.startTime.String(), - ServerCurrentTime: time.Now().String(), + StaticBoxSize: int32(staticBoxSize), + ServerStartTime: svc.startTime.Format(time.RFC3339), + ServerCurrentTime: time.Now().Format(time.RFC3339), ServerUptime: time.Since(svc.startTime).String(), } return out, nil diff --git a/views/funcmap.go b/views/funcmap.go index 40321a51..30614ce0 100644 --- a/views/funcmap.go +++ b/views/funcmap.go @@ -71,6 +71,9 @@ func (f *ctxFuncmap) activePage() string { } func (f *ctxFuncmap) activeMenu() string { + if f == nil || f.req == nil || f.req.URL == nil { + return "home" + } switch path := f.req.URL.Path; { case path == "/": return "home" diff --git a/views/views.go b/views/views.go index 628358e4..8fc47600 100644 --- a/views/views.go +++ b/views/views.go @@ -18,8 +18,8 @@ type Options struct { Router *mux.Router Debug bool Svc api.ServerServer - StaticBox packr.Box - TemplatesBox packr.Box + StaticBox *packr.Box + TemplatesBox *packr.Box } type handlers struct {