httpserver

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2021 License: BSD-3-Clause Imports: 18 Imported by: 1

README

httpserver

HTTP Server in Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadTemplate

func LoadTemplate(path string) (string, error)

func RenderHTML

func RenderHTML(tmplName string, tmpl string, data interface{}, funcMap ...template.FuncMap) (template.HTML, error)

RenderHTML render template with given data into string. @tmplName: template name if a template is wrapped inside {{ define "tmplName" }}, otherwise empty string. @tmpl: template content in form of string loaded from template file. @data: data to be embedded into html template, preferably in form of map[string]interface{}. @funcMap: golang template FuncMap.

func RenderMultiHTML

func RenderMultiHTML(mainTmplName string, tmplNameToTmpl map[string]string, data interface{}, funcMap ...template.FuncMap) (template.HTML, error)

func Response

func Response(w http.ResponseWriter, statusCode int, body []byte)

Response response by writing the body to http.ResponseWriter. Call at the end line of your handler.

func ResponseHTML

func ResponseHTML(w http.ResponseWriter, tmplName string, tmpl string, data interface{},
	funcMap ...template.FuncMap) error

ResponseHTML render and return html with given data. @tmplName: template name if a template is wrapped inside {{ define "tmplName" }}, otherwise empty string. @tmpl: template content in form of string loaded from template file. @data: data to be embedded into html template, preferably in form of map[string]interface{}. @funcMap: golang template FuncMap.

func ResponseJSON

func ResponseJSON(w http.ResponseWriter, statusCode int, body interface{}) error

ResponseJSON response by writing body with json encoder into http.ResponseWriter. Body must be either struct or map[string]interface{}. Otherwise would result in incorrect parsing at client side. If you have []byte as response body, then use Response function instead. Call at the end line of your handler.

func ResponseMultiHTML

func ResponseMultiHTML(w http.ResponseWriter, mainTmplName string, tmplNameToTmpl map[string]string, data interface{}, funcMap ...template.FuncMap) error

func ResponseString

func ResponseString(w http.ResponseWriter, statusCode int, body interface{})

ResponseString response in form of string whatever passed into body param. Call at the end line of your handler.

func ResponseXML added in v1.1.0

func ResponseXML(w http.ResponseWriter, statusCode int, body interface{}) error

ResponseXML response by writing body with xml encoder into http.ResponseWriter. Body must be either struct or map[string]interface{}. Otherwise would result in incorrect parsing at client side. If you have []byte as response body, then use Response function instead. Call at the end line of your handler.

Types

type Builder added in v1.1.0

type Builder interface {
	WithIdleTimeout(time.Duration) *ServerBuilder
	WithCors(*Cors) *ServerBuilder
	WithLogger() *ServerBuilder
	WithTLS(*tls.Config) *ServerBuilder
	WithPanicHandler(func(w http.ResponseWriter, r *http.Request, rcv ...interface{})) *ServerBuilder
	WithNotFoundHandler(http.HandlerFunc) *ServerBuilder
	WithMiddleware(Middleware) *ServerBuilder

	AddHandler(methodName string, path string, handler http.HandlerFunc, middlewares ...Middleware) *ServerBuilder
	AddFilesServer(filePath string, rootPath string, middlewares ...Middleware) *ServerBuilder

	Start() // blocking
}

type Cors

type Cors struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	ExposedHeaders   []string
	MaxAge           int
	AllowCredentials bool
	IsDebug          bool
}

Cors corst options

type Group

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

func (*Group) DELETE

func (g *Group) DELETE(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Group) FILES

func (g *Group) FILES(filePath string, rootPath string, middlewares ...Middleware)

FILES serve files from 1 directory dynamically in a group path. @filePath: must end with '/*filepath' as placeholder for filename to be accessed. @rootPath: root directory where @filepath locate.

func (*Group) GET

func (g *Group) GET(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Group) HEAD

func (g *Group) HEAD(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Group) OPTIONS

func (g *Group) OPTIONS(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Group) PATCH

func (g *Group) PATCH(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Group) POST

func (g *Group) POST(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Group) PUT

func (g *Group) PUT(path string, handler http.HandlerFunc, middlewares ...Middleware)

type GroupBuilder added in v1.1.0

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

func (*GroupBuilder) AddGroupFilesServer added in v1.1.0

func (gb *GroupBuilder) AddGroupFilesServer(filePath string, rootPath string, middlewares ...Middleware) *GroupBuilder

func (*GroupBuilder) AddGroupHandler added in v1.1.0

