rest

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

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

Go to latest
Published: Jun 30, 2019 License: MIT Imports: 8 Imported by: 5

README

REST API Framework

REST API framework for go lang

Framework is under development

Status:

Released alpha 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
var api rest.API

// 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() error {
    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(errors.New("UNAUTHORIZED"))
})

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

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

fmt.Println("Starting server.")

http.ListenAndServe(":8080", &api)
Powered by

GoLand - JetBrains

Documentation

Overview

!

  • rest-api-framework
  • Copyright(c) 2019 Roshan Gade
  • MIT Licensed

!

  • rest-api-framework
  • Copyright(c) 2019 Roshan Gade
  • MIT Licensed

!

  • rest-api-framework
  • Copyright(c) 2019 Roshan Gade
  • MIT Licensed

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound          = errors.New("URL_NOT_FOUND")
	ErrUncaughtException = errors.New("UNCAUGHT_EXCEPTION")
)

Functions

This section is empty.

Types

type API

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

*

  • API - Application

func (*API) All

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

func (*API) Delete

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

func (*API) Exception

func (api *API) Exception(err string, handle Handler)

func (*API) Get

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

func (*API) Head

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

func (*API) Options

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

func (*API) Patch

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

func (*API) Post

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

func (*API) Put

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

func (*API) Route

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

*

  • Common Route

func (*API) ServeHTTP

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

*

  • Required handle for http module

func (*API) UnhandledException

func (api *API) UnhandledException(handle Handler)

func (*API) Use

func (api *API) Use(handle Handler)

type Context

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

*

  • Context

func (*Context) End

func (ctx *Context) End()

*

  • End

func (*Context) Get

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

*

  • Get request data from context

func (*Context) GetError

func (ctx *Context) GetError() error

*

  • Get error

func (*Context) JSON

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

*

  • Write JSON

func (*Context) PostSend

func (ctx *Context) PostSend(task Task)

*

*

func (*Context) PreSend

func (ctx *Context) PreSend(task Task)

*

*

func (*Context) Set

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

*

  • Set request data in context

func (*Context) SetHeader

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

*

  • Set Header

func (*Context) Status

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

*

  • Set Status

func (*Context) Text

func (ctx *Context) Text(data string)

*

  • Write Text

func (*Context) Throw

func (ctx *Context) Throw(err error)

*

  • Throw error

func (*Context) Write

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

*

  • Write Bytes

type Handler

type Handler func(ctx *Context)

type Namespace

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

*

  • Namespace - Application

func (*Namespace) All

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

func (*Namespace) Delete

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

func (*Namespace) Exception

func (n *Namespace) Exception(err string, handle Handler)

func (*Namespace) Get

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

func (*Namespace) Head

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

func (*Namespace) Options

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

func (*Namespace) Patch

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

func (*Namespace) Post

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

func (*Namespace) Put

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

func (*Namespace) Set

func (n *Namespace) Set(prefix string, api *API)

func (*Namespace) Use

func (n *Namespace) Use(handle Handler)

type Task

type Task func()

Directories

Path Synopsis
! * rest-api-framework * Copyright(c) 2019 Roshan Gade * MIT Licensed ! * rest-api-framework * Copyright(c) 2019 Roshan Gade * MIT Licensed
! * rest-api-framework * Copyright(c) 2019 Roshan Gade * MIT Licensed ! * rest-api-framework * Copyright(c) 2019 Roshan Gade * MIT Licensed
! * rest-api-framework * Copyright(c) 2019 Roshan Gade * MIT Licensed
! * rest-api-framework * Copyright(c) 2019 Roshan Gade * MIT Licensed

Jump to

Keyboard shortcuts

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