chain

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 29 Imported by: 0

README


To create distributed systems in a simple, elegant and safe way.

---

Chain is a core library that tries to provide all the necessary machinery to create distributed systems in a simple, elegant and safe way.

Feature Overview

  • Optimized HTTP Router middleware
  • Realtime Publisher/Subscriber service.
  • Socket & Channels: A socket implementation that multiplexes messages over channels.
  • Crypto-related functionalities

Installation

go get github.com/nidorx/chain

Router

router.png

chain has a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go. In contrast to the default mux of Go's net/http package, this router supports variables in the routing pattern and matches against the request method. It also scales better.

  • Optimized HTTP router which smartly prioritize routes
  • Build robust and scalable RESTful APIs
  • Extensible Middleware framework
  • Handy functions to send variety of HTTP responses
  • Centralized HTTP error handling
package main

import (
	"github.com/nidorx/chain"
	"log"
	"net/http"
)

func main() {
	router := chain.New()

	// Middleware
	router.Use(func(ctx *chain.Context, next func() error) error {
		println("first middleware")
		return next()
	})

	router.Use("GET", "/*", func(ctx *chain.Context) {
		println("second middleware")
	})

	// Handler
	router.GET("/", func(ctx *chain.Context) {
		ctx.Write([]byte("Hello World!"))
	})

	// Grouping
	v1 := router.Group("/v1")
	{
		v1.GET("/users", func(ctx *chain.Context) {
			ctx.Write([]byte("[001]"))
		})
	}

	v2 := router.Group("/v2")
	{
		v2.GET("/users", func(ctx *chain.Context) {
			ctx.Write([]byte("[002]"))
		})
	}

	if err := http.ListenAndServe("localhost:8080", router); err != nil {
		log.Fatalf("ListenAndServe: %v", err)
	}
}

More about Router

PubSub

pubsub.png

Realtime Publisher/Subscriber service.

You can use the functions in this module to subscribe and broadcast messages:

package main

import (
	"fmt"
	"github.com/nidorx/chain"
	"github.com/nidorx/chain/pubsub"
	"time"
)

type MyDispatcher struct {
}

func (d *MyDispatcher) Dispatch(topic string, message any, from string) {
	println(fmt.Sprintf("New Message. Topic: %s, Content: %s", topic, message))
}

func main() {

	dispatcher := &MyDispatcher{}
	serializer := &chain.JsonSerializer{}

	pubsub.Subscribe("user:123", dispatcher)

	bytes, _ := serializer.Encode(map[string]any{
		"Event": "user_update",
		"Payload": map[string]any{
			"Id":   6,
			"Name": "Gabriel",
		},
	})
	pubsub.Broadcast("user:123", bytes)
	pubsub.Broadcast("user:123", []byte("Message 2"))

	// await
	<-time.After(time.Millisecond * 10)

	pubsub.Unsubscribe("user:123", dispatcher)

	pubsub.Broadcast("user:123", []byte("Message Ignored"))

	// await
	<-time.After(time.Millisecond * 10)
}

More about PubSub

Socket & Channels

socket.png

A socket implementation that multiplexes messages over channels.

Once connected to a socket, incoming and outgoing events are routed to channels. The incoming client data is routed to channels via transports. It is the responsibility of the socket to tie transports and channels together.

Chain ships with a JavaScript implementation that interacts with backend and can be used as reference for those interested in implementing custom clients.

Server

package main

import (
	"github.com/nidorx/chain"
	"github.com/nidorx/chain/socket"
	"log"
	"net/http"
)

func main() {
	router := chain.New()

	router.Configure("/socket", AppSocket)

	if err := http.ListenAndServe(":8080", router); err != nil {
		log.Fatalf("ListenAndServe: %v", err)
	}
}

var AppSocket = &socket.Handler{
	Channels: []*socket.Channel{
		socket.NewChannel("chat:*", chatChannel),
	},
}

func chatChannel(channel *socket.Channel) {

	channel.Join("chat:lobby", func(params any, socket *socket.Socket) (reply any, err error) {
		return
	})

	channel.HandleIn("my_event", func(event string, payload any, socket *socket.Socket) (reply any, err error) {
		reply = "Ok"

		socket.Push("other_event", map[string]any{"value": 1})
		return
	})
}

Client (javascript)

const socket = chain.Socket('/socket')
socket.connect()

const channel = socket.channel("chat:lobby", {param1: 'foo'})
channel.join()

channel.push('my_event', {name: $inputName.value})
    .on('ok', (reply) => chain.log('MyEvent', reply))


channel.on('other_event', (message) => chain.log('OtherEvent', message))

More about Socket & Channels

Crypto

