restik

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: MIT Imports: 10 Imported by: 0

README

restik godoc license

restik a Go library that provides an easy and convenient way to implement a REST service.

Import

import "github.com/vettich/restik"

Examples

Echo example

Simple GET request resolver

func echo(req *restik.Request) string {
	return req.Vars.String("msg")
}

func main() {
  r := restik.NewRouter()
  r.Get("/echo/{msg}", echo)
  http.Handle("/", r.Handler())
  http.ListenAndServe("0.0.0.0:8000", nil)
}

Request:

$ curl "localhost:8000/echo/example"

Response:

{"response":"example"}

Hello world example

POST request

type helloArg struct {
  Value string `json:"value"`
}

func hello(arg helloArg) (string, error) {
  if arg.Value == "" {
    return "", errors.New("value is empty")
  }
  return fmt.Sprint("Hello, " arg.Value)
}

func main() {
  r := restik.NewRouter()
  r.Post("/hello", hello)
  http.Handle("/", r.Handler())
  http.ListenAndServe("0.0.0.0:8000", nil)
}

Request:

$ curl -XPOST -d '{"value":"world!"}' "localhost:8000/hello"

Response:

{"response":"Hello, world!"}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFoundEndpoint is error
	ErrNotFoundEndpoint = NewError(404, "endpoint_not_found", "Endpoint not found")
)

Functions

This section is empty.

Types

type CorsMiddleware

type CorsMiddleware struct {
	AllowedMethods []string
	AllowedHeaders []string
	AllowedOrigin  string
}

func (*CorsMiddleware) Middleware

func (mw *CorsMiddleware) Middleware(next HandlerFunc) HandlerFunc

type Error

type Error interface {
	Error() string
	GetStatus() int
	GetCode() string
	GetMessage() string
}

func FromAnotherError

func FromAnotherError(err error) Error

FromAnotherError wrap any error to Error

func NewBadRequestError

func NewBadRequestError(args ...string) Error

NewBadRequestError return error with BadRequest status

Using:

NewBadRequestError()

or

NewBadRequestError(message)

or

NewBadRequestError(code, message)

func NewError

func NewError(status int, code, msg string) Error

NewError return new Error instance

func NewInternalError added in v0.0.8

func NewInternalError(args ...string) Error

NewInternalError return error with BadRequest status

Using:

NewInternalError()

or

NewInternalError(message)

or

NewInternalError(code, message)

func NewNotFoundError

func NewNotFoundError(args ...string) Error

NewNotFoundError return error with BadRequest status

Using:

NewNotFoundError()

or

NewNotFoundError(message)

or

NewNotFoundError(code, message)

type HandlerFunc

type HandlerFunc func(ResponseWriter, *Request)

type Middleware

type Middleware interface {
	Middleware(next HandlerFunc) HandlerFunc
}

type Reply

type Reply interface {
	New() Reply
	SetResponse(interface{})
	SetError(error)
	GetError() error
}

type Request

type Request struct {
	Vars    Vars
	Headers http.Header
	Route   *Route
	*http.Request
}

Request represent arg for handle func

func NewRequest

func NewRequest(r *http.Request, rt *Route) *Request

NewRequest create new Request instance

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriter implement rest ResponseWriter from http

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter, commonReply Reply) ResponseWriter

NewResponseWriter create new ResponseWriter instance

func (*ResponseWriter) WriteError

func (w *ResponseWriter) WriteError(err error) bool

func (*ResponseWriter) WriteJSON

func (w *ResponseWriter) WriteJSON(src interface{}) error

WriteJSON write src to response with encoding json

func (*ResponseWriter) WriteReply

func (w *ResponseWriter) WriteReply(rpl Reply) (int, error)

func (*ResponseWriter) WriteResponse

func (w *ResponseWriter) WriteResponse(resp interface{}) (int, error)

type Route

type Route struct {
	Method   string
	Endpoint string
	// contains filtered or unexported fields
}

Route is route of rest

func NewRoute

func NewRoute(method, endpoint string, fn interface{}) *Route

NewRoute create new route by http method and endpoint. fn can be one of

func([*Request | [,] *struct]) [*struct | [,] error]
func(http.ResponseWriter, *http.Request)
func(rest.ResponseWriter, *rest.Request)

type Router

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

Router is main router in rest

func NewRouter

func NewRouter() *Router

NewRouter create new Router

func (*Router) Add

func (r *Router) Add(rts ...*Route) *Router

Add add new routers

func (*Router) Delete

func (r *Router) Delete(endpoint string, fn interface{}) *Route

Delete add new route with DELETE method to router and return

func (*Router) Get

func (r *Router) Get(endpoint string, fn interface{}) *Route

Get add new route with GET method to router and return

func (*Router) Handler

func (r *Router) Handler() *mux.Router

Handler return http handler

func (*Router) Patch

func (r *Router) Patch(endpoint string, fn interface{}) *Route

Patch add new route with PATCH method to router and return

func (*Router) Post

func (r *Router) Post(endpoint string, fn interface{}) *Route

Post add new route with POST method to router and return

func (*Router) Put

func (r *Router) Put(endpoint string, fn interface{}) *Route

Put add new route with PUT method to router and return

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(hw http.ResponseWriter, hr *http.Request)

func (*Router) SetCustomReply

func (r *Router) SetCustomReply(rpl Reply)

func (*Router) SetMethodNotAllowedHandlerFunc

func (r *Router) SetMethodNotAllowedHandlerFunc(handler func(ResponseWriter, *Request))

SetMethodNotAllowedHandlerFunc set not found handler

func (*Router) SetNotFoundHandlerFunc

func (r *Router) SetNotFoundHandlerFunc(handler func(ResponseWriter, *Request))

SetNotFoundHandlerFunc set not found handler

func (*Router) Use

func (r *Router) Use(middlewares ...Middleware) *Router

func (*Router) UseFunc added in v0.0.9

func (r *Router) UseFunc(middlewareFuncs ...func(HandlerFunc) HandlerFunc) *Router

type Vars

type Vars map[string]interface{}

Vars is endpoint path variables

func NewVars

func NewVars(r *http.Request) Vars

NewVars return new Vars instance

func (Vars) Int

func (vars Vars) Int(key string, defaultValue ...int) int

Int return int value from vars by key

func (Vars) Int32

func (vars Vars) Int32(key string, defaultValue ...int32) int32

Int32 return int32 value from vars by key

func (Vars) Int32Ok

func (vars Vars) Int32Ok(key string) (ret int32, ok bool)

Int32Ok return int32 value from vars by key

func (Vars) Int64

func (vars Vars) Int64(key string, defaultValue ...int64) int64

Int64 return int64 value from vars by key

func (Vars) Int64Ok

func (vars Vars) Int64Ok(key string) (ret int64, ok bool)

Int64Ok return int64 value from vars by key

func (Vars) IntOk

func (vars Vars) IntOk(key string) (ret int, ok bool)

IntOk return int value from vars by key

func (Vars) Set

func (vars Vars) Set(key string, value interface{})

Set set value in key

func (Vars) String

func (v Vars) String(key string, defaultValue ...string) string

String return string value from vars by key

func (Vars) StringOk

func (v Vars) StringOk(key string) (string, bool)

StringOk return string value from vars by key

func (Vars) Uint

func (vars Vars) Uint(key string, defaultValue ...uint) uint

Uint return uint value from vars by key

func (Vars) Uint32

func (vars Vars) Uint32(key string, defaultValue ...uint32) uint32

Uint32 return uint32 value from vars by key

func (Vars) Uint32Ok

func (vars Vars) Uint32Ok(key string) (ret uint32, ok bool)

Uint32Ok return uint32 value from vars by key

func (Vars) Uint64

func (vars Vars) Uint64(key string, defaultValue ...uint64) uint64

Uint64 return uint64 value from vars by key

func (Vars) Uint64Ok

func (vars Vars) Uint64Ok(key string) (ret uint64, ok bool)

Uint64Ok return uint64 value from vars by key

func (Vars) UintOk

func (vars Vars) UintOk(key string) (ret uint, ok bool)

UintOk return uint value from vars by key

Directories

Path Synopsis
examples
cors command
custom-reply command
logger command
simple command

Jump to

Keyboard shortcuts

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