Skip to content

Commit

Permalink
feat: add new info in metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jan 27, 2019
1 parent ecce769 commit a71248f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 11 additions & 3 deletions svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -22,6 +24,7 @@ import (
type Options struct {
SoundcloudUserID int
SoundcloudClientID string
StaticBox *packr.Box
}

type svc struct {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions views/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions views/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a71248f

Please sign in to comment.