Simplify and standardize the use and maintenance of symmetric cryptographic keys.

Features:

  • SecretKeyBase Solution that allows your application to have a single security key and from that it is possible to generate an infinite number of derived keys used in the most diverse features of your project.
  • Keyring Allows you to enable key rotation, allowing encryption processes to be performed with a new key and data encrypted with old keys can still be decrypted.
  • KeyGenerator: It can be used to derive a number of keys for various purposes from a given secret. This lets applications have a single secure secret, but avoid reusing that key in multiple incompatible contexts.
  • MessageVerifier: makes it easy to generate and verify messages which are signed to prevent tampering.
  • MessageEncryptor is a simple way to encrypt values which get stored somewhere you don't trust.

More about Crypto

Documentation

Overview

Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/binding.go

Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/form.go

Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/form_mapping.go

Copyright 2019 Gin Core Team. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/multipart_form_mapping.go

Copyright 2022 Gin Core Team. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/header.go

Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/json.go

Copyright 2018 Gin Core Team. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/uri.go

Copyright 2017 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/query.go

Copyright 2017 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/default_validator.go

Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/xml.go

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrConvertMapStringSlice can not convert to map[string][]string
	ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings")

	// ErrConvertToMapString can not convert to map[string]string
	ErrConvertToMapString = errors.New("can not convert to map of strings")
)
View Source
var (
	// ErrMultiFileHeader multipart.FileHeader invalid
	ErrMultiFileHeader = errors.New("unsupported field type for multipart.FileHeader")

	// ErrMultiFileHeaderLenInvalid array for []*multipart.FileHeader len invalid
	ErrMultiFileHeaderLenInvalid = errors.New("unsupported len of array for []*multipart.FileHeader")
)
View Source
var (
	ErrInvalidHandler = errors.New("invalid handler")
	ErrInvalidMethod  = errors.New("method must not be empty")
	ErrInvalidPath    = errors.New("path must begin with '/'")
	ErrHandlerIsNil   = errors.New("handle must not be nil")
)
View Source
var BodyBytesKey = bodyBytesKey{}

BodyBytesKey indicates a default body bytes key.

View Source
var ContextKey = chainContextKey{}

ContextKey is the request context key under which URL params are stored.

View Source
var EnableDecoderDisallowUnknownFields = false

EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

View Source
var EnableDecoderUseNumber = false

EnableDecoderUseNumber is used to call the UseNumber method on the JSON Decoder instance. UseNumber causes the Decoder to unmarshal a number into an any as a Number instead of as a float64.

View Source
var ErrAlreadySent = errors.New("the response was already sent")

ErrAlreadySent Error raised when trying to modify or send an already sent response

View Source
var UnixEpoch = time.Unix(0, 0)

Functions

func Crypto

func Crypto() crypto.Crypto

Crypto get the reference to a structure that has shortcut to all encryption related functions

func HashCrc32

func HashCrc32(content []byte) string

func HashMD5

func HashMD5(text string) string

HashMD5 computing the MD5 checksum of strings

func HashXxh64 added in v1.0.3

func HashXxh64(content []byte) string

Xxh64 return a base64-encoded checksum of a resource using Xxh64 algorithm

Encoded using Base64 URLSafe

func MapFormWithTag added in v1.0.3

func MapFormWithTag(ptr any, form map[string][]string, tag string) error

func NewKeyring

func NewKeyring(salt string, iterations int, length int, digest string) *crypto.Keyring

NewKeyring starts a Keyring that will be updated whenever SecretKeySync() is invoked

  • `salt` - a salt used with SecretKeyBase to generate a secret
  • `iterations` - defaults to 1000 (increase to at least 2^16 if used for passwords)
  • `length` - a length in octets for the derived key. Defaults to 32
  • `digest` - a hmac function to use as the pseudo-random function. Defaults to `sha256`

func NewUID

func NewUID() (uid string)

func NodeName

func NodeName() string

func SecretKeyBase

func SecretKeyBase() string

SecretKeyBase A secret key used to verify and encrypt data.

This data must be never used directly, always use chain.Crypto().KeyGenerate() to derive keys from it

func SecretKeySync

func SecretKeySync(sync SecretKeySyncFunc) (cancel func())

SecretKeySync is used to transmit SecretKeyBase changes

func SecretKeys

func SecretKeys() []string

SecretKeys gets the list of all SecretKeyBase that have been defined. Can be used in key rotation algorithms

The LAST item in the list is the most recent key (primary key)

func SetNodeName

func SetNodeName(name string)

func SetSecretKeyBase

func SetSecretKeyBase(secret string) error

SetSecretKeyBase see SecretKeyBase()

Types

type BindUnmarshaler added in v1.0.3

