Documentation ¶
Index ¶
Constants ¶
const (
Version = "0.0.3"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Binder ¶
type Binder interface {
Bind(f interface{}) Controller
}
Binder is an optional but strongly recommended interface that need developer to implement on his ghost, that will speed up controller invoking.
See the benchmark for detail: https://gist.github.com/deadblue/b232340144acd20f48d38602fd628a1b#file-benchmark_test-go
A standard implementation looks like this:
func (g *YourGhost) Bind(f interface{}) ghost.Controller { if ctrl, ok := f.(func(*YourGhost, ghost.Context)(ghost.View, error)); ok { return func(ctx ghost.Context) (ghost.View, error) { return ctrl(g, ctx) } } else { return nil } }
type Context ¶
type Context interface { // Request returns the original HTTP request object. Request() *http.Request // Scheme returns request scheme(http/https). Scheme() string // Host returns request server host. Host() string // Method returns request method in upper-case. Method() string // Path returns request path. Path() string // RemoteIp returns the client IP. RemoteIp() string // PathVar return the variable value in request path. PathVar(name string) string // Header returns a value in request header with given name. Header(name string) string // HeaderArray returns all values in request header who has the given name. HeaderArray(name string) []string // Query returns the parameter value in query string who has the given name. Query(name string) string // QueryArray returns all parameter values in query string who has the given name. QueryArray(name string) []string // Cookie returns the cookie value who has the given name. Cookie(name string) string // CookieArray returns all cookie values who has the given name. CookieArray(name string) []string // Body return the request body, do not use it in individual goroutine, // because it will be closed after controller return. Body() io.Reader }
Context describes the request context.
type Controller ¶
Controller is a function to handle request.
type Shell ¶
type Shell interface { // Startup starts up the shell manually, use this when you want to // control the shell lifecycle by yourself. // Otherwise, use Run instead. Startup() error // Shutdown shuts down the shell manually, use this when you want to // control the shell lifecycle by yourself. // Otherwise, use Run instead. Shutdown() // Done returns a read-only error channel, you will get notification // from it when the shell completely shutdown, use this when you // control the shell lifecycle by yourself. // Otherwise, use Run instead. Done() <-chan error // Run automatically runs the shell, and shutdown it when receive specific // OS signals, Run will exit after the shell completely shutdown. // If no signal specified, handles SIGINT and SIGTERM as default. Run(sig ...os.Signal) error }
Shell is a lifeless object, until developer gives an interesting ghost to it.
func Born ¶
func Born(ghost interface{}) Shell
Born creates a Shell with your ghost, and will listen at default network and address: "http://127.0.0.1:8066".
type ShutdownObserver ¶ added in v0.0.2
type ShutdownObserver interface { // AfterShutdown will always be called after Shell completely shut down, even // Shell shut down with an error, developer can do finalizing works in it. // Currently, the returned error will be ignored. AfterShutdown() error }
ShutdownObserver is an optional interface for developer's ghost.
type StartupObserver ¶ added in v0.0.2
type StartupObserver interface { // BeforeStartup will be called before Shell starts up, developer can do // initializing works in it. When BeforeStartup returns an error, the shell // won't start up, and return the error. BeforeStartup() error }
StartupObserver is an optional interface for developer's ghost.
type StatusHandler ¶ added in v0.0.2
type StatusHandler interface { // OnStatus will be called when HTTP 40x and 50x error occurred. OnStatus(status int, ctx Context, err error) View }
StatusHandler is an optional interface for developer's ghost, it allows developer to customize the view when HTTP 40x and 50x error occurred.
type ViewHeaderInterceptor ¶ added in v0.0.2
type ViewHeaderInterceptor interface { // BeforeSendHeader will be called before kernel send the response headers to // client. // View can add/update/remove any headers in it. BeforeSendHeader(h http.Header) }
ViewHeaderInterceptor is an optional interface for View. When a view implements it, kernel will pass response header to the view before send to client, view can manipulate the response header here.
type ViewSizeAdviser ¶ added in v0.0.2
type ViewSizeAdviser interface { // ContentLength returns the body size of the view, it will be set in response // header as "Content-Length", DO NOT return a incorrect value which is less or // more than the body size, that may cause some strange issues. ContentLength() int64 }
ViewSizeAdviser is an optional interface for View. Developer need not implement it when the view does not have a body, or the body is one of "bytes.Buffer", "bytes.Reader", "strings.Reader".
type ViewTypeAdviser ¶ added in v0.0.2
type ViewTypeAdviser interface { // ContentType returns the content type of the view, it will be set in response // header as "Content-Type". ContentType() string }
ViewTypeAdviser is an optional interface for View. Developer need not implement it when the view does not have a body.