web

package module
v0.0.0-...-785fa05 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2018 License: MIT Imports: 20 Imported by: 16

README

web

Documentation Build Status Report Card

About

Go (golang) package which includes utility methods and helpers for creating web services.

I highly recommend checking out the web/cli package README. You can use this to super easily and conveniently create some sweet and extensible command-line web server applications!

Created by Jay Taylor and used by Gigawatt.

Requirements
  • Go version 1.6 or newer
Running the test suite
go test ./...
License

Permissive MIT license, see the LICENSE file for more information.

Documentation

Index

Constants

View Source
const (
	MimeHtml       = "text/html"
	MimeJavascript = "application/javascript" // Should only be used as a response content-type.
	MimeJson       = "application/json"
	MimePlain      = "text/plain"
	MimePostForm   = "application/x-www-form-urlencoded"
	MimeXml        = "application/xml"
	MimeXml2       = "text/xml"
	MimeYaml       = "application/x-yaml"
	MimeYaml2      = "text/yaml"
	MimeYaml3      = "text/x-yaml"
)

Mime-types.

View Source
const MaxStopChecks = 10

Variables

This section is empty.

Functions

func Bind

func Bind(req *http.Request, value interface{}) (err error)

Bind automatically deserializes the request body into the specified value.

`value` must be a pointer to the value to be deserialized.

The decoder is automatically selected based on the "Content-Type" header in the request.

Currently supported content types are:

  • JSON
  • XML
  • YAML

func DecodeJson

func DecodeJson(src io.Reader, value interface{}) error

func DecodeXml

func DecodeXml(src io.Reader, value interface{}) error

func DecodeYaml

func DecodeYaml(src io.Reader, value interface{}) error

func RespondWith

func RespondWith(w http.ResponseWriter, statusCode int, contentType string, data interface{}) (n int, err error)

RespondWith tries to detect what kind of data is being sent and serializes it when appropriate, e.g. transmitting a struct, ptr, etc.

func RespondWithHtml

func RespondWithHtml(w http.ResponseWriter, statusCode int, data interface{}) (int, error)

func RespondWithJson

func RespondWithJson(w http.ResponseWriter, statusCode int, data interface{}) (int, error)

func RespondWithText

func RespondWithText(w http.ResponseWriter, statusCode int, data interface{}) (int, error)

func RespondWithXml

func RespondWithXml(w http.ResponseWriter, statusCode int, data interface{}) (int, error)

func RespondWithYaml

func RespondWithYaml(w http.ResponseWriter, statusCode int, data interface{}) (int, error)

func StaticHandlerFunc

func StaticHandlerFunc(content []byte, statusCode int, headers map[string]string) http.HandlerFunc

StaticHandlerFunc generates a handler which always serves up the same thing regardless of the request.

Types

type AssetProvider

type AssetProvider func(name string) ([]byte, error)

AssetProvider defines the function signature for a StaticFilesMiddleware asset looker-upper.

While a default implementation of disk-based AssetProvider is provided, tools such as go-bindata can also be easily used.

func DiskBasedAssetProvider

func DiskBasedAssetProvider(basePath string) AssetProvider

type FsServer

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

FsServer is a filesystem server.

func NewFsServer

func NewFsServer(bind string, dir http.Dir) *FsServer

func (*FsServer) Addr

func (fsServer *FsServer) Addr() net.Addr

func (*FsServer) Dir

func (fsServer *FsServer) Dir() http.Dir

func (*FsServer) Start

func (fsServer *FsServer) Start() error

func (*FsServer) Stop

func (fsServer *FsServer) Stop() error

type Json

type Json map[string]interface{}

func JsonError

func JsonError(detail interface{}) Json

func JsonErrorf

func JsonErrorf(format string, args ...interface{}) Json

type Marshaller

type Marshaller func(v interface{}) ([]byte, error)

func MarshallerFor

func MarshallerFor(contentType string) (m Marshaller, err error)

type MiddlewareFunc

type MiddlewareFunc func(next http.Handler) http.Handler

MiddlewareFunc defines the function signature for middleware functions.

func StaticFilesMiddleware

func StaticFilesMiddleware(assetProvider AssetProvider) MiddlewareFunc

StaticFilesMiddleware generates a middleware function for serving static files from the specified asset provider.

type StaticHttpHandler

type StaticHttpHandler struct {
	Content    []byte
	StatusCode int
	Headers    map[string]string
}

type WebServer

type WebServer struct {
	Options WebServerOptions
	// contains filtered or unexported fields
}

func NewStaticWebServer

func NewStaticWebServer(options WebServerOptions, content []byte, statusCode int, headers map[string]string) *WebServer

NewStaticWebServer creates a WebServer which always serves the same content.

func NewWebServer

func NewWebServer(options WebServerOptions) *WebServer

NewWebServer creates a basic stoppable WebServer.

func (*WebServer) Addr

func (ws *WebServer) Addr() net.Addr

Addr exposes the listener address.

func (*WebServer) BaseUrl

func (ws *WebServer) BaseUrl() string

BaseUrl provides a working URL base path to the web server instance.

func (*WebServer) Listener

func (ws *WebServer) Listener() net.Listener

func (*WebServer) Start

func (ws *WebServer) Start() error

Start starts up the WebServer.

func (*WebServer) Stop

func (ws *WebServer) Stop() error

Stop terminates the WebServer.

type WebServerOptions

type WebServerOptions struct {
	Addr           string        // TCP address to listen on, ":http" if empty.
	Handler        http.Handler  // handler to invoke, http.DefaultServeMux if nil.
	ReadTimeout    time.Duration // maximum duration before timing out read of the request.
	WriteTimeout   time.Duration // maximum duration before timing out write of the response.
	MaxHeaderBytes int           // maximum size of request headers, net/http.DefaultMaxHeaderBytes if 0.
	TLSConfig      *tls.Config   // optional TLS config, used by ListenAndServeTLS.
	ErrorLog       *golog.Logger
}

Directories

Path Synopsis
cli

Jump to

Keyboard shortcuts

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