type BindUnmarshaler interface {
	// UnmarshalParam decodes and assigns a value from an form or query param.
	UnmarshalParam(param string) error
}

BindUnmarshaler is the interface used to wrap the UnmarshalParam method.

type Binding added in v1.0.3

type Binding interface {
	Bind(*Context, any) error
}

Binding describes the interface which needs to be implemented for binding the data present in the request such as JSON request body, query parameters or the form POST.

var (
	BindingXML           Binding = xmlBinding{}            // xml
	BindingJSON          Binding = jsonBinding{}           // json
	BindingPath          Binding = pathBinding{}           // path
	BindingForm          Binding = formBinding{}           // form
	BindingFormPost      Binding = formPostBinding{}       // form
	BindingFormMultipart Binding = formMultipartBinding{}  // form
	BindingQuery         Binding = queryBinding{}          // query
	BindingHeader        Binding = headerBinding{}         // header
	BindingDefault       Binding = &BindingDefaultStruct{} // query, json, xml, form
)

These implement the Binding interface and can be used to bind the data present in the request to struct instances.

type BindingDefaultStruct added in v1.0.3

type BindingDefaultStruct struct {
	BindHeader bool
}

func (*BindingDefaultStruct) Bind added in v1.0.3

func (s *BindingDefaultStruct) Bind(ctx *Context, obj any) error

type Context

type Context struct {
	Route   *RouteInfo
	Writer  http.ResponseWriter
	Request *http.Request
	Crypto  *cryptoImpl
	// contains filtered or unexported fields
}

Context represents a request & response Context.

func GetContext

func GetContext(ctx context.Context) *Context

GetContext pulls the URL parameters from a request context, or returns nil if none are present.

func (*Context) AddHeader

func (ctx *Context) AddHeader(key, value string)

AddHeader adds the key, value pair to the header. It appends to any existing values associated with key. The key is case insensitive; it is canonicalized by CanonicalHeaderKey.

func (*Context) AfterSend

func (ctx *Context) AfterSend(callback func()) error

func (*Context) BadRequest added in v1.0.3

func (ctx *Context) BadRequest()

BadRequest replies to the request with an HTTP 400 bad request error.

func (*Context) BeforeSend

func (ctx *Context) BeforeSend(callback func()) error

BeforeSend Registers a callback to be invoked before the response is sent.

Callbacks are invoked in the reverse order they are defined (callbacks defined first are invoked last).

func (*Context) Bind added in v1.0.3

func (ctx *Context) Bind(obj any) error

Bind checks the Method and Content-Type to select a binding engine automatically, Depending on the "Content-Type" header different bindings are used, for example:

"application/json" --> JSON binding
"application/xml"  --> XML binding

It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.

func (*Context) BindForm added in v1.0.3

func (ctx *Context) BindForm(obj any) error

BindForm is a shortcut for c.MustBindWith(obj, BindingForm).

func (*Context) BindFormMultipart added in v1.0.3

func (ctx *Context) BindFormMultipart(obj any) error

BindFormMultipart is a shortcut for c.MustBindWith(obj, BindingFormMultipart).

func (*Context) BindFormPost added in v1.0.3

func (ctx *Context) BindFormPost(obj any) error

BindFormPost is a shortcut for c.MustBindWith(obj, BindingFormPost).

func (*Context) BindHeader added in v1.0.3

func (ctx *Context) BindHeader(obj any) error

BindHeader is a shortcut for c.MustBindWith(obj, BindingHeader).

func (*Context) BindJSON added in v1.0.3

func (ctx *Context) BindJSON(obj any) error

BindJSON is a shortcut for c.MustBindWith(obj, BindingJSON).

func (*Context) BindPath added in v1.0.3

func (ctx *Context) BindPath(obj any) error

BindPath is a shortcut for c.MustBindWith(obj, BindingPath).

func (*Context) BindQuery added in v1.0.3

func (ctx *Context) BindQuery(obj any) error

BindQuery is a shortcut for c.MustBindWith(obj, BindingQuery).

func (*Context) BindXML added in v1.0.3

func (ctx *Context) BindXML(obj any) error

BindXML is a shortcut for c.MustBindWith(obj, binding.BindXML).

func (*Context) BodyBytes added in v1.0.3

func (ctx *Context) BodyBytes() (body []byte, err error)

BodyBytes get body as array of bytes

func (*Context) Child added in v1.0.4

func (ctx *Context) Child() *Context

func (*Context) ContentType added in v1.0.3

func (ctx *Context) ContentType(ctype string)

ContentType set the response content type

func (*Context) Created added in v1.0.3

func (ctx *Context) Created()

Created sends an HTTP response header with the 201 Created status code.

func (*Context) Destroy added in v1.0.4

