web

package
v0.0.0-...-61175eb Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2016 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

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

App stands for a application which applies some http services.

func NewApp

func NewApp() *App

NewApp returns a new App instance.

func (*App) Delete

func (app *App) Delete(pattern string, handler RequestHandler) error

Delete handles the http request whose request method is "DELETE".

func (*App) Get

func (app *App) Get(pattern string, handler RequestHandler) error

Get handles the http request whose request method is "GET".

func (*App) Listen

func (app *App) Listen(port uint32) error

Listen some port and start to serve.

func (*App) Patch

func (app *App) Patch(pattern string, handler RequestHandler) error

Patch handles the http request whose request method is "PATCH".

func (*App) Post

func (app *App) Post(pattern string, handler RequestHandler) error

Post handles the http request whose request method is "POST".

func (*App) Put

func (app *App) Put(pattern string, handler RequestHandler) error

Put handles the http request whose request method is "PUT".

func (*App) RegisterPatternHandler

func (app *App) RegisterPatternHandler(handler PatternHandler)

RegisterPatternHandler registers your own PatternHandler. See pattern.go for more details.

func (*App) SetStaticDir

func (app *App) SetStaticDir(dir string)

SetStaticDir sets the static resource directory. Typically, you can put the js, css and img folder in the static resource directory.

func (*App) SetViewDir

func (app *App) SetViewDir(dir string)

SetViewDir sets the view directory. You should put all your view templates in the view directory.

func (*App) SetViewType

func (app *App) SetViewType(viewType ENUM_VIEW_TYPE) error

SetViewType sets the view type. See ENUM_VIEW_TYPE in view.go for more details.

type AppSettings

type AppSettings struct {
	// The ViewEngine you have choose.
	View ViewEngine
	// The view directory you have choose.
	ViewDir string
	// The static resource directory you have choose.
	StaticDir string
}

AppSettings records the current settings.

type ENUM_VIEW_TYPE

type ENUM_VIEW_TYPE byte

ENUM_VIEW_TYPE is a wrapper type for view-type.

const (
	// VIEW_NULL means there's no need to use view.
	VIEW_NULL ENUM_VIEW_TYPE = iota
	// VIEW_EGO is a view template that like the ejs in node.js Express framework.
	VIEW_EGO
)

type HTTPMethod

type HTTPMethod string

HTTPMethod is a wrapper type for http method string.

const (
	// HTTP_GET is the "GET" http method
	HTTP_GET HTTPMethod = "GET"
	// HTTP_POST is the "POST" http method
	HTTP_POST HTTPMethod = "POST"
	// HTTP_PUT is the "PUT" http method
	HTTP_PUT HTTPMethod = "PUT"
	// HTTP_DELETE is the "DELETE" http method
	HTTP_DELETE HTTPMethod = "DELETE"
	// HTTP_PATCH is the "PATCH" http method
	HTTP_PATCH HTTPMethod = "PATCH"
)

type HandlersMap

type HandlersMap map[HTTPMethod]map[string]RequestHandler

HandlersMap is a map that matches the request url and it's handler in a http method.

func (HandlersMap) GetHandler

func (hm HandlersMap) GetHandler(method HTTPMethod, pattern string) RequestHandler

GetHandler gets the handler matches the request method and url.

func (HandlersMap) GetPatterns

func (hm HandlersMap) GetPatterns(method HTTPMethod) []string

GetPatterns gets the pattern(url) list in a http method.

type KeyValues

type KeyValues map[string]interface{}

KeyValues is an utility that helps to make the key-value pairs more comfortably.

func NewKeyValues

func NewKeyValues(params ...interface{}) (*KeyValues, error)

NewKeyValues returns a new KeyValues instance. Example: keyValues := NewKeyValues(

"key1", 1,
"key2", "key2value",
"key3", 1.23456

)

func (*KeyValues) Get

func (vp *KeyValues) Get(k string) (interface{}, error)

Get the value by key.

func (*KeyValues) GetAsList

func (vp *KeyValues) GetAsList(k string) (*list.List, error)

GetAsList gets the value by key and convert the value to the *list.List type, it will return an error if convert failed.

func (*KeyValues) GetAsString

func (vp *KeyValues) GetAsString(k string) (string, error)

GetAsString gets the value by key and convert the value to the string type, it will return an error if convert failed.

func (*KeyValues) GetKeys

func (vp *KeyValues) GetKeys() []string

GetKeys returns the keys the KeyValues object.

func (*KeyValues) PutFloat

func (vp *KeyValues) PutFloat(k string, v float64)

PutFloat puts a key-value pair and the value's type is float64.

func (*KeyValues) PutInt

func (vp *KeyValues) PutInt(k string, v int)

PutInt puts a key-value pair and the value's type is int.

func (*KeyValues) PutList

func (vp *KeyValues) PutList(k string, v *list.List)

PutList puts a key-value pair and the value's type is *list.List.

func (*KeyValues) PutString

func (vp *KeyValues) PutString(k string, v string)

PutString puts a key-value pair and the value's type is string.

type PatternHandler

type PatternHandler interface {
	// HandlePattern will return two boolean values.
	// The first boolean value points if the pattern handled.
	// The second boolean value points if we should continue.
	HandlePattern(req *http.Request, res http.ResponseWriter, handlersMap HandlersMap, settings *AppSettings) (bool, bool)
}

PatternHandler interface helps people make their own PatterHandler to handle the special url format, such as /user/:username (it's an build-in pattern handler).

type Request

type Request struct {
	*http.Request
	Params *KeyValues
}

Request object stands for a http request.

func (*Request) GetReqArgs

func (req *Request) GetReqArgs() (url.Values, error)

GetReqArgs returns the form value if the request method is "GET" or "POST".

type RequestHandler

type RequestHandler interface {
	HandleRequest(req *Request, res *Response)
}

RequestHandler interface helps people to make their own request handler.

type Response

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

Response object stands for a http response.

func (*Response) Flush

func (res *Response) Flush() error

Flush the response.

func (*Response) Render

func (res *Response) Render(templateRelativePath string, viewParams *KeyValues) (string, error)

Render parses a view template.

func (*Response) SetHeader

func (res *Response) SetHeader(key string, value string)

SetHeader sets the response header.

func (*Response) SetHeaders

func (res *Response) SetHeaders(params ...interface{}) error

SetHeaders sets the response headers.

func (*Response) SetStatusCode

func (res *Response) SetStatusCode(code int)

SetStatusCode sets the status code of response.

func (*Response) WriteJSON

func (res *Response) WriteJSON(value interface{}) error

WriteJSON writes json data to the response cache. The response won't be send util you call the Flush() method.

func (*Response) WriteString

func (res *Response) WriteString(value string)

WriteString writes string data to the response cache. The response won't be send util you call the Flush() method.

type ViewEngine

type ViewEngine interface {
	Render(templateRelativePath string, params *KeyValues) (string, error)
}

ViewEngine interface helps people make their own view template parser.

Jump to

Keyboard shortcuts

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