restful

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseUrl

func ParseUrl(url string) (*regexp.Regexp, []string)

Types

type Config

type Config struct {
	Path      string
	ServePort uint16
	NetListen func(port uint16) (net.Listener, error)
	Response  func(result interface{}, err error) []byte
}

Config is the configuration of the RESTful server.

type Response

type Response struct {
	Result interface{}
	Error  int
	Desc   string
}

Response represent the default RESTful Response data structure.

type Route

type Route struct {
	GetHandler    func(params htp.Params) (interface{}, error)
	PutHandler    func(params htp.Params) (interface{}, error)
	PatchHandler  func(params htp.Params) (interface{}, error)
	DeleteHandler func(params htp.Params) (interface{}, error)
	PostHandler   func(data []byte) (interface{}, error)
	// contains filtered or unexported fields
}

func NewRoute

func NewRoute(regex *regexp.Regexp) *Route

func (*Route) SetDeleteHandler

func (r *Route) SetDeleteHandler(handler func(htp.Params) (interface{}, error)) *Route

func (*Route) SetGetHandler

func (r *Route) SetGetHandler(handler func(htp.Params) (interface{}, error)) *Route

func (*Route) SetHandler

func (r *Route) SetHandler(method string, handler interface{}) *Route

func (*Route) SetParams

func (r *Route) SetParams(method string, params ...string) *Route

func (*Route) SetPatchHandler

func (r *Route) SetPatchHandler(handler func(htp.Params) (interface{}, error)) *Route

func (*Route) SetPostHandler

func (r *Route) SetPostHandler(handler func(data []byte) (interface{}, error)) *Route

func (*Route) SetPutHandler

func (r *Route) SetPutHandler(handler func(htp.Params) (interface{}, error)) *Route

type Server

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

func NewServer

func NewServer(cfg *Config) *Server

NewServer creates and return a RESTful server instance.

func (*Server) RegisterAction

func (s *Server) RegisterAction(method, url string, handler interface{}) error

RegisterAction register a RESTful handler with the given url and method. For a url with parameters, add /:param at the end of url like /api/block/:hash. multiple parameters are supported, like /api/account/:assetid/:address. The registered handler type must match the method, for example, GET method must use func(params http.Params) (interface{}, error) as handler.

func (*Server) RegisterDeleteAction

func (s *Server) RegisterDeleteAction(url string, handler func(htp.Params) (interface{}, error)) error

RegisterDeleteAction register a RESTful handler with the given url.

func (*Server) RegisterGetAction

func (s *Server) RegisterGetAction(url string, handler func(htp.Params) (interface{}, error)) error

RegisterGetAction register a RESTful handler with the given url.

func (*Server) RegisterPatchAction

func (s *Server) RegisterPatchAction(url string, handler func(htp.Params) (interface{}, error)) error

RegisterPatchAction register a RESTful handler with the given url.

func (*Server) RegisterPostAction

func (s *Server) RegisterPostAction(url string, handler func([]byte) (interface{}, error)) error

RegisterPostAction register a RESTful handler with the given url.

func (*Server) RegisterPutAction

func (s *Server) RegisterPutAction(url string, handler func(htp.Params) (interface{}, error)) error

RegisterPutAction register a RESTful handler with the given url.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Server) Start

func (s *Server) Start() error

func (*Server) Stop

func (s *Server) Stop() error

Jump to

Keyboard shortcuts

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