func (ctx *Context) Destroy()

func (*Context) Error

func (ctx *Context) Error(error string, code int)

Error replies to the request with the specified error message and HTTP code. It does not otherwise end the request; the caller should ensure no further writes are done to w. The error message should be plain text.

func (*Context) Forbidden added in v1.0.3

func (ctx *Context) Forbidden()

Unauthorized replies to the request with an HTTP 403 Forbidden error.

func (*Context) Get

func (ctx *Context) Get(key any) (any, bool)

Get obtém um valor compartilhado no contexto de execução da requisição

func (*Context) GetContentType added in v1.0.3

func (ctx *Context) GetContentType() string

GetContentType returns the Content-Type header of the request.

func (*Context) GetCookie

func (ctx *Context) GetCookie(name string) *http.Cookie

GetCookie returns the named cookie provided in the request or nil if not found. If multiple cookies match the given name, only one cookie will be returned.

func (*Context) GetHeader

func (ctx *Context) GetHeader(key string) string

GetHeader gets the first value associated with the given key. If there are no values associated with the key, GetHeader returns "". It is case insensitive; textproto.CanonicalMIMEHeaderKey is used to canonicalize the provided key. Get assumes that all keys are stored in canonical form. To use non-canonical keys, access the map directly.

func (*Context) GetParam

func (ctx *Context) GetParam(name string) string

GetParam returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

func (*Context) GetParamByIndex

func (ctx *Context) GetParamByIndex(index int) string

GetParamByIndex get one parameter per index

func (*Context) GetStatus added in v1.0.4

func (ctx *Context) GetStatus() int

func (*Context) Header

func (ctx *Context) Header() http.Header

Header returns the header map that will be sent by WriteHeader. The Header map also is the mechanism with which Handlers can set HTTP trailers.

Changing the header map after a call to WriteHeader (or Write) has no effect unless the HTTP status code was of the 1xx class or the modified headers are trailers.

There are two ways to set Trailers. The preferred way is to predeclare in the headers which trailers you will later send by setting the "Trailer" header to the names of the trailer keys which will come later. In this case, those keys of the Header map are treated as if they were trailers. See the example. The second way, for trailer keys not known to the Handle until after the first Write, is to prefix the Header map keys with the TrailerPrefix constant value. See TrailerPrefix.

To suppress automatic response headers (such as "Date"), set their value to nil.

func (*Context) Host added in v1.0.3

func (ctx *Context) Host() string

Host host as string

func (*Context) InternalServerError added in v1.0.3

func (ctx *Context) InternalServerError()

InternalServerError replies to the request with an HTTP 500 Internal Server Error error.

func (*Context) Ip added in v1.0.3

func (ctx *Context) Ip() string

Host ip as string

func (*Context) Json added in v1.0.3

func (ctx *Context) Json(v any)

Json encode and writes the data to the connection as part of an HTTP reply.

The Content-Length and Content-Type headers are added automatically.

func (*Context) Method added in v1.0.3

func (ctx *Context) Method() string

Method specifies the HTTP method (GET, POST, PUT, etc.).

func (*Context) MustBindWith added in v1.0.3

func (ctx *Context) MustBindWith(obj any, b Binding) error

MustBindWith binds the passed struct pointer using the specified binding engine. It will abort the request with HTTP 400 if any error occurs. See the binding package.

func (*Context) NewUID

func (ctx *Context) NewUID() (uid string)

NewUID get a new KSUID.

KSUID is for K-Sortable Unique IDentifier. It is a kind of globally unique identifier similar to a RFC 4122 UUID, built from the ground-up to be "naturally" sorted by generation timestamp without any special type-aware logic.

See: https://github.com/segmentio/ksuid

func (*Context) NoContent added in v1.0.3

func (ctx *Context) NoContent()

Created sends an HTTP response header with the 204 No Content status code.

func (*Context) NotFound

func (ctx *Context) NotFound()

NotFound replies to the request with an HTTP 404 not found error.

func (*Context) NotImplemented added in v1.0.3

func (ctx *Context) NotImplemented()

NotImplemented replies to the request with an HTTP 501 Not Implemented error.

func (*Context) OK added in v1.0.3

func (ctx *Context) OK()

Created sends an HTTP response header with the 200 OK status code.

func (*Context) QueryParam added in v1.0.3

func (ctx *Context) QueryParam(name string, defaultValue ...string) string

@TODO: cache

func (*Context) QueryParamInt added in v1.0.3

func (ctx *Context) QueryParamInt(name string, defaultValue ...int) int

func (*Context) Redirect

func (ctx *Context) Redirect(url string, code int)

Redirect replies to the request with a redirect to url, which may be a path relative to the request path.

