Skip to content

Commit eb050a9

Browse files
committed
Added cube middleware
Signed-off-by: Vishal Rana <[email protected]>
1 parent 7099b86 commit eb050a9

File tree

3 files changed

+154
-77
lines changed

3 files changed

+154
-77
lines changed

Gopkg.lock

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

Gopkg.toml

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,38 @@
11

2-
## Gopkg.toml example (these lines may be deleted)
3-
4-
## "metadata" defines metadata about the project that could be used by other independent
5-
## systems. The metadata defined here will be ignored by dep.
6-
# [metadata]
7-
# key1 = "value that convey data to other systems"
8-
# system1-data = "value that is used by a system"
9-
# system2-data = "value that is used by another system"
10-
11-
## "required" lists a set of packages (not projects) that must be included in
12-
## Gopkg.lock. This list is merged with the set of packages imported by the current
13-
## project. Use it when your project needs a package it doesn't explicitly import -
14-
## including "main" packages.
15-
# required = ["github.com/user/thing/cmd/thing"]
16-
17-
## "ignored" lists a set of packages (not projects) that are ignored when
18-
## dep statically analyzes source code. Ignored packages can be in this project,
19-
## or in a dependency.
20-
# ignored = ["github.com/user/project/badpkg"]
21-
22-
## Constraints are rules for how directly imported projects
23-
## may be incorporated into the depgraph. They are respected by
24-
## dep whether coming from the Gopkg.toml of the current project or a dependency.
25-
# [[constraint]]
26-
## Required: the root import path of the project being constrained.
27-
# name = "github.com/user/project"
2+
# Gopkg.toml example
283
#
29-
## Recommended: the version constraint to enforce for the project.
30-
## Only one of "branch", "version" or "revision" can be specified.
31-
# version = "1.0.0"
32-
# branch = "master"
33-
# revision = "abc123"
4+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
5+
# for detailed Gopkg.toml documentation.
346
#
35-
## Optional: an alternate location (URL or import path) for the project's source.
36-
# source = "https://github.com/myfork/package.git"
7+
# required = ["github.com/user/thing/cmd/thing"]
8+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
379
#
38-
## "metadata" defines metadata about the dependency or override that could be used
39-
## by other independent systems. The metadata defined here will be ignored by dep.
40-
# [metadata]
41-
# key1 = "value that convey data to other systems"
42-
# system1-data = "value that is used by a system"
43-
# system2-data = "value that is used by another system"
44-
45-
## Overrides have the same structure as [[constraint]], but supersede all
46-
## [[constraint]] declarations from all projects. Only [[override]] from
47-
## the current project's are applied.
48-
##
49-
## Overrides are a sledgehammer. Use them only as a last resort.
50-
# [[override]]
51-
## Required: the root import path of the project being constrained.
52-
# name = "github.com/user/project"
10+
# [[constraint]]
11+
# name = "github.com/user/project"
12+
# version = "1.0.0"
5313
#
54-
## Optional: specifying a version constraint override will cause all other
55-
## constraints on this project to be ignored; only the overridden constraint
56-
## need be satisfied.
57-
## Again, only one of "branch", "version" or "revision" can be specified.
58-
# version = "1.0.0"
59-
# branch = "master"
60-
# revision = "abc123"
14+
# [[constraint]]
15+
# name = "github.com/user/project2"
16+
# branch = "dev"
17+
# source = "github.com/myfork/project2"
6118
#
62-
## Optional: specifying an alternate source location as an override will
63-
## enforce that the alternate location is used for that project, regardless of
64-
## what source location any dependent projects specify.
65-
# source = "https://github.com/myfork/package.git"
66-
19+
# [[override]]
20+
# name = "github.com/x/y"
21+
# version = "2.4.0"
6722

6823

6924
[[constraint]]
7025
name = "github.com/casbin/casbin"
71-
version = "0.2.0"
26+
version = "0.8.0"
7227

7328
[[constraint]]
74-
branch = "master"
75-
name = "github.com/labstack/echo"
29+
name = "github.com/gorilla/sessions"
30+
version = "1.1.0"
7631

7732
[[constraint]]
78-
name = "github.com/labstack/gommon"
79-
version = "0.2.1"
33+
name = "github.com/labstack/echo"
34+
version = "3.2.1"
8035

8136
[[constraint]]
8237
branch = "master"
83-
name = "golang.org/x/crypto"
38+
name = "github.com/labstack/labstack-go"

cube/cube.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 Cube middleware.
13+
Config struct {
14+
// Skipper defines a function to skip middleware.
15+
Skipper middleware.Skipper
16+
17+
// Node name
18+
Node string `json:"node"`
19+
20+
// Node group
21+
Group string `json:"group"`
22+
23+
// LabStack API key
24+
APIKey string `json:"api_key"`
25+
26+
// Number of requests in a batch
27+
BatchSize int `json:"batch_size"`
28+
29+
// Interval in seconds to dispatch the batch
30+
DispatchInterval time.Duration `json:"dispatch_interval"`
31+
32+
// TODO: To be implemented
33+
ClientLookup string `json:"client_lookup"`
34+
}
35+
)
36+
37+
var (
38+
// DefaultConfig is the default Cube middleware config.
39+
DefaultConfig = Config{
40+
Skipper: middleware.DefaultSkipper,
41+
BatchSize: 60,
42+
DispatchInterval: 60,
43+
}
44+
)
45+
46+
// Middleware implements Cube middleware.
47+
func Middleware(apiKey string) echo.MiddlewareFunc {
48+
c := DefaultConfig
49+
c.APIKey = apiKey
50+
return MiddlewareWithConfig(c)
51+
}
52+
53+
// MiddlewareWithConfig returns a Cube middleware with config.
54+
// See: `Middleware()`.
55+
func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
56+
// Defaults
57+
if config.APIKey == "" {
58+
panic("echo: cube middleware requires an api key")
59+
}
60+
if config.Skipper == nil {
61+
config.Skipper = DefaultConfig.Skipper
62+
}
63+
if config.BatchSize == 0 {
64+
config.BatchSize = DefaultConfig.BatchSize
65+
}
66+
if config.DispatchInterval == 0 {
67+
config.DispatchInterval = DefaultConfig.DispatchInterval
68+
}
69+
70+
// Initialize
71+
cube := labstack.NewClient(config.APIKey).Cube()
72+
cube.Node = config.Node
73+
cube.Group = config.Group
74+
cube.APIKey = config.APIKey
75+
cube.BatchSize = config.BatchSize
76+
cube.DispatchInterval = config.DispatchInterval
77+
cube.ClientLookup = config.ClientLookup
78+
79+
return func(next echo.HandlerFunc) echo.HandlerFunc {
80+
return func(c echo.Context) (err error) {
81+
if config.Skipper(c) {
82+
return next(c)
83+
}
84+
request := cube.Start(c.Request(), c.Response())
85+
if err = next(c); err != nil {
86+
c.Error(err)
87+
}
88+
cube.Stop(request, c.Response().Status, c.Response().Size)
89+
return
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)