endpoint

package
v0.0.0-...-0dabf1d Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

endpoint

Both human and machine readable endpoint format.

Format

<protocol>:<value>+

For example:

  • File: file:/var/cache/data
  • HTTP: http:example.com:80
  • HTTPS: https:example.com:443

Quick Start

ep, err := endpoint.Parse("https:example.com")
if err != nil {
	log.Fatal("parse: ", err)
}

switch ep.Protocol() {
case ProtocolHTTP:
    url, host, port := ep.HTTP()
    log.Println("url: ", url)
    log.Println("host: ", host)
    log.Println("port: ", port)
case ProtocolHTTPS:
    url, host, port := ep.HTTPS()
    log.Println("url: ", url)
    log.Println("host: ", host)
    log.Println("port: ", port)
case ProtocolFile:
    path := ep.File()
    log.Println("path: ", path)
default:
    panic("unsupported protocol")
}

Documentation

Index

Examples

Constants

View Source
const (
	// ProtocolHTTP is the http endpoint protocol.
	ProtocolHTTP = "http"
	// ProtocolHTTPS is the https endpoint protocol.
	ProtocolHTTPS = "https"
	// ProtocolFile is the file endpoint protocol
	ProtocolFile = "file"
	// ProtocolTCP is the tcp endpoint protocol
	ProtocolTCP = "tcp"
)

Variables

View Source
var (
	// ErrUnsupportedProtocol will return if protocol is unsupported.
	ErrUnsupportedProtocol = errors.New("unsupported protocol")
	// ErrInvalidValue means value is invalid.
	ErrInvalidValue = errors.New("invalid value")
)

Functions

This section is empty.

Types

type Endpoint

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

func NewFile

func NewFile(path string) Endpoint

func NewHTTP

func NewHTTP(host string, port int) Endpoint

func NewHTTPS

func NewHTTPS(host string, port int) Endpoint

func NewTCP

func NewTCP(host string, port int) Endpoint

func Parse

func Parse(cfg string) (p Endpoint, err error)

Parse will parse config string to create a endpoint Endpoint.

Example
ep, err := Parse("http:example.com")
if err != nil {
	log.Fatal(err)
}

switch ep.Protocol() {
case ProtocolHTTP:
	url, host, port := ep.HTTP()
	log.Println("url: ", url)
	log.Println("host: ", host)
	log.Println("port: ", port)
case ProtocolHTTPS:
	url, host, port := ep.HTTPS()
	log.Println("url: ", url)
	log.Println("host: ", host)
	log.Println("port: ", port)
case ProtocolFile:
	path := ep.File()
	log.Println("path: ", path)
case ProtocolTCP:
	addr, host, port := ep.TCP()
	log.Println("addr:", addr)
	log.Println("host:", host)
	log.Println("port", port)
default:
	panic("unsupported protocol")
}
Output:

func (Endpoint) File

func (p Endpoint) File() (path string)

func (Endpoint) HTTP

func (p Endpoint) HTTP() (url, host string, port int)

func (Endpoint) HTTPS

func (p Endpoint) HTTPS() (url, host string, port int)

func (Endpoint) Protocol

func (p Endpoint) Protocol() string

func (Endpoint) String

func (p Endpoint) String() string

func (Endpoint) TCP

func (p Endpoint) TCP() (addr, host string, port int)

type Error

type Error struct {
	Op  string
	Err error

	Protocol string
	Values   interface{}
}

Error represents error related to endpoint.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap implements xerrors.Wrapper

Jump to

Keyboard shortcuts

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