|
| 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