ghttp

package
v0.0.0-...-de30e9e Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2017 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetNextCtxKey

func GetNextCtxKey() int

GetNextCtxKey returns next available integer key for the Ctx

Types

type Ctx

type Ctx interface {
	context.Context
}

func ChildCtx

func ChildCtx(parent Ctx, key, val interface{}) Ctx

type Handler

type Handler interface {
	ServeHTTPWithCtx(Ctx, *Http) Ctx
}

type HandlerFunc

type HandlerFunc func(Ctx, *Http) Ctx

func (HandlerFunc) ServeHTTPWithCtx

func (hf HandlerFunc) ServeHTTPWithCtx(c Ctx, h *Http) Ctx

type Http

type Http struct {
	Resp  Response
	Query url.Values
	W     http.ResponseWriter
	R     *http.Request
	Log   log.Context //nil - use logging.Decor to assign the logger
}

type Response

type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message,omitempty"`
	Data    interface{} `json:"data,omitempty"`
}

type Router

type Router map[string]Handler

Router impliments http handler registration Usage example: http.Handle("/query", router.Router{"GET":getHandler, "POST":postHandler}) In case there is no handler found, http status code 501 is returned Refer to https://golang.org/src/net/http/method.go for supported methods

https://golang.org/pkg/net/http/ for overall http information

Below is a simple HTTP server:

//Get Http request handler function

var getHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("GetTestResponse"))
})

//Post http request handler function

var postHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("PostTestResponse"))
})

//Put http request handler type PutHandler struct { }

func (ph *PutHandler) ServeHTTP (w http.ResponseWriter, r *http.Request) {
	//decode request
	//process request
	//encode response
	//write encoded response
	w.Write([]byte("PutTestResponse"))
}
func main() {
	mux := http.NewServeMux()
	mux.Handle("/test", Router{"GET":getHandler, "POST":postHandler, "PUT":PutHanlder{}})
	log.Println("Listening on port 8080...")
	log.Println(http.ListenAndServe(":8080", mux))
}

type Router map[string]http.Handler

func (Router) ServeHTTP

func (rt Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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