monkeys

package module
v0.0.0-...-8f08ccc Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2018 License: MIT Imports: 7 Imported by: 0

README

monkeys team

monkeys

tiny rest framework for golang

Install

go get -u github.com/melihmucuk/monkeys

Usage

package main

import (
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
	"github.com/melihmucuk/monkeys"
)

type ItemController struct{}

func (ic ItemController) Get() *monkeys.Response {
	items := []string{"item1", "item2"}
	data := map[string][]string{"items": items}
	return &monkeys.Response{StatusCode: 200, Data: data}
}

func (ic ItemController) GetByID(ID string) *monkeys.Response {
	return &monkeys.Response{StatusCode: 200, Data: ID}
}

func (ic ItemController) Post(body monkeys.Request) *monkeys.Response {
	return &monkeys.Response{StatusCode: 200, Data: body}
}


type PingController struct{}

func (pc PingController) Get() *monkeys.Response {
	data := make(map[string]interface{})
	data["respond"] = "pong"
	return &monkeys.Response{StatusCode: 200, Data: data}
}

// HostMiddleware custom middleware
func HostMiddleware(next httprouter.Handle) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
		log.Printf("%s", r.Host)
		if r.Host == "localhost:5000" {
			next(w, r, p)
			return
		}
		monkeys.ErrorResponse(http.StatusForbidden, 4, "Host not allowed!", w)
	}
}

func main() {
	api := monkeys.NewAPI()
	api.Use(monkeys.Logger)
	api.Use(HostMiddleware)
	api.NewEndpointGroup("/item", ItemController{})
	api.NewEndpoint("GET", "/ping", PingController{})
	api.Start(5000)
}

TODO

  • Integrate all http methods (PUT, PATCH etc.)
  • Create documentation
  • Write tests
  • Add benchmark result
  • Create built-in middleware for CORS

Documentation

Index

Constants

View Source
const (
	GET    = "GET"
	POST   = "POST"
	PUT    = "PUT"
	DELETE = "DELETE"
	HEAD   = "HEAD"
	PATCH  = "PATCH"
)

Available Methods

Variables

This section is empty.

Functions

func ErrorResponse

func ErrorResponse(statusCode, errorCode int, message string, w http.ResponseWriter)

ErrorResponse TO-DO

func Logger

func Logger(next httprouter.Handle) httprouter.Handle

Logger is a middleware that logs request method, request uri and processing time.

func SuccessResponse

func SuccessResponse(response *Response, w http.ResponseWriter)

SuccessResponse TO-DO

Types

type API

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

API TO-DO

func NewAPI

func NewAPI() *API

NewAPI TO-DO

func (*API) NewEndpoint

func (api *API) NewEndpoint(method, path string, resource interface{})

NewEndpoint TO-DO

func (*API) NewEndpointGroup

func (api *API) NewEndpointGroup(path string, resource interface{})

NewEndpointGroup TO-DO

func (*API) Router

func (api *API) Router() *httprouter.Router

Router TO-DO

func (*API) Start

func (api *API) Start(port int) error

Start TO-DO

func (*API) Use

func (api *API) Use(middleware func(next httprouter.Handle) httprouter.Handle)

Use TO-DO

type GetByIDController

type GetByIDController interface {
	GetByID(ID string) *Response
}

GetByIDController TO-DO

type GetController

type GetController interface {
	Get() *Response
}

GetController TO-DO

type PostController

type PostController interface {
	Post(values Request) *Response
}

PostController TO-DO

type Request

type Request map[string]interface{}

Request TO-DO

type Response

type Response struct {
	StatusCode   int         `json:"-"`
	Meta         interface{} `json:"meta,omitempty"`
	Data         interface{} `json:"data,omitempty"`
	ErrorCode    int         `json:"error_code,omitempty"`
	ErrorMessage string      `json:"error_message,omitempty"`
}

Response TO-DO

Jump to

Keyboard shortcuts

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