minirest

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2020 License: MIT Imports: 11 Imported by: 0

README

PkgGoDev A micro REST framework

Documentation

Index

Constants

View Source
const (
	CodeOk               = 200
	CodeNoContent        = 204
	CodeBadRequest       = 400
	CodeNotFound         = 404
	CodeMethodNotAllowed = 405
	CodeInternalError    = 500
	CodeOverload         = 503
)

HTTP status codes

View Source
const (
	MsgOk               = "ok"
	MsgNoContent        = "no_content"
	MsgBadRequest       = "bad_request"
	MsgNotFound         = "not_found"
	MsgMethodNotAllowed = "method_not_allowed"
	MsgInternalError    = "internal_error"
	MsgOverloadError    = "server_overload"
)

HTTP status message

Variables

This section is empty.

Functions

This section is empty.

Types

type CORSOption

type CORSOption struct {
	AllowOrigin      string
	AllowCredentials string
	ExposeHeaders    string
	AllowHeaders     string
	AllowMethods     string
}

CORSOption set options for CORS headers

type Controller

type Controller interface {
	// Endpoints register all endpoints to its handler.
	// You can register your middleware as well in here
	Endpoints() *Endpoints
}

Controller is interface for controller. If you want to register service into controller, make sure you have field with the same name as the service, example:

type Controller struct {
	UserService *UserService
	ItemService *ItemService
}

type Endpoints

type Endpoints struct {
	// Set to true for returning gzip encoded response on all endpoints
	Gzip bool
	// contains filtered or unexported fields
}

Endpoints register handlers its path and method

func (*Endpoints) Add

func (ep *Endpoints) Add(method, path string, callback interface{})

Add add endpoint with custom method

func (*Endpoints) BasePath

func (ep *Endpoints) BasePath(path string)

BasePath set base path for endpoints

func (*Endpoints) DELETE

func (ep *Endpoints) DELETE(path string, callback interface{})

DELETE add endpoint with method DELETE

func (*Endpoints) GET

func (ep *Endpoints) GET(path string, callback interface{})

GET add endpoint with method GET

func (*Endpoints) Middlewares

func (ep *Endpoints) Middlewares(mds ...handleToHandle)

Middlewares register middleware chain. miniREST is using julienschmidt/httprouter for implementing router, so the middleware will use httprouter.Handle as its handle

func (*Endpoints) PATCH

func (ep *Endpoints) PATCH(path string, callback interface{})

PATCH add method endpoint with method PATCH

func (*Endpoints) POST

func (ep *Endpoints) POST(path string, callback interface{})

POST add method endpoint with method POST

func (*Endpoints) PUT

func (ep *Endpoints) PUT(path string, callback interface{})

PUT add method endpoint with method PUT

type Minirest

type Minirest struct {
	// Set to true for returning gzip encoded response globally
	Gzip bool
	// contains filtered or unexported fields
}

Minirest is singleton for Minirest framework

func New

func New() *Minirest

New initiate new Minirest

func (*Minirest) AddController

func (mn *Minirest) AddController(controller Controller, srv ...Service)

AddController add controller. You can link services srv into controller, see Controller and AddService for more information. If service is not registered, it will automatically register it

func (*Minirest) AddService

func (mn *Minirest) AddService(service Service)

AddService add service. Service must be pointer to struct

func (*Minirest) CORS

func (mn *Minirest) CORS(opt CORSOption)

CORS set CORS

func (*Minirest) LinkService added in v1.1.0

func (mn *Minirest) LinkService(dest Service, svcs ...Service)

LinkService link service dest with services svc. If service not registered, it will automatically registered. Service must have fields with same name as services that want to be linked. example:

type Service struct {
	UserService *UserService
	ItemService *ItemService
}

func (*Minirest) RunServer

func (mn *Minirest) RunServer()

RunServer run http server

func (*Minirest) ServeIP

func (mn *Minirest) ServeIP(ip string)

ServeIP set http server IP

func (*Minirest) ServePort

func (mn *Minirest) ServePort(port string)

ServePort set http server port

type Response

type Response struct {
	StatusCode  int         `json:"statusCode"`
	Status      string      `json:"status"`
	Description string      `json:"description,omitempty"`
	Body        interface{} `json:"body,omitempty"`
}

Response is body for HTTP response

type ResponseBuilder

type ResponseBuilder struct {
	// Set to true for returning gzip encoded response
	Gzip bool
	// contains filtered or unexported fields
}

ResponseBuilder is a response builder

func (*ResponseBuilder) BadRequest

func (resp *ResponseBuilder) BadRequest(desc string) *ResponseBuilder

BadRequest build response with HTTP Status 400

func (*ResponseBuilder) Body

func (resp *ResponseBuilder) Body(body interface{}) *ResponseBuilder

Body set body

func (*ResponseBuilder) Headers

func (resp *ResponseBuilder) Headers(headers [][2]string) *ResponseBuilder

Headers add headers

func (*ResponseBuilder) InternalError

func (resp *ResponseBuilder) InternalError(desc string) *ResponseBuilder

InternalError build response with HTTP Status 500

func (*ResponseBuilder) MethodNotAllowed

func (resp *ResponseBuilder) MethodNotAllowed(desc string) *ResponseBuilder

MethodNotAllowed build response with HTTP Status 405

func (*ResponseBuilder) NoContent

func (resp *ResponseBuilder) NoContent(desc string) *ResponseBuilder

NoContent build response with HTTP Status 204

func (*ResponseBuilder) NotFound

func (resp *ResponseBuilder) NotFound(desc string) *ResponseBuilder

NotFound build response with HTTP Status 404

func (*ResponseBuilder) Ok

func (resp *ResponseBuilder) Ok(data interface{}) *ResponseBuilder

Ok build response with HTTP Status 200

func (*ResponseBuilder) ServerOverload

func (resp *ResponseBuilder) ServerOverload(desc string) *ResponseBuilder

ServerOverload build response with HTTP Status 503

func (*ResponseBuilder) Status

func (resp *ResponseBuilder) Status(code int) *ResponseBuilder

Status set status code

type Service

type Service interface {
	// Init initialize service
	Init()
}

Service is interface for service

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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