The provided code should be in the 3xx range and is usually StatusMovedPermanently, StatusFound or StatusSeeOther.

If the Content-Type header has not been set, Redirect sets it to "text/html; charset=utf-8" and writes a small HTML body.

Setting the Content-Type header to any value, including nil, disables that behavior.

func (*Context) RemoveCookie added in v1.0.3

func (ctx *Context) RemoveCookie(name string)

RemoveCookie delete a cookie by name

func (*Context) Router

func (ctx *Context) Router() *Router

Router get current router reference

func (*Context) ServeContent added in v1.0.3

func (ctx *Context) ServeContent(content []byte, name string, modtime time.Time)

ServeContent replies to the request using the content in the provided. The main benefit of ServeContent over io.Copy is that it handles Range requests properly, sets the MIME type, and handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since, and If-Range requests.

If the response's Content-Type header is not set, ServeContent first tries to deduce the type from name's file extension and, if that fails, falls back to reading the first block of the content and passing it to DetectContentType. The name is otherwise unused; in particular it can be empty and is never sent in the response.

If modtime is not the zero time or Unix epoch, ServeContent includes it in a Last-Modified header in the response. If the request includes an If-Modified-Since header, ServeContent uses modtime to decide whether the content needs to be sent at all.

The content's Seek method must work: ServeContent uses a seek to the end of the content to determine its size.

If the caller has set w's ETag header formatted per RFC 7232, section 2.3, ServeContent uses it to handle requests using If-Match, If-None-Match, or If-Range.

func (*Context) ServiceUnavailable added in v1.0.3

func (ctx *Context) ServiceUnavailable()

ServiceUnavailable replies to the request with an HTTP 503 Service Unavailable error.

func (*Context) Set

func (ctx *Context) Set(key any, value any)

Set define um valor compartilhado no contexto de execução da requisição

func (*Context) SetCookie

func (ctx *Context) SetCookie(cookie *http.Cookie)

SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.

func (*Context) SetHeader

func (ctx *Context) SetHeader(key, value string)

SetHeader sets the header entries associated with key to the single element value. It replaces any existing values associated with key. The key is case insensitive; it is canonicalized by textproto.CanonicalMIMEHeaderKey. To use non-canonical keys, assign to the map directly.

func (*Context) ShouldBind added in v1.0.3

func (ctx *Context) ShouldBind(obj any) error

ShouldBind checks the Method and Content-Type to select a binding engine automatically, Depending on the "Content-Type" header different bindings are used, for example:

"application/json" --> JSON binding
"application/xml"  --> XML binding

It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. Like c.Bind() but this method does not set the response status code to 400 or abort if input is not valid.

func (*Context) ShouldBindForm added in v1.0.3

func (ctx *Context) ShouldBindForm(obj any) error

ShouldBindForm is a shortcut for c.ShouldBindWith(obj, BindingForm).

func (*Context) ShouldBindFormMultipart added in v1.0.3

func (ctx *Context) ShouldBindFormMultipart(obj any) error

ShouldBindFormMultipart is a shortcut for c.ShouldBindWith(obj, BindingFormMultipart).

func (*Context) ShouldBindFormPost added in v1.0.3

func (ctx *Context) ShouldBindFormPost(obj any) error

ShouldBindFormPost is a shortcut for c.ShouldBindWith(obj, BindingFormPost).

func (*Context) ShouldBindHeader added in v1.0.3

func (ctx *Context) ShouldBindHeader(obj any) error

ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, BindingHeader).

func (*Context) ShouldBindJSON added in v1.0.3

func (c *Context) ShouldBindJSON(obj any) error

ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, BindingJSON).

func (*Context) ShouldBindPath added in v1.0.3

func (ctx *Context) ShouldBindPath(obj any) error

ShouldBindPath is a shortcut for c.ShouldBindWith(obj, BindingPath).

func (*Context) ShouldBindQuery added in v1.0.3

func (ctx *Context) ShouldBindQuery(obj any) error

ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, BindingQuery).

func (*Context) ShouldBindWith added in v1.0.3

func (ctx *Context) ShouldBindWith(obj any, b Binding) error

ShouldBindWith binds the passed struct pointer using the specified binding engine. See the binding package.

func (*Context) ShouldBindXML added in v1.0.3

func (c *Context) ShouldBindXML(obj any) error

ShouldBindXML is a shortcut for c.ShouldBindWith(obj, BindingXML).

func (*Context) Status added in v1.0.3

func (ctx *Context) Status(statusCode int)

WriteHeader sends an HTTP response header with the provided status code.

func (*Context) TooManyRequests added in v1.0.3

func (ctx *Context) TooManyRequests()

