Documentation ¶
Overview ¶
Package ghost is a simple HTTP server framework.
Index ¶
Constants ¶
const ( DefaultNetwork = "tcp" DefaultAddress = "127.0.0.1:9057" )
const (
Version = "0.1.2"
)
Variables ¶
var (
ErrNotFound = errors.New("not found")
)
Functions ¶
func Implant ¶ added in v0.1.2
Implant implants other ghosts into shell, and use "root" as prefix. "root" should not be empty and should start with "/".
NOTE: This function is not goroutine-safety, DO NOT call it in multiple goroutines.
Example:
shell := Born(&MasterGhost{}) Implant(shell, &FooGhost{}, "/foo") Implant(shell, &BarGhost{}, "/bar") shell.Run()
Q: Why is Implant not a method on Shell? A: Because Go DOES NOT allow using type parameter in method :(
Types ¶
type Context ¶
type Context interface { // Request returns the original HTTP request object. Request() *http.Request // Method returns request method. Method() string // Path returns request path. Path() string // Body returns a reader for request body. Body() io.Reader // Header returns request header with specific name. Header(name string) (string, bool) // Scheme returns request scheme(http/https). Scheme() string // Host returns request server host. Host() string // RemoteIp returns the client IP. RemoteIp() string // PathVar return the variable value in request path. PathVar(name string) string }
Context wraps HTTP request, and provides some helpful methods.
type ErrorHandler ¶ added in v0.1.2
type ErrorHandler interface { // OnError will be called when HTTP 40x and 50x error occurred. OnError(ctx Context, err error) View }
ErrorHandler is an optional interface for your ghost, which allows developer to customize the view when HTTP 40x and 50x error occurred.
type Shell ¶
type Shell interface { // Startup starts up the shell manually, use this when you want to manage // shell's lifecycle by yourself. Otherwise, just use `Run`. Startup() error // Shutdown shuts down the shell manually, use this when you want to manage // shell's lifecycle by yourself. Otherwise, just use Run. Shutdown() // Done returns a read-only error channel, you will get error events from it // when the shell shut down, use this when you manage shell's lifecycle by // yourself. Otherwise, just use Run. Done() <-chan error // Run starts up the shell, and wait for it normally shut down or exit with // error. // Run will shut down shell when receives specific OS signals. If no signal // set, it will handle SIGINT and SIGTERM as default. Run(sig ...os.Signal) error }
Shell is the shell of your ghost, it covers the basic reactions what an HTTP server should do, and dispatches requests to your ghost.
You can use Shell in two ways:
1. Simply run it:
// Create a shell from your ghost. shell := ghost.Born(&YourGhost{}) // Just run the shell, Run will return when shell completely shut down. if err := shell.Run(); err != nil { panic(err) }
2. Manage its lifecycle by yourself:
// Create a shell from your ghost. shell := ghost.Born(&YourGhost{}) // Start up the shell. if err := shell.Startup(); err != nil { panic(err) } // Waiting for shell shut down. for running := true; running; { select { case <- someEventArrived: // Shut down the shell. shell.Shutdown() case err := <- shell.Done(): // The shell completely shut down. if err != nil { // Shut down with error } else { // Normally shut down. } // Exit the for-loop. running = false; } }
type ShutdownObserver ¶ added in v0.0.2
type ShutdownObserver interface { // AfterShutdown will always be called after Shell completely shut down, even // Shell shuts 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 your ghost.
type StartupObserver ¶ added in v0.0.2
type StartupObserver interface { // BeforeStartup will be called before Shell starts up, you can do some // initializing jos here. When BeforeStartup returns an error, the shell // won't start up, and return the error. BeforeStartup() error }
StartupObserver allows developer be aware when the shell startup, developer can do initializing jobs at that time.
type View ¶
type View interface { // Status returns response code. Status() int // Body returns an io.Reader for reading response body from, it will be // auto-closed after read if it implements io.Closer. Body() io.Reader }
View describes the response.
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.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
Package view provides most common-use ghost.View implementations for developers.
|
Package view provides most common-use ghost.View implementations for developers. |