server

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package server provides a webserver constructor.

To use this, you must also use the registry package to register handlers. That is generally done in modules by doing the following:

func init() {
  registry.Register("/hello", handler)
}

handler can be an existing http.Handler or can be made by using an

http.HandlerFunc(func(w http.ResponseWriter, r *http.Request{})

You can start the server in a few ways:

  // Start the server on the :http port (80).
  s, err := New()
  if err != nil {
    // Do something
  }
  s.ListenAndServe()  // This blocks.

  // Start on any free port.
  l, err := net.Listen("tcp", ":0") // Start listening on any free port.
	if err != nil {
    // Do something
  }
  fmt.Println("Server listening on: %s", l.Addr().String())

  s, err := New()
  if err != nil {
    // Do something
  }
  s.Serve(l)  // This blocks.

  // Start on a specific port.
  s, err := New(Addr(":2560"))
  if err != nil {
    // Do something
  }
  s.ListenAndServe()  // This blocks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(options ...Option) (*http.Server, error)

New is the constructor for an *http.Server.

func ServeDir

func ServeDir(serv *http.Server, pattern string, fileDir string)

ServeDir updates the server to serve files at fileDir path at URL pattern. So this might look like: ServeDir(server, "/img/", "images/") You should only call this before staring the server.

func ServeFile

func ServeFile(serv *http.Server, pattern string, file string)

ServeFile updates the server to serve a file at URL pattern. Example: ServeFile(serv, "/", "index.html") You should only call this before starting the server.

Types

type Option

type Option func(s *http.Server, w *wrapOptions)

Option is an optional argument to the New() constructor.

func Addr

func Addr(addr string) Option

Addr sets up the address for the server to listen on. By default this is :http.

func ErrorLog

func ErrorLog(l *log.Logger) Option

ErrorLog allows you to specify the errror log for the server. If not set, will use os.Stderr.

func LogRequests

func LogRequests() Option

LogRequests will log all of the incoming requests. This is useful for debugging only.

func TLS

func TLS(t *tls.Config) Option

TLS passes a TLS config to the server to use.

Directories

Path Synopsis
Package registry contains a registry of URL patterns to http.Handlers and a Register() method for registering a handler during init() in a module.
Package registry contains a registry of URL patterns to http.Handlers and a Register() method for registering a handler during init() in a module.

Jump to

Keyboard shortcuts

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