TooManyRequests replies to the request with an HTTP 429 Too Many Requests error.

func (*Context) URL added in v1.0.3

func (ctx *Context) URL() *url.URL

URL request url

func (*Context) Unauthorized added in v1.0.3

func (ctx *Context) Unauthorized()

Unauthorized replies to the request with an HTTP 401 Unauthorized error.

func (*Context) UserAgent added in v1.0.3

func (ctx *Context) UserAgent() string

UserAgent returns the client's User-Agent, if sent in the request.

func (*Context) WithParams

func (ctx *Context) WithParams(names []string, values []string) *Context

func (*Context) Write

func (ctx *Context) Write(data []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply.

If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType. Additionally, if the total size of all written data is under a few KB and there are no Flush calls, the Content-Length header is added automatically.

Depending on the HTTP protocol version and the client, calling Write or WriteHeader may prevent future reads on the Request.Body. For HTTP/1.x requests, handlers should read any needed request body data before writing the response. Once the headers have been flushed (due to either an explicit Flusher.Flush call or writing enough data to trigger a flush), the request body may be unavailable. For HTTP/2 requests, the Go HTTP server permits handlers to continue to read the request body while concurrently writing the response. However, such behavior may not be supported by all HTTP/2 clients. Handlers should read before writing if possible to maximize compatibility.

func (*Context) WriteCalled added in v1.0.3

func (ctx *Context) WriteCalled() bool

WriteCalled returns true if the ctx.Writer.Write method was called

func (*Context) WriteHeader

func (ctx *Context) WriteHeader(statusCode int)

WriteHeader sends an HTTP response header with the provided status code.

If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes or 1xx informational responses.

The provided code must be a valid HTTP 1xx-5xx status code. Any number of 1xx headers may be written, followed by at most one 2xx-5xx header. 1xx headers are sent immediately, but 2xx-5xx headers may be buffered. Use the Flusher interface to send buffered data. The header map is cleared when 2xx-5xx headers are sent, but not with 1xx headers.

The server will automatically send a 100 (Continue) header on the first read from the request body if the request has an "Expect: 100-continue" header.

func (*Context) WriteHeaderCalled added in v1.0.3

func (ctx *Context) WriteHeaderCalled() bool

WriteCalled returns true if the ctx.Writer.WriteHeader method was called

func (*Context) WriteStarted added in v1.0.3

func (ctx *Context) WriteStarted() bool

WriteStarted returns true if the ctx.Writer.Write or ctx.Writer.WriteHeader method was called

type Group

type Group interface {
	GET(route string, handle any) error
	HEAD(route string, handle any) error
	OPTIONS(route string, handle any) error
	POST(route string, handle any) error
	PUT(route string, handle any) error
	PATCH(route string, handle any) error
	DELETE(route string, handle any) error
	Use(args ...any) Group
	Group(route string) Group
	Handle(method string, route string, handle any) error
	Configure(route string, configurator RouteConfigurator)
}

type Handle

type Handle func(*Context) error

func Handler added in v1.0.3

func Handler(handle any) (h Handle, err error)

Handle registers a new Route for the given method and path.

type JsonSerializer

type JsonSerializer struct {
}

func (*JsonSerializer) Decode

func (s *JsonSerializer) Decode(data []byte, v any) (any, error)

func (*JsonSerializer) Encode

func (s *JsonSerializer) Encode(v any) ([]byte, error)

type Middleware

type Middleware struct {
	Path   *RouteInfo
	Handle func(ctx *Context, next func() error) error
}

type MiddlewareHandler

type MiddlewareHandler interface {
	Handle(ctx *Context, next func() error) error
}

type MiddlewareWithInitHandler

type MiddlewareWithInitHandler interface {
	Init(method string, path string, router *Router)
	Handle(ctx *Context, next func() error) error
}

type Registry

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

Registry is an algorithm-independent framework for recording routes. This division allows us to explore different algorithms without breaking the contract.

type ResponseWriterSpy

type ResponseWriterSpy struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*ResponseWriterSpy) Status added in v1.0.4

func (w *ResponseWriterSpy) Status() int

func (*ResponseWriterSpy) Write

func (w *ResponseWriterSpy) Write(b []byte) (int, error)

func (*ResponseWriterSpy) WriteHeader

func (w *ResponseWriterSpy) WriteHeader(status int)

type Route

type Route struct {
	Info        *RouteInfo
	Handle      Handle
	Middlewares []*Middleware
	// contains filtered or unexported fields
}

Route control of a registered route

func (*Route) Dispatch

func (r *Route) Dispatch(ctx *Context) error

Dispatch ctx into this route

type RouteConfigurator

type RouteConfigurator interface {
	Configure(router *Router, path string)
}

