baker

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2021 License: MIT Imports: 9 Imported by: 0

README

   ____        _
  | __ )  __ _| | _____ _ __      __ _  ___
  |  _ \ / _  | |/ / _ \ '__|    / _  |/ _ \
  | |_) | (_| |   <  __/ |   _  | (_| | (_) |
  |____/ \__,_|_|\_\___|_|  (_)  \__, |\___/
                                 |___/

Introduction

Baker.go is a dynamic http reverse proxy designed to be highly extensible.

Features

  • Include Docker driver to listen to Docker's events
  • Has exposed a driver interface which can be easily hook to other orchestration engines
  • Dynamic configuration, no need to restart reverse proxy in order to change the configuration
  • Uses only Standard Library
  • Uses a custom trie data structure, to compute fast path pattern matching
  • It can be use as library as it has implemented http.Handler interface
  • Highly extendable as most of the components have exposed interfaces
  • Middleware like feature to change the incoming and outgoing traffics
  • load balancing by default
  • Automatically updates and creates SSL certificates using Let's Encrypt

Usage

First we need to run Baker inside docker. The following docker-compose.yml

version: "3.5"

services:
  baker:
    image: alinz/baker.go:latest

    environment:
      # enables ACME system
      - BAKER_ACME=NO
      # folder location which holds all certification
      - BAKER_ACME_PATH=/acme/cert
      - BAKER_LOG_LEVEL=DEBUG

    ports:
      - "80:80"
      - "443:443"

    # make sure to use the right network
    networks:
      - baker

    volumes:
      # make sure it can access to main docker.sock
      - /var/run/docker.sock:/var/run/docker.sock
      - ./acme/cert:/acme/cert

networks:
  baker:
    name: baker_net
    driver: bridge

Then for each service, a following docker-compose can be used. The only requirements is labels and networks. Make sure both baker and service has the same network interface

version: "3.5"

services:
  service1:
    image: service:latest

    labels:
      - "baker.enable=true"
      - "baker.network=baker_net"
      - "baker.service.port=8000"
      - "baker.service.ping=/config"

    networks:
      - baker

networks:
  baker:
    external:
      name: baker_net

The service, should expose a REST endpoint which returns a configuration, the configuration endpoint act as a health check and providing realtime configuration:

[
  {
    "domain": "example.com",
    "path": "/sample1",
    "ready": true
  },
  {
    "domain": "example.com",
    "path": "/sample2",
    "ready": false
  },
  {
    "domain": "example1.com",
    "path": "/sample1*",
    "ready": true,
    "rules": [
      {
        "name": "ReplacePath",
        "config": {
          "search": "/sample1",
          "replace": "",
          "times": 1
        }
      }
    ]
  }
]

Middleware

At the moment, there are 2 middlewares provided by default

ReplacePath

Remove a specific path from incoming request. Service will be receiving the modified path.

in order to use this middleware, simply add the following rule to rules section of the configuration

{
  "name": "ReplacePath",
  "config": {
    "search": "/sample1",
    "replace": "",
    "times": 1
  }
}
AppendPath

Add a path at the beginning and end of path

in order to use this middleware, simply add the following rule to rules section of the configuration

{
  "name": "AppendPath",
  "config": {
    "begin": "/begin",
    "end": "/end"
  }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWatcherClosed = errors.New("watcher is closed")
)

Functions

func StartAcme added in v1.0.0

func StartAcme(handler http.Handler, cachePath string) error

Types

type Container

type Container interface {
	ID() string
	Addr() string
}

type ContainerRegistor added in v1.0.0

type ContainerRegistor interface {
	UpdateContainer(container Container, endpoint *Endpoint)
	FindContainer(domain, path string) (Container, *Endpoint)
}

type Endpoint added in v1.0.0

type Endpoint struct {
	Domain string `json:"domain"`
	Path   string `json:"path"`
	Rules  []Rule `json:"rules"`
	Ready  bool   `json:"ready"`
}

type EndpointsFetcher added in v1.0.0

type EndpointsFetcher interface {
	FetchEndpoints() ([]*Endpoint, error)
}

type Middleware added in v1.0.0

type Middleware interface {
	Process(next http.Handler) http.Handler
}

type Paths

type Paths struct {
	// contains filtered or unexported fields
}

func NewPaths

func NewPaths() *Paths

func (*Paths) Del added in v1.0.0

func (p *Paths) Del(key []rune)

func (*Paths) Get

func (p *Paths) Get(key []rune) *Service

func (*Paths) Put added in v1.0.0

func (p *Paths) Put(key []rune, val *Service)

func (*Paths) Size added in v1.0.0

func (p *Paths) Size() int

type Registry added in v1.0.0

type Registry struct {
	// contains filtered or unexported fields
}

func NewRegistry added in v1.0.0

func NewRegistry() *Registry

func (*Registry) FindContainer added in v1.0.0

func (r *Registry) FindContainer(domain string, path string) (Container, *Endpoint)

func (*Registry) UpdateContainer added in v1.0.0

func (r *Registry) UpdateContainer(container Container, endpoint *Endpoint)

type Rule

type Rule struct {
	Type string          `json:"type"`
	Args json.RawMessage `json:"args"`
}

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService added in v1.0.0

func NewService() *Service

func (*Service) Add added in v0.1.0

func (s *Service) Add(endpoint *Endpoint, container Container)

func (*Service) Random added in v1.0.0

func (s *Service) Random() (Container, *Endpoint)

func (*Service) Remove added in v0.1.0

func (s *Service) Remove(container Container)

func (*Service) Size added in v1.0.0

func (s *Service) Size() int

type Services

type Services struct {
	// contains filtered or unexported fields
}

func NewServices

func NewServices() *Services

func (*Services) Add

func (s *Services) Add(endpoint *Endpoint, container Container)

func (*Services) Get added in v1.0.0

func (s *Services) Get(path string) (Container, *Endpoint)

func (*Services) Remove added in v1.0.0

func (s *Services) Remove(endpoint *Endpoint, container Container)

type Watcher

type Watcher interface {
	Watch(errs chan<- error) <-chan Container
}

Directories

Path Synopsis
cmd
baker command
examples
service command

Jump to

Keyboard shortcuts

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