Skip to content

Commit

Permalink
Refactoring package
Browse files Browse the repository at this point in the history
Signed-off-by: Mathis Joffre <[email protected]>
  • Loading branch information
Joffref committed Aug 25, 2022
1 parent 0d40e1b commit 80807d1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
16 changes: 8 additions & 8 deletions middleware/fiber/middleware.go → fiber_middleware.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fibermiddleware
package opamiddleware

import (
"errors"
Expand All @@ -10,17 +10,17 @@ import (
)

// InputCreationMethod is the method that is used to create the input for the policy.
type InputCreationMethod func(c *fiber.Ctx) (map[string]interface{}, error)
type FiberInputCreationMethod func(c *fiber.Ctx) (map[string]interface{}, error)

type Middleware struct {
type FiberMiddleware struct {
// Config is the configuration for the middleware.
Config *config.Config
// InputCreationMethod is a function that returns the value to be sent to the OPA server.
InputCreationMethod InputCreationMethod `json:"binding_method,omitempty"`
InputCreationMethod FiberInputCreationMethod `json:"binding_method,omitempty"`
}

// NewFiberMiddleware is the constructor for the opa fiber middleware.
func NewFiberMiddleware(cfg *config.Config, input InputCreationMethod) (*Middleware, error) {
func NewFiberMiddleware(cfg *config.Config, input FiberInputCreationMethod) (*FiberMiddleware, error) {
err := cfg.Validate()
if err != nil {
return nil, err
Expand All @@ -30,14 +30,14 @@ func NewFiberMiddleware(cfg *config.Config, input InputCreationMethod) (*Middlew
return nil, errors.New("[opa-middleware-fiber] InputCreationMethod must be provided")
}
}
return &Middleware{
return &FiberMiddleware{
Config: cfg,
InputCreationMethod: input,
}, nil
}

// Use returns the handler for the middleware that is used by fiber to evaluate the request against the policy.
func (g *Middleware) Use() func(c *fiber.Ctx) error {
func (g *FiberMiddleware) Use() func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
if g.Config.Debug {
g.Config.Logger.Printf("[opa-middleware-fiber] Request: %s", c.Request().URI())
Expand Down Expand Up @@ -65,7 +65,7 @@ func (g *Middleware) Use() func(c *fiber.Ctx) error {
}
}

func (g *Middleware) query(c *fiber.Ctx) (bool, error) {
func (g *FiberMiddleware) query(c *fiber.Ctx) (bool, error) {
bind, err := g.InputCreationMethod(c)
if err != nil {
return !g.Config.ExceptedResult, err
Expand Down
16 changes: 8 additions & 8 deletions middleware/gin/middleware.go → gin_middleware.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ginmiddleware
package opamiddleware

import (
"errors"
Expand All @@ -8,16 +8,16 @@ import (
"net/http"
)

type InputCreationMethod func(c *gin.Context) (map[string]interface{}, error)
type GinInputCreationMethod func(c *gin.Context) (map[string]interface{}, error)

type Middleware struct {
type GinMiddleware struct {
Config *config.Config
// BindingMethod is a function that returns the value to be sent to the OPA server.
InputCreationMethod InputCreationMethod `json:"binding_method,omitempty"`
InputCreationMethod GinInputCreationMethod `json:"binding_method,omitempty"`
}

// NewGinMiddleware is the constructor for the opa gin middleware.
func NewGinMiddleware(cfg *config.Config, input InputCreationMethod) (*Middleware, error) {
func NewGinMiddleware(cfg *config.Config, input GinInputCreationMethod) (*GinMiddleware, error) {
err := cfg.Validate()
if err != nil {
return nil, err
Expand All @@ -34,14 +34,14 @@ func NewGinMiddleware(cfg *config.Config, input InputCreationMethod) (*Middlewar
return bind, nil
}
}
return &Middleware{
return &GinMiddleware{
Config: cfg,
InputCreationMethod: input,
}, nil
}

// Use returns the handler for the middleware that is used by gin to evaluate the request against the policy.
func (g *Middleware) Use() func(c *gin.Context) {
func (g *GinMiddleware) Use() func(c *gin.Context) {
return func(c *gin.Context) {
if g.Config.Debug {
g.Config.Logger.Printf("[opa-middleware-gin] Request: %s", c.Request.URL.String())
Expand All @@ -64,7 +64,7 @@ func (g *Middleware) Use() func(c *gin.Context) {
}
}

func (g *Middleware) query(c *gin.Context) (bool, error) {
func (g *GinMiddleware) query(c *gin.Context) (bool, error) {
bind, err := g.InputCreationMethod(c)
if err != nil {
return !g.Config.ExceptedResult, err
Expand Down
2 changes: 1 addition & 1 deletion middleware/http/middleware.go → http_middleware.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package httpmiddleware
package opamiddleware

import (
"github.com/Joffref/opa-middleware/config"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package httpmiddleware
package opamiddleware

import (
"github.com/Joffref/opa-middleware/config"
Expand Down

0 comments on commit 80807d1

Please sign in to comment.