type RouteInfo added in v1.0.4

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

RouteInfo represents all useful information about a dynamic path (used by handlers)

func ParseRouteInfo added in v1.0.4

func ParseRouteInfo(pathOrig string) *RouteInfo

ParseRouteInfo obtém informações sobre um path dinamico.

func (*RouteInfo) Details added in v1.0.4

func (d *RouteInfo) Details() (segments []string, params []string, indexes []int)

func (*RouteInfo) FastMatch added in v1.0.4

func (d *RouteInfo) FastMatch(ctx *Context) bool

func (*RouteInfo) HasParameter added in v1.0.4

func (d *RouteInfo) HasParameter() bool

func (*RouteInfo) HasStatic added in v1.0.4

func (d *RouteInfo) HasStatic() bool

func (*RouteInfo) HasWildcard added in v1.0.4

func (d *RouteInfo) HasWildcard() bool

func (RouteInfo) Match added in v1.0.4

func (d RouteInfo) Match(ctx *Context) (match bool, paramNames []string, paramValues []string)

Match checks if the patch is compatible, performs the extraction of parameters

func (*RouteInfo) Matches added in v1.0.4

func (d *RouteInfo) Matches(o *RouteInfo) bool

Matches checks if this path is applicable over the other. Used for registering middlewares in routes

func (*RouteInfo) Params added in v1.0.4

func (d *RouteInfo) Params() []string

func (*RouteInfo) ParamsIndex added in v1.0.4

func (d *RouteInfo) ParamsIndex() []int

func (*RouteInfo) Path added in v1.0.4

func (d *RouteInfo) Path() string

func (*RouteInfo) Pattern added in v1.0.4

func (d *RouteInfo) Pattern() string

func (*RouteInfo) Priority added in v1.0.4

func (d *RouteInfo) Priority() int

func (*RouteInfo) ReplacePath added in v1.0.4

func (d *RouteInfo) ReplacePath(ctx *Context) string

func (*RouteInfo) Segments added in v1.0.4

func (d *RouteInfo) Segments() []string

func (*RouteInfo) String added in v1.0.4

func (d *RouteInfo) String() string

type RouteStorage

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

type Router

type Router struct {
	Crypto cryptoImpl

	// ReqContext optionally specifies a function that modifies
	// the context used for the request.
	ReqContext func(*Context) context.Context

	// If enabled, the router automatically replies to OPTIONS requests.
	// Custom OPTIONS handlers take priority over automatic replies.
	HandleOPTIONS bool

	// If enabled, the router tries to fix the current request path, if no handle is registered for it.
	// First superfluous path elements like ../ or // are removed.
	// Afterwards the router does a case-insensitive lookup of the cleaned path.
	// If a handle can be found for this route, the router makes a redirection to the corrected path with status code
	// 301 for GET requests and 308 for all other request methods.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool

	// Enables automatic redirection if the current route can't be matched but a handler for the path with (without)
	// the trailing slash exists.
	// For example if /foo/ is requested but a route only exists for /foo, the client is redirected to /foo with http
	// status code 301 for GET requests and 308 for all other request methods.
	RedirectTrailingSlash bool

	// If enabled, the router checks if another method is allowed for the current route, if the current request can not
	// be routed.
	// If this is the case, the request is answered with 'Method Not Allowed' and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFoundHandler handler.
	HandleMethodNotAllowed bool

	// Function to handle panics recovered from http handlers.
	// It should be used to generate a error page and return the http error code 500 (Internal Server Error).
	// The handler can be used to keep your server from crashing because of unrecovered panics.
	PanicHandler func(http.ResponseWriter, *http.Request, any)

	// Function to handle errors recovered from http handlers and middlewares.
	// The handler can be used to do global error handling (not handled in middlewares)
	ErrorHandler func(*Context, error)

	// Configurable http.Handler function which is called when no matching route is found. If it is not set, http.NotFound is
	// used.
	NotFoundHandler http.Handler

	// An optional http.Handler function that is called on automatic OPTIONS requests.
	// The handler is only called if HandleOPTIONS is true and no OPTIONS handler for the specific path was set.
	// The "Allowed" header is set before calling the handler.
	GlobalOPTIONSHandler http.Handler

	// Configurable http.Handler function which is called when a request cannot be routed and HandleMethodNotAllowed is true.
	// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
	// The "Allow" header with allowed request methods is set before the handler is called.
	MethodNotAllowedHandler http.Handler
	// contains filtered or unexported fields
}

Router is a high-performance router.

func New

func New() *Router

func (*Router) Configure

func (r *Router) Configure(route string, configurator RouteConfigurator)

Configure allows a RouteConfigurator to perform route configurations

func (*Router) DELETE

func (r *Router) DELETE(route string, handle any) error

DELETE is a shortcut for router.handleFunc(http.MethodDelete, Route, handle)

func (*Router) GET

func (r *Router) GET(route string, handle any) error

GET is a shortcut for router.handleFunc(http.MethodGet, Route, handle)

func (*Router) Group

func (r *Router) Group(route string) Group

func (*Router) HEAD

func (r *Router) HEAD(route string, handle any) error

HEAD is a shortcut for router.handleFunc(http.MethodHead, Route, handle)

func (*Router) Handle

func (r *Router) Handle(method string, route string, handle any) error

Handle registers a new Route for the given method and path.

func (*Router) Lookup

func (r *Router) Lookup(method string, path string) (*Route, *Context)

Lookup finds the Route and parameters for the given Route and assigns them to the given Context.

func (*Router) OPTIONS

func (r *Router) OPTIONS(route string, handle any) error

OPTIONS is a shortcut for router.handleFunc(http.MethodOptions, Route, handle)

func (*Router) PATCH

func (r *Router) PATCH(route string, handle any) error

PATCH is a shortcut for router.handleFunc(http.MethodPatch, Route, handle)

func (*Router) POST

func (r *Router) POST(route string, handle any) error

POST is a shortcut for router.handleFunc(http.MethodPost, Route, handle)

func (*Router) PUT

func (r *Router) PUT(route string, handle any) error

PUT is a shortcut for router.handleFunc(http.MethodPut, Route, handle)

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP responds to the given request.

func (*Router) Use

func (r *Router) Use(args ...any) Group

Use registers a middleware routeT that will match requests with the provided prefix (which is optional and defaults to "/*").

router.Use(func(ctx *chain.Context) error {
    return ctx.NextFunc()
})

router.Use(firstMiddleware, secondMiddleware)

app.Use("/api", func(ctx *chain.Context) error {
    return ctx.NextFunc()
})

app.Use("GET", "/api", func(ctx *chain.Context) error {
    return ctx.NextFunc()
})

app.Use("GET", "/files/*filepath", func(ctx *chain.Context) error {
    println(ctx.GetParam("filepath"))
    return ctx.NextFunc()
})

type RouterGroup

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

func (*RouterGroup) Configure

func (r *RouterGroup) Configure(route string, configurator RouteConfigurator)

func (*RouterGroup) DELETE

func (r *RouterGroup) DELETE(route string, handle any) error

func (*RouterGroup) GET

func (r *RouterGroup) GET(route string, handle any) error

func (*RouterGroup) Group

func (r *RouterGroup) Group(route string) Group

func (*RouterGroup) HEAD

func (r *RouterGroup) HEAD(route string, handle any) error

func (*RouterGroup) Handle

func (r *RouterGroup) Handle(method string, route string, handle any) error

func (*RouterGroup) OPTIONS

func (r *RouterGroup) OPTIONS(route string, handle any) error

func (*RouterGroup) PATCH

func (r *RouterGroup) PATCH(route string, handle any) error

func (*RouterGroup) POST

func (r *RouterGroup) POST(route string, handle any) error

func (*RouterGroup) PUT

func (r *RouterGroup) PUT(route string, handle any) error

func (*RouterGroup) Use

func (r *RouterGroup) Use(args ...any) Group

type SecretKeySyncFunc

type SecretKeySyncFunc func(key string)

type Serializer

type Serializer interface {
	Encode(v any) ([]byte, error)
	Decode(data []byte, v any) (any, error)
}

type SliceValidationError added in v1.0.3

type SliceValidationError []error

func (SliceValidationError) Error added in v1.0.3

func (err SliceValidationError) Error() string

Error concatenates all error elements in SliceValidationError into a single string separated by \n.

type StructValidator added in v1.0.3

type StructValidator interface {
	// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
	// If the received type is a slice|array, the validation should be performed travel on every element.
	// If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned.
	// If the received type is a struct or pointer to a struct, the validation should be performed.
	// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
	// Otherwise nil must be returned.
	ValidateStruct(any) error

	// Engine returns the underlying validator engine which powers the
	// StructValidator implementation.
	Engine() any
}

StructValidator is the minimal interface which needs to be implemented in order for it to be used as the validator engine for ensuring the correctness of the request. Gin provides a default implementation for this using https://github.com/go-playground/validator/tree/v10.6.1.

var Validator StructValidator = &defaultValidator{}

Validator is the default validator which implements the StructValidator interface. It uses https://github.com/go-playground/validator/tree/v10.6.1 under the hood.

Directories

Path Synopsis
examples
middlewares

Jump to

Keyboard shortcuts

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