urlshort

package module
v0.0.0-...-1cbbcc6 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONHandler

func JSONHandler(jsn []byte, fallback http.Handler) (http.HandlerFunc, error)

JSONHandler will parse the provided JSON and then return an http.HandlerFunc (which also implements http.Handler) that will attempt to map any paths to their corresponding URL. If the path is not provided in the JSON, then the fallback http.Handler will be called instead.

JSON is expected to be in the format:

[
  {
    "path": "/some-path",
    "url": "https://www.some-url.com/demo"
  }
]

The only errors that can be returned all related to having invalid JSON data.

See MapHandler to create a similar http.HandlerFunc via a mapping of paths to URLs.

func MapHandler

func MapHandler(pathsToURLs map[string]string, fallback http.Handler) http.HandlerFunc

MapHandler will return an http.HandlerFunc (which also implements http.Handler) that will attempt to map any paths (keys in the map) to their corresponding URL (values that each key in the map points to, in string format). If the path is not provided in the map, then the fallback http.Handler will be called instead.

func StoreHandler

func StoreHandler(store RuleStore, initialRules []Rule, fallback http.Handler) (http.HandlerFunc, error)

StoreHandler returns an http.HandlerFunc (which also implements http.Handler) that will dynamically to map any paths to their corresponding URL retrieved from a store. If the path is not provided in the store then the fallback http.Handler will be called instead.

The only errors that can be returned all related to having invalid store data.

See MapHandler to create a similar http.HandlerFunc via a mapping of paths to URLs.

func YAMLHandler

func YAMLHandler(yml []byte, fallback http.Handler) (http.HandlerFunc, error)

YAMLHandler will parse the provided YAML and then return an http.HandlerFunc (which also implements http.Handler) that will attempt to map any paths to their corresponding URL. If the path is not provided in the YAML, then the fallback http.Handler will be called instead.

YAML is expected to be in the format:

The only errors that can be returned all related to having invalid YAML data.

See MapHandler to create a similar http.HandlerFunc via a mapping of paths to URLs.

Types

type Rule

type Rule struct {
	Path string `yaml:"path"`
	URL  string `yaml:"url"`
}

Rule is a rule to forward a path to a specific URL.

type RuleStore

type RuleStore interface {
	Get(ctx context.Context, path string) (Rule, error)
	Add(ctx context.Context, rule Rule) error
}

RuleStore allows to interact with rules.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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