server

package
v0.0.0-...-8e96db4 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2015 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package server provides an integrated http server with graceful shutdown and parameterized routing capabilities for serving webapps locally on mobile.

Create an instance of the Server by calling Server.NewServer(), register your handlers using Server.Router.Handle() or Server.RouterHandleFunc() and start the server with Server.Start(). The server expects handlers to be of the contextrouter.ContextHandler type which are similar to http.Handler but additionally take a context.Context as the first parameter in ServeHTTP()

Unlike long-running servers, mobile apps should expect to be closed by the operating system at any time. Call Server.Stop() to shut down the server gracefully. When Stop() is called, the server instance closes the Done() channel on the router Context to signal Handlers and blocks upto a timeOut duration for them to finish before shutting down the server. Android documentation suggests apps should not block the UI thread for more than 100 to 200 milliseconds. Handlers that might spawn long-running functions or computation should check for Done channel closure and abandon or finish work if closed. See https://blog.golang.org/context for an illustration. Server uses github.com/tylerb/graceful package for the shutdown functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	Router *contextrouter.ContextRouter

	sync.RWMutex
	// contains filtered or unexported fields
}

Server is an integrated http server with graceful shutdown and parameterized routing capabilities

func NewServer

func NewServer() *Server

NewServer initializes and returns a new Server. Call Start() to start the server and Stop() to shut it down.

func (*Server) Start

func (s *Server) Start(addr string) (string, error)

Start creates and starts a graceful HTTP server listening on the specified address and verifies it is running. It creates a new root context to use with the server instance and will also copy any settings passed as key/value pairs in ctxValues to the context. If a server is already running, Start will call Stop() first to close it. Start will return the root url of the server (without the trailing slash) if successfully started. This could be useful if you have requested for a system chosen port

func (*Server) Stop

func (s *Server) Stop(timeOut time.Duration)

Stop closes the done channel of the root Context of the server to signal any open handlers to terminate and shuts down the server after waiting for upto the TimeOut period for any handlers to close. Stop blocks until the server closes

Jump to

Keyboard shortcuts

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