func (gb *GroupBuilder) AddGroupHandler(methodName string, path string, handler http.HandlerFunc, middlewares ...Middleware) *GroupBuilder

func (*GroupBuilder) Return added in v1.1.0

func (gb *GroupBuilder) Return() *ServerBuilder

type Middleware

type Middleware func(next http.HandlerFunc, params ...interface{}) http.HandlerFunc

type Opts

type Opts struct {
	Port uint16

	// EnableLogger enable logging for incoming requests
	EnableLogger bool

	// Logger logger file
	LogWriter io.Writer

	// IdleTimeout keep-alive timeout while waiting for the next request coming. If empty then no timeout.
	IdleTimeout time.Duration

	// TLS to enable HTTPS
	TLS *tls.Config

	// Cors optional, can be nil, if nil then default will be set.
	Cors *Cors

	// PanicHandler triggered if panic happened.
	// rcv: first param is argument retrieved from `recover()` function.
	PanicHandler PanicHandler

	// NotFoundHandler triggered if path not found.
	// If empty then default is used.
	NotFoundHandler http.HandlerFunc
}

type PanicHandler added in v1.1.0

type PanicHandler func(w http.ResponseWriter, r *http.Request, rcv ...interface{})

type Server

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

func New

func New(opts *Opts) *Server

func (*Server) DELETE

func (s *Server) DELETE(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Server) FILES

func (s *Server) FILES(filePath string, rootPath string, middlewares ...Middleware)

FILES serve files from 1 directory dynamically. @filePath: must end with '/*filepath' as placeholder for filename to be accessed. @rootPath: root directory where @filepath locate.

func (*Server) GET

func (s *Server) GET(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Server) Group

func (s *Server) Group(prefix string, middlewares ...Middleware) *Group

func (*Server) HEAD

func (s *Server) HEAD(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Server) ListenError

func (s *Server) ListenError() <-chan error

func (*Server) OPTIONS

func (s *Server) OPTIONS(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Server) PATCH

func (s *Server) PATCH(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Server) POST

func (s *Server) POST(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Server) PUT

func (s *Server) PUT(path string, handler http.HandlerFunc, middlewares ...Middleware)

func (*Server) Run

func (s *Server) Run()

Run the server. Blocking.

func (*Server) TLSConfig

func (s *Server) TLSConfig(cert, key string) error

TLSConfig generate certificate config using provided certificate and private key. It will overwrite the one set in Opts.

func (*Server) Use

func (s *Server) Use(m ...Middleware)

type ServerBuilder added in v1.1.0

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

func Build added in v1.1.0

func Build(port uint16) *ServerBuilder

func (*ServerBuilder) AddFilesServer added in v1.1.0

func (sb *ServerBuilder) AddFilesServer(filePath string, rootPath string, middlewares ...Middleware) *ServerBuilder

func (*ServerBuilder) AddGroup added in v1.1.0

func (sb *ServerBuilder) AddGroup(prefix string, middlewares ...Middleware) *GroupBuilder

func (*ServerBuilder) AddHandler added in v1.1.0

func (sb *ServerBuilder) AddHandler(methodName string, path string, handler http.HandlerFunc, middlewares ...Middleware) *ServerBuilder

func (*ServerBuilder) Run added in v1.1.0

func (sb *ServerBuilder) Run()

func (*ServerBuilder) WithCors added in v1.1.0

func (sb *ServerBuilder) WithCors(cors *Cors) *ServerBuilder

func (*ServerBuilder) WithIdleTimeout added in v1.1.0

func (sb *ServerBuilder) WithIdleTimeout(idleTimeout time.Duration) *ServerBuilder

func (*ServerBuilder) WithLogger added in v1.1.0

func (sb *ServerBuilder) WithLogger(wr ...io.Writer) *ServerBuilder

func (*ServerBuilder) WithMiddleware added in v1.1.0

func (sb *ServerBuilder) WithMiddleware(middleware Middleware) *ServerBuilder

func (*ServerBuilder) WithNotFoundHandler added in v1.1.0

func (sb *ServerBuilder) WithNotFoundHandler(notFoundHandlerFunc http.HandlerFunc) *ServerBuilder

func (*ServerBuilder) WithPanicHandler added in v1.1.0

func (sb *ServerBuilder) WithPanicHandler(panicHandler PanicHandler) *ServerBuilder

func (*ServerBuilder) WithTLS added in v1.1.0

func (sb *ServerBuilder) WithTLS(tls *tls.Config) *ServerBuilder

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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