Skip to content

Commit 5415ab4

Browse files
committed
Putting cube back
Signed-off-by: Vishal Rana <[email protected]>
1 parent 05b673b commit 5415ab4

File tree

3 files changed

+153
-15
lines changed

3 files changed

+153
-15
lines changed

Gopkg.lock

Lines changed: 36 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@
2323

2424
[[constraint]]
2525
name = "github.com/casbin/casbin"
26-
version = "1.0.0"
26+
version = "1.4.0"
27+
28+
[[constraint]]
29+
name = "github.com/gorilla/context"
30+
version = "1.1.0"
2731

2832
[[constraint]]
2933
name = "github.com/gorilla/sessions"
3034
version = "1.1.0"
3135

3236
[[constraint]]
3337
name = "github.com/labstack/echo"
34-
version = "3.2.3"
38+
version = "3.2.6"
39+
40+
[[constraint]]
41+
name = "github.com/labstack/labstack-go"
42+
version = "0.20.1"
3543

3644
[[constraint]]
3745
name = "github.com/stretchr/testify"
38-
version = "1.1.4"
46+
version = "1.2.1"

cube/cube.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package cube
2+
3+
import (
4+
"time"
5+
6+
"github.com/labstack/echo"
7+
"github.com/labstack/echo/middleware"
8+
"github.com/labstack/labstack-go"
9+
)
10+
11+
type (
12+
// Config defines the config for LabStack cube middleware.
13+
Config struct {
14+
// Skipper defines a function to skip middleware.
15+
Skipper middleware.Skipper
16+
17+
// LabStack account id
18+
AccountID string `json:"account_id"`
19+
20+
// LabStack api key
21+
APIKey string `json:"api_key"`
22+
23+
// API node
24+
Node string `json:"node"`
25+
26+
// API group
27+
Group string `json:"group"`
28+
29+
// Number of requests in a batch
30+
BatchSize int `json:"batch_size"`
31+
32+
// Interval in seconds to dispatch the batch
33+
DispatchInterval time.Duration `json:"dispatch_interval"`
34+
35+
// Additional tags
36+
Tags []string `json:"tags"`
37+
38+
// TODO: To be implemented
39+
ClientLookup string `json:"client_lookup"`
40+
}
41+
)
42+
43+
var (
44+
// DefaultConfig is the default LabStack cube middleware config.
45+
DefaultConfig = Config{
46+
Skipper: middleware.DefaultSkipper,
47+
BatchSize: 60,
48+
DispatchInterval: 60,
49+
}
50+
)
51+
52+
// Middleware implements LabStack cube middleware.
53+
func Middleware(accountID, apiKey string) echo.MiddlewareFunc {
54+
c := DefaultConfig
55+
c.APIKey = apiKey
56+
return MiddlewareWithConfig(c)
57+
}
58+
59+
// MiddlewareWithConfig returns a LabStack cube middleware with config.
60+
// See: `Middleware()`.
61+
func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
62+
// Defaults
63+
if config.APIKey == "" {
64+
panic("echo: labstack analytics middleware requires an api key")
65+
}
66+
if config.Skipper == nil {
67+
config.Skipper = DefaultConfig.Skipper
68+
}
69+
if config.BatchSize == 0 {
70+
config.BatchSize = DefaultConfig.BatchSize
71+
}
72+
if config.DispatchInterval == 0 {
73+
config.DispatchInterval = DefaultConfig.DispatchInterval
74+
}
75+
76+
// Initialize
77+
client := labstack.NewClient(config.AccountID, config.APIKey)
78+
cube := client.Cube()
79+
cube.Node = config.Node
80+
cube.Group = config.Group
81+
cube.Tags = config.Tags
82+
cube.BatchSize = config.BatchSize
83+
cube.DispatchInterval = config.DispatchInterval
84+
cube.ClientLookup = config.ClientLookup
85+
86+
return func(next echo.HandlerFunc) echo.HandlerFunc {
87+
return func(c echo.Context) (err error) {
88+
if config.Skipper(c) {
89+
return next(c)
90+
}
91+
92+
// Start
93+
r := cube.Start(c.Request(), c.Response())
94+
95+
// Next
96+
if err = next(c); err != nil {
97+
c.Error(err)
98+
}
99+
100+
// Stop
101+
cube.Stop(r, c.Response().Status, c.Response().Size)
102+
103+
return nil
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)