http_util

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2023 License: BSD-3-Clause Imports: 11 Imported by: 39

Documentation

Overview

Package http_util provides useful routines for writing web apps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddStatic

func AddStatic(mux Mux, path, content string)

AddStatic adds static content to mux. path is the path to the file; content is the file content.

func AddStaticBinary

func AddStaticBinary(mux Mux, path string, content []byte)

AddStaticBinary adds static content to mux. path is the path to the file; content is the file content.

func AddStaticFromFile

func AddStaticFromFile(mux Mux, path, localPath string) error

AddStaticFromFile adds static content to mux. path is the path to the file; localPath is the actual path of the file on the local filesystem.

func AppendParams

func AppendParams(u *url.URL, nameValues ...string) *url.URL

AppendParams returns a URL with new parameters appended. No existing parameter is replaced. u is the original URL; nameValues is parameter name, parameter value, parameter name, parameter value, etc. nameValues must have even length.

func Error

func Error(w http.ResponseWriter, status int)

Error sends the status code along with its corresponding message

func HasParam

func HasParam(values url.Values, param string) bool

HasParam returns true if values contains a particular parameter.

func NewUrl

func NewUrl(path string, nameValues ...string) *url.URL

NewUrl returns a new URL with a given path and parameters. nameValues is parameter name, parameter value, parameter name, parameter value, etc. nameValues must have even length.

func Redirect

func Redirect(w http.ResponseWriter, r *http.Request, redirectUrl string)

Redirect sends a 302 redirect

func ReportError

func ReportError(w http.ResponseWriter, message string, err error)

Repoort error reports an error. message is what user sees.

func WithParams

func WithParams(u *url.URL, nameValues ...string) *url.URL

WithParams returns a URL with new parameters. If the parameters already exist in the original URL, they are replaced. u is the original URL; nameValues is parameter name, parameter value, parameter name, parameter value, etc. nameValues must have even length.

func WriteTemplate

func WriteTemplate(w io.Writer, t *template.Template, v interface{})

WriteTemplate writes a template. v is the values for the template.

Types

type Choice

type Choice struct {
	// What the user sees in the choice dialog
	Name string
	// The parameter value attached to this choice
	Value interface{}
}

Choice represents a choice in a combo box.

type ComboBox

type ComboBox []Choice

ComboBox represents an immutable combo box of items. ComboBox implements SelectModel.

func (ComboBox) Items

func (c ComboBox) Items() []Selection

Items returns all the items in this combo box.

func (ComboBox) ToSelection

func (c ComboBox) ToSelection(s string) *Selection

func (ComboBox) ToValue

func (c ComboBox) ToValue(s string) interface{}

ToValue returns the value associated with the selected choice or nil if none selected. s is the value from the form.

type MultipartFile added in v0.6.0

type MultipartFile struct {
	FileName string
	Contents []byte
}

MultipartFile represents a file in a multipart form.

type MultipartForm added in v0.6.0

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

MultipartForm represents a multipart form

func NewMultipartForm added in v0.6.0

func NewMultipartForm(
	reader *multipart.Reader, maxSizes map[string]int) (*MultipartForm, error)

NewMultipartForm creates a new MultipartForm. reader comes from calling MultipartReader() on the http.Request valaue. maxSizes limits the sizes of files. The keys are the http request keys; the values are the maximum number of bytes to read. No value for a request key means no size limit for that key.

func (*MultipartForm) Get added in v0.6.0

func (m *MultipartForm) Get(key string) string

Get gets the value for the request key.

func (*MultipartForm) GetFile added in v0.6.0

func (m *MultipartForm) GetFile(key string) (file MultipartFile, ok bool)

GetFile gets the file for the request key.

type Mux

type Mux interface {
	Handle(pattern string, handler http.Handler)
}

Mux is the interface that wraps the Handle method.

type PageBreadCrumb struct {
	// the currennt URL
	URL *url.URL
	// The page number URL parameter name
	PageNoParam string
	// The zero based page number
	PageNo int
	// Whether or not we are at last page.
	End bool
}

PageBreadCrumb is used for displaying the page breadcrumb

func (p *PageBreadCrumb) DisplayPageNo() int

DisplayPageNo returns the 1-based page number.

func (p *PageBreadCrumb) NextPageLink() *url.URL

NextPageLink returns the URL for the next page.

func (p *PageBreadCrumb) PrevPageLink() *url.URL

PrevPageLink returns the URL for the previous page.

type SelectModel

type SelectModel interface {
	// ToSelection converts a parameter value to a selection. ToSelection may
	// return nil if value does not map to a valid selection.
	ToSelection(s string) *Selection
}

SelectModel converts a parameter value to a selection.

type Selection

type Selection struct {
	// Value is the value of the selection
	Value string
	// Name is what is displayed for the selection
	Name string
}

Selection represents a single selection from a drop down

type Selections

type Selections []Selection

Selections implements SelectModel

func (Selections) ToSelection

func (s Selections) ToSelection(str string) *Selection

type Values

type Values struct {
	url.Values
}

Values is a wrapper around url.Values providing additional methods.

func (Values) Equals

func (v Values) Equals(paramName, value string) bool

Equals returns true if the request parameter 'paramName' is equal to 'value'

func (Values) GetSelection

func (v Values) GetSelection(
	model SelectModel, name string) *Selection

GetSelection gets the current selection. name is the request parameter name. GetSelection may return nil if there is no valid selection.

Jump to

Keyboard shortcuts

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