Skip to content

Commit

Permalink
Putting cube back
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Feb 18, 2018
1 parent 05b673b commit 5415ab4
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 15 deletions.
48 changes: 36 additions & 12 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@

[[constraint]]
name = "github.com/casbin/casbin"
version = "1.0.0"
version = "1.4.0"

[[constraint]]
name = "github.com/gorilla/context"
version = "1.1.0"

[[constraint]]
name = "github.com/gorilla/sessions"
version = "1.1.0"

[[constraint]]
name = "github.com/labstack/echo"
version = "3.2.3"
version = "3.2.6"

[[constraint]]
name = "github.com/labstack/labstack-go"
version = "0.20.1"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.1.4"
version = "1.2.1"
106 changes: 106 additions & 0 deletions cube/cube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cube

import (
"time"

"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/labstack/labstack-go"
)

type (
// Config defines the config for LabStack cube middleware.
Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper

// LabStack account id
AccountID string `json:"account_id"`

// LabStack api key
APIKey string `json:"api_key"`

// API node
Node string `json:"node"`

// API group
Group string `json:"group"`

// Number of requests in a batch
BatchSize int `json:"batch_size"`

// Interval in seconds to dispatch the batch
DispatchInterval time.Duration `json:"dispatch_interval"`

// Additional tags
Tags []string `json:"tags"`

// TODO: To be implemented
ClientLookup string `json:"client_lookup"`
}
)

var (
// DefaultConfig is the default LabStack cube middleware config.
DefaultConfig = Config{
Skipper: middleware.DefaultSkipper,
BatchSize: 60,
DispatchInterval: 60,
}
)

// Middleware implements LabStack cube middleware.
func Middleware(accountID, apiKey string) echo.MiddlewareFunc {
c := DefaultConfig
c.APIKey = apiKey
return MiddlewareWithConfig(c)
}

// MiddlewareWithConfig returns a LabStack cube middleware with config.
// See: `Middleware()`.
func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
// Defaults
if config.APIKey == "" {
panic("echo: labstack analytics middleware requires an api key")
}
if config.Skipper == nil {
config.Skipper = DefaultConfig.Skipper
}
if config.BatchSize == 0 {
config.BatchSize = DefaultConfig.BatchSize
}
if config.DispatchInterval == 0 {
config.DispatchInterval = DefaultConfig.DispatchInterval
}

// Initialize
client := labstack.NewClient(config.AccountID, config.APIKey)
cube := client.Cube()
cube.Node = config.Node
cube.Group = config.Group
cube.Tags = config.Tags
cube.BatchSize = config.BatchSize
cube.DispatchInterval = config.DispatchInterval
cube.ClientLookup = config.ClientLookup

return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
if config.Skipper(c) {
return next(c)
}

// Start
r := cube.Start(c.Request(), c.Response())

// Next
if err = next(c); err != nil {
c.Error(err)
}

// Stop
cube.Stop(r, c.Response().Status, c.Response().Size)

return nil
}
}
}

0 comments on commit 5415ab4

Please sign in to comment.