redirect

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2021 License: MIT Imports: 12 Imported by: 0

README

HTTP service discovery

Mx

GitHub release license Snap Status

Simple, very fast, in a single binary, with web UI HTTP redirect service

Install

Get it from the Snap Store

go get -v -u github.com/reddec/redirect/...

snapcraft use redirect-to package

WEB panel

web interface

Usage

Docker

Translate this ports as you wish:

  • 10100 - main redirect server
  • 10101 - UI web panel

Use exposed volume /etc/redirect to persist data

CLI

-bind

Redirect address (default "0.0.0.0:10100"). You can do any HTTP operation to address http://your-server:10100/your/cool/service/name and it will be redirected to specified address

-config

File to save configuration (default "./redir.json"). It will be loaded at startup (if exists) and saved after each modification operation over API

-ui

Directory of static UI files. If not defined - embedded used

-ui-addr string

Address for UI (default "127.0.0.1:10101")

  • / - Will be served as static directory from specified directory
  • /ui/ - UI interface
  • /api/ - API handlers

Actions on redirect server

  • GET/POST/PUT/DELETE - returns redirection with 302 Found status
  • HEAD - returns only real service location in Location header with 200 OK status

API

GET

Get list of services or detailed information of one service if service name provided. Adds into response headers (X-Redir-Port) port of main server

  • Endpoint (all): http://ui-addr/api/
  • Endpoint (one): http://ui-addr/api/your/cool/service/name
POST

Add or update one service. If service already exists, hits will saved. Expects this form fields:

  • service - service name
  • template - content of template

Each template must be valid expression of Go template engine with http request as environment.

Simple example

All requests to http://127.0.0.1:10100/google will be redirected to http://google.com

Complex example

All requests to http://127.0.0.1:10100/mdn?q=QUERY will be redirected to https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/QUERY

E.x.:

http://127.0.0.1:10100/mdn?q=encodeuricomponent maps to https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/encodeuricomponent

  • Endpoint: http://ui-addr/api/
DELETE

Remove service if it exists

  • Endpoint: http://ui-addr/api/your/cool/service/name

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultUI

func DefaultUI(storage Storage, stats StatReader, engine Engine, redirPort string) http.Handler

func DefaultUIStatic added in v1.1.0

func DefaultUIStatic() embed.FS

Get default UI static files (prefixed by ui/).

Types

type Engine

type Engine interface {
	http.Handler
	Reload() error // reload configuration from storage
}

Engine of all redirection.

func DefaultEngine

func DefaultEngine(storage Storage, sink StatWriter) Engine

Create default engine based on provided storage and sink.

type JSONStorage added in v1.1.0

type JSONStorage struct {
	FileName string // File name to store and read
	// contains filtered or unexported fields
}

Simple single-file storage. All rules saved as-is by JSON indented encoder to the provided file after each Set ops.

func (*JSONStorage) All added in v1.1.0

func (js *JSONStorage) All() ([]*Rule, error)

All rules stored in cache. Never returns error.

func (*JSONStorage) Get added in v1.1.0

func (js *JSONStorage) Get(url string) (string, bool)

Get single record from cache.

func (*JSONStorage) Reload added in v1.1.0

func (js *JSONStorage) Reload() error

Read all rules from file. Will not update cache if file will not exists.

func (*JSONStorage) Remove added in v1.1.0

func (js *JSONStorage) Remove(url string) error

Remove rule from cache and save dump to disk. Even if dump failed rule removed from cache.

func (*JSONStorage) Set added in v1.1.0

func (js *JSONStorage) Set(url string, locationTemplate string) error

Set or replace one rule, serialize cache to JSON and then dump to disk. Even if dump failed rule is saved into cache.

type Rule

type Rule struct {
	URL              string // Matching URL (aka service name)
	LocationTemplate string // Go-Template of target location
}

Single rule for redirection.

type StatReader

type StatReader interface {
	Visits(url string) int64 // Get number of visits for specific service/url
}

Stats reader.

type StatWriter

type StatWriter interface {
	Touch(url string) // Touch resource and increment counter (hot operation, should be fast)
}

Stats consumer.

type Stats

type Stats interface {
	StatWriter
	StatReader
}

Stats reader and writer.

func InMemoryStats

func InMemoryStats() Stats

type Storage

type Storage interface {
	Set(url string, locationTemplate string) error // add or replace rule
	Get(url string) (string, bool)                 // get location template. should return true if exists
	Remove(url string) error                       // remove rule (or ignore if not exists)
	All() ([]*Rule, error)                         // dump all save rules
	Reload() error                                 // reload storage and fill the internal cache
}

Rules storage type.

type UIEntry

type UIEntry struct {
	Template string `json:"template"`
	Hits     int64  `json:"hits"`
	URL      string `json:"url"`
}

description of rule for API request.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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