Help us shape the future of Go! Take the 2026 Go Developer Survey

vortex

package module
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2019 License: MPL-2.0 Imports: 11 Imported by: 0

README

Vortex

godoc goreportcard

Fast HTTP/s framework with middleware, routing and templating built-in.

Install

From latest module:

go mod download github.com/streamwithme/vortex@v2.4.0

From latest commit:

go get -u github.com/streamwithme/vortex

Example

server := NewServer(&Options{})

server.RegisterController(Controller{
  Route:  "/",
  Method: http.MethodGet,
  Handler: func(request *Request, response *Response) {
    response.Render(server.FindTemplateMust("index"), &Context{})
  },
})

server.Listen()

Contributing

Pull requests are encouraged.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context map[string]reflect.Value

Context is the keyed data you want to give a template.

func NewContext

func NewContext(inheritance ...Context) Context

NewContext creates a new context with optional inheritance from parent(s).

func (Context) Get

func (c Context) Get(key string) reflect.Value

Get the value for the provided key.

func (Context) Inherit

func (c Context) Inherit(context ...Context)

Inherit clones all keys to the current context.

func (Context) Set

func (c Context) Set(key string, value interface{})

Set a key and value in the context.

type Controller

type Controller struct {
	Route   string // URI
	Method  string
	Handler func(*Request, *Response)
}

Controller -

type CookieJar

type CookieJar []*http.Cookie

CookieJar is an abstraction of the []*http.Cookie slice.

func (CookieJar) Domain

func (c CookieJar) Domain(domain string) []*http.Cookie

Domain returns cookies who belong to the given domain.

func (CookieJar) Name

func (c CookieJar) Name(name string) (*http.Cookie, error)

Name returns a cookie by the given name.

type EntrySet

type EntrySet map[string]string

EntrySet is a map of keys and their string values.

func (EntrySet) Get

func (e EntrySet) Get(key string) string

Get returns a value for a given key.

func (EntrySet) Has

func (e EntrySet) Has(key string) bool

Has returns whether or not a key exists.

func (EntrySet) Set

func (e EntrySet) Set(key string, value string)

Set assigns a new value to a given key.

type Handler

type Handler func(*Request, *Response)

Handler -

type Options

type Options struct {
	Host               string   `json:"host"`              // optional, defaults to localhost
	Port               int      `json:"port"`              // optional, defaults to 80
	Directory          string   `json:"directory"`         // optional, relative to current working directory
	FriendlyExtensions []string `json:"friendlyURLs"`      // optional, remove extension requirement in url
	TemplateExtensions []string `json:"templateExtension"` // optional, which extension to associate with templates (.tmp is default)
	Certificate        string   `json:"certificate"`       // optional
	Key                string   `json:"key"`               // optional
}

Options are configurable server settings. All are optional and have default values.

func (*Options) HostWithPort

func (o *Options) HostWithPort() string

HostWithPort -

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

Request is an abstraction of the http.Request.

func (*Request) JSON

func (r *Request) JSON(pointer interface{}) error

JSON decodes the JSON value (if any) of the request body.

func (*Request) Params

func (r *Request) Params() EntrySet

Params returns named route parameters as a map.

func (*Request) Secure

func (r *Request) Secure() bool

Secure returns whether or not the protocol is https.

func (*Request) Server

func (r *Request) Server() *Server

Server returns the server which received the request.

type Response

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

Response is an abstraction of the http.ResponseWriter.

func (*Response) Bytes

func (r *Response) Bytes(body []byte) error

Bytes responds to the request with raw bytes.

func (*Response) Cookie

func (r *Response) Cookie(cookie http.Cookie)

Cookie adds the provided cookie to the session.

func (*Response) DestroyCookie

func (r *Response) DestroyCookie(cookie http.Cookie)

DestroyCookie removes the provided cookie from the session.

func (*Response) End

func (r *Response) End(statusCode int)

End completes the response with a given status code.

func (*Response) Ended

func (r *Response) Ended() bool

Ended returns whether or not the response has ended.

func (*Response) File

func (r *Response) File(path string) error

File responds with a resource relative to the server directory. An extension is required to get the MIME type.

func (*Response) Header

func (r *Response) Header() http.Header

Header returns the header of the response.

func (*Response) JSON

func (r *Response) JSON(body interface{}) error

JSON responds to the request with a struct.

func (*Response) Locals

func (r *Response) Locals() EntrySet

Locals is a key/value store of variables scoped to this response.

func (*Response) Redirect

func (r *Response) Redirect(url string)

Redirect completes the response with a redirection.

func (*Response) Render

func (r *Response) Render(template *template.Template, context interface{}) error

Render responds to the request with a template.

func (*Response) Response

func (r *Response) Response() http.ResponseWriter

Response returns the original http.ResponseWriter of the request.

func (*Response) Server

func (r *Response) Server() *Server

Server returns the server which will send the response.

func (*Response) String

func (r *Response) String(body string) error

String responds to the request with a string body.

type Router

type Router func(*Server, *Request) (Handler, error)

Router is a function that maps requests to a handler.

func (Router) Must

func (r Router) Must(server *Server, request *Request) Handler

Must return a handler, regardless of errors.

type Server

type Server struct {
	*Options
	Middleware  []Handler
	Controllers []Controller
	Template    *template.Template
}

Server -

func NewServer

func NewServer(options *Options) *Server

NewServer returns a new vortex server.

func (*Server) FindTemplate

func (s *Server) FindTemplate(name string) (*template.Template, error)

FindTemplate returns a defined template and errors if not found.

func (*Server) FindTemplateMust

func (s *Server) FindTemplateMust(name string) *template.Template

FindTemplateMust returns a defined template and panics if not found.

func (*Server) Listen

func (s *Server) Listen()

Listen starts the HTTP/s server and blocks.

func (*Server) RegisterController

func (s *Server) RegisterController(controller Controller)

RegisterController maps a path to a handler.

func (*Server) RegisterMiddleware

func (s *Server) RegisterMiddleware(handler ...Handler)

RegisterMiddleware adds a new middleware to the server. The order in which middleware is added determines the execution order. Middleware will gets called before routing takes place.

func (*Server) Resource

func (s *Server) Resource(path string) ([]byte, error)

Resource fetches the raw bytes from a provided resource path. The path will be translated relative to specified the server directory.

func (*Server) ResourcePath

func (s *Server) ResourcePath(path string) string

ResourcePath fetches the relative to the requested file.

Jump to

Keyboard shortcuts

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