revertigo

package module
v0.0.0-...-0b940fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2024 License: MIT Imports: 13 Imported by: 0

README

RevertiGo

Reverse-proxy library that offers the ability to perform custom actions before and after your request, using pluggable code parts. Add your custom plugins or use some of the existing ones.

The entire proxy is automatically set up using the configuration specified in the configuration YAML file. Simply write your plugins, specify them in the configuration and start the server.

Quickstart

The example in quickstart can be found in cmd/example folder.

Config

A simple configuration looks like this

server:
  host: 0.0.0.0
  port: 8080
proxies:
  poke_api:
    url: "https://pokeapi.co"
    plugins:
      - name: Request logger
        key: request_logger
        type: req
    endpoints:
      - path: "/api/v2/pokemon/{name}"
        methods: ["GET"]
        plugins:
          - name: Some Plugin
            key: response_logger
            type: res
      - path: "/api/v2/generation/{name}"
        methods: ["GET"]

The above configuration will:

  • Create a proxy server listening on 0.0.0.0:8080.
  • Attach handlers for requests incoming to the https://pokeapi.co API, specifically requests for the /api/v2/pokeman/{name} and /api/v2/generation/{name} endpoints
  • The pokeapi proxy defines a request logger plugin
    • It gets applied to all proxied endpoints
  • The api/v2/pokemon/{name} endpoint also loads a response logger plugin
    • Only applies to the specific endpoint
Server

To start the application simply create a file with the following code.

package main

import (
"fmt"
"github.com/ToninoK/revertigo"
"log"
)

func main() {
	// Load your configuration
    configuration, err := revertigo.NewConfiguration(
        "path_to_your_config.yml",
        map[string]revertigo.RequestHandlerSig{},  // Your custom request handler plugins
        map[string]revertigo.ResponseHandlerSig{},  // Your custom response handler plugins
    )
    if err != nil {
        log.Fatal(fmt.Errorf("configuration error", "error", err))
    }

    // Run the server with the loaded configuration
    if err := revertigo.Run(configuration); err != nil {
        log.Fatal(fmt.Errorf("could not start the server", "error", err))
    }
}

THe above will create a configuration and attempt to run the server using the loaded configuration.

YAML Configuration schema

$defs:
  plugin:
    type: object
    required: [name, key, type]
    properties:
      name:
        type: string
      key:
        type: string
      type:
        type: string
        enum: [req, res]
      config:
        type: object
        properties:
          env_prefix: string
          data:
            type: object
            properties:
              ^[a-zA-Z_]*$: string

type: object
properties:
  server:
    type: object
    required: [host, port]
    properties:
      host:
        type: string
        format: ipv4
      port:
        type: integer
        minimum: 1
        maximum: 65535
      plugins:
        type: array
        items:
          $ref: "#/$defs/plugin"
  proxies:
    type: object
    patternProperties:
      ^[a-zA-Z_][a-zA-Z0-9_]*$:
        type: object
        required: [url, endpoints]
        properties:
          url:
            type: string
            format: uri
          plugins:
            type: array
            items:
              $ref: "#/$defs/plugin"
          endpoints:
            type: array
            items:
              type: object
              required: [path, methods]
              properties:
                path:
                  type: string
                methods:
                  type: array
                  items:
                    type: string
                    enum: [GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD]
                plugins:
                  type: array
                  items:
                    $ref: "#/$defs/plugin"
required: [server, proxies]
additionalProperties: false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(config *Configuration) error

Types

type Configuration

type Configuration struct {
	Server  Server
	Proxies map[string]Resource
}

Configuration represents the root configuration structure

func NewConfiguration

func NewConfiguration(
	configPath string,
	requestPlugins map[string]RequestHandlerSig,
	responsePlugins map[string]ResponseHandlerSig,
) (*Configuration, error)

NewConfiguration function generates a configuration structure from a given yaml file Additionally it can also receive user defined plugins for requests/responses The plugins are associated to an endpoint by the key defined in the yaml config

type Endpoint

type Endpoint struct {
	Path    string
	Plugins []Plugin
	Methods []string
}

Endpoint configuration structure

type ErrorResponse

type ErrorResponse struct {
	StatusCode int    `json:"status_code"`
	Message    string `json:"message"`
}

type Plugin

type Plugin struct {
	Key             string
	Type            string
	Tags            []string
	Config          PluginConfig
	RequestHandler  RequestHandlerSig
	ResponseHandler ResponseHandlerSig
}

Plugin configuration structure RequestHandler and ResponseHandler are not set through config but instead additionally mapped on by the configuration

type PluginConfig

type PluginConfig struct {
	EnvPrefix string
	Data      map[string]string
}

PluginConfig specifies the plugin configuration structure

type ProxyResponse

type ProxyResponse struct {
	*http.Response
	// contains filtered or unexported fields
}

func (*ProxyResponse) GetBody

func (resp *ProxyResponse) GetBody() ([]byte, error)

GetBody returns the original body, loading it if necessary

func (*ProxyResponse) SetBody

func (resp *ProxyResponse) SetBody(newBody []byte)

SetBody resets the body to a new value

type RequestHandlerSig

type RequestHandlerSig = func(http.ResponseWriter, *http.Request, map[string]string) *ErrorResponse

type Resource

type Resource struct {
	Url       string
	Plugins   []Plugin // applies to all endpoints
	Endpoints []Endpoint
}

Resource configuration structure

type ResponseHandlerSig

type ResponseHandlerSig = func(*ProxyResponse, map[string]string)

type Server

type Server struct {
	Host    string
	Port    string
	Plugins []Plugin
}

Server represents the configuration for the server part of the proxy

Directories

Path Synopsis
cmd
example command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL