rest

package module
v0.0.1-beta.9 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2019 License: MIT Imports: 8 Imported by: 5

README

REST API Framework

REST API framework for go lang

Framework is under development

Status:

Released beta version
See examples

  • Request Interceptors/Middleware
  • Routes with URL pattern
  • Methods [GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH]
  • Extend routes with namespace
  • Error handler
  • HTTP, HTTPS support

How to use?

var api = rest.New("/v1")

// request interceptor / middleware
api.Use(func(ctx *rest.Context) {
  ctx.Set("authtoken", "roshangade")
})

// hooks support on request pre-send or post-send
// ctx.PreSend(func() error) OR ctx.PostSend(func() error)
api.Use(func(ctx *rest.Context) {
  s := time.Now().UnixNano()
  ctx.PreSend(func() {
    x := time.Now().UnixNano() - s
    ctx.SetHeader("X-Runtime", strconv.FormatInt(x/int64(time.Millisecond), 10))
    return nil
  })
})


// routes
api.Get("/", func(ctx *rest.Context) {
  ctx.Text("Hello World!")
})

api.Get("/foo", func(ctx *rest.Context) {
  ctx.Status(401).Throw("UNAUTHORIZED")
})

api.Get("/:bar", func(ctx *rest.Context) {
  fmt.Println("authtoken", ctx.Get("authtoken"))
  ctx.JSON(ctx.Params)
})

// error handler
api.OnError("UNAUTHORIZED", func(ctx *rest.Context) {
  ctx.Text("You are unauthorized")
})

fmt.Println("Starting server.")

http.ListenAndServe(":8080", api)

Extend routes

var user = rest.Extend("/user", api)

user.Use(func(ctx *rest.Context) {
    //User middleware will execute for /user/* routes
})

user.Get("/:uid/profile", func(ctx *rest.Context) {
    ctx.JSON(`{"user": "profile"}`)
})

user.Get("/:uid", func(ctx *rest.Context) {
    ctx.JSON(ctx.Params)
})

Documentation

https://godoc.org/github.com/go-rs/rest-api-framework

Powered by

GoLand - JetBrains

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCodeNotFound     = "URL_NOT_FOUND"
	ErrCodeRuntimeError = "RUNTIME_ERROR"
)

error variables to handle expected errors

Functions

This section is empty.

Types

type API

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

API It provides all methods, which are required to setup all kind of routes. It manages request interceptor/middlewares. which are used to intercept requests calls before performing actual operation, such as authentication, method override, request logger, etc. Also, it handles errors, which are thrown by users

func New

func New(prefix string) *API

Initialize an API with prefix value and return the API pointer

func (*API) All

func (api *API) All(pattern string, handle Handler)

All method is slightly similar to Use method, but in All method you can use pattern before intercepting any request

func (*API) Delete

func (api *API) Delete(pattern string, handle Handler)

Delete method is used for DELETE http method with specific pattern

func (*API) Get

func (api *API) Get(pattern string, handle Handler)

Get method is used for GET http method with specific pattern

func (*API) Head

func (api *API) Head(pattern string, handle Handler)

Head method is used for HEAD http method with specific pattern

func (*API) OnError

func (api *API) OnError(code string, handle Handler)

OnError method is used to handle a custom errors thrown by users

func (*API) OnErrors

func (api *API) OnErrors(codes []string, handle Handler)

OnErrors method is used to handle a custom errors thrown by users

func (*API) Options

func (api *API) Options(pattern string, handle Handler)

Options method is used for OPTIONS http method with specific pattern

func (*API) Patch

func (api *API) Patch(pattern string, handle Handler)

Patch method is used for PATCH http method with specific pattern

func (*API) Post

func (api *API) Post(pattern string, handle Handler)

Post method is used for POST http method with specific pattern

func (*API) Put

func (api *API) Put(pattern string, handle Handler)

Put method is used for PUT http method with specific pattern

func (*API) Route

func (api *API) Route(method string, pattern string, handle Handler)

Route method is used to define specific routes with handler. You can use http method, declare patten and finally you have to pass handler

func (*API) ServeHTTP

func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request)

It's required handle for http module. Every request travels from this method.

func (*API) UnhandledException

func (api *API) UnhandledException(handle Handler)

UnhandledException method is used to handle all unhandled exceptions

func (*API) Use

func (api *API) Use(handle Handler)

Use method is use to declare interceptors/middlewares It executes on all type method request

type Context

type Context struct {
	// available to users
	Request  *http.Request
	Response http.ResponseWriter
	Query    url.Values
	Body     map[string]interface{}
	Params   map[string]string
	// contains filtered or unexported fields
}

Context, which initializes at every request with pre-declared variables such as Request, Response, Query, Body, Params etc.

func (*Context) Delete

func (ctx *Context) Delete(key string)

Delete data

func (*Context) End

func (ctx *Context) End()

Marked the request is ended

func (*Context) Get

func (ctx *Context) Get(key string) (val interface{}, exists bool)

Get data

func (*Context) GetError

func (ctx *Context) GetError() error

Get error if any

func (*Context) JSON

func (ctx *Context) JSON(data interface{})

Send JSON data in response

func (*Context) PostSend

func (ctx *Context) PostSend(task Task)

Register pre-post hook

func (*Context) PreSend

func (ctx *Context) PreSend(task Task)

Register pre-send hook

func (*Context) Set

func (ctx *Context) Set(key string, val interface{})

Set data

func (*Context) SetHeader

func (ctx *Context) SetHeader(key string, val string) *Context

Set response header

func (*Context) Status

func (ctx *Context) Status(code int) *Context

Set response status

func (*Context) Text

func (ctx *Context) Text(data string)

Send text in response

func (*Context) Throw

func (ctx *Context) Throw(code string)

Caught error in context on throw

func (*Context) ThrowWithError

func (ctx *Context) ThrowWithError(code string, err error)

Throw an error with error

func (*Context) Write

func (ctx *Context) Write(data []byte)

Send response in bytes

type Handler

type Handler func(ctx *Context)

Handler function is used to perform a specified task on request. In handler function, you will get rest context object, which carries request, response writer objects and other methods too.

type Namespace

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

Namespace is used to extend routes with a specific prefix

func Extend

func Extend(prefix string, api *API) *Namespace

Set method is used to set prefix with API

func (*Namespace) All

func (n *Namespace) All(pattern string, handle Handler)

All method is used to execute for all methods, but with some more prefix as compare to Use method

func (*Namespace) Delete

func (n *Namespace) Delete(pattern string, handle Handler)

Delete method is used for DELETE http method with specific pattern

func (*Namespace) Get

func (n *Namespace) Get(pattern string, handle Handler)

Get method is used for GET http method with specific pattern

func (*Namespace) Head

func (n *Namespace) Head(pattern string, handle Handler)

Head method is used for HEAD http method with specific pattern

func (*Namespace) OnError

func (n *Namespace) OnError(code string, handle Handler)

OnError method is an exactly same with API.OnError

func (*Namespace) Options

func (n *Namespace) Options(pattern string, handle Handler)

Options method is used for OPTIONS http method with specific pattern

func (*Namespace) Patch

func (n *Namespace) Patch(pattern string, handle Handler)

Patch method is used for PATCH http method with specific pattern

func (*Namespace) Post

func (n *Namespace) Post(pattern string, handle Handler)

Post method is used for POST http method with specific pattern

func (*Namespace) Put

func (n *Namespace) Put(pattern string, handle Handler)

Put method is used for PUT http method with specific pattern

func (*Namespace) Use

func (n *Namespace) Use(handle Handler)

Use method is used to set interceptors/middlewares for all requests, which are going to travel through prefix

type Task

type Task func()

Task, which is used to perform a specific job, just before request completion or after request completion

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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