Giter Site home page Giter Site logo

rtr's Introduction

gorest

A simple router for REST APIs built on top of https://github.com/gowww/router.

Installation

$ go get github.com/ijsnow/gorest
import "github.com/ijsnow/gorest"

Usage

  1. Create a router
rt := gorest.NewRouter()
  1. Declare some routes with Get, Post, Put, or Delete methods.

Using basic route generator. You have to give the route your WriteFunc.

// Return an http status code and the data you would like to serve
func getUser(w http.ResponseWriter, r *http.Request) (int, interface{}) {
	user := User{
    Name: "Isaac",
  }

	return http.StatusOK, user
}

// ...

rt.Get(gorest.JSON, "/user", getUser)

Using the JSON helper

rt.GetJSON("/user", getUser)
  1. Give your server the handler
http.ListenAndServe(":8080", rt.GetHandler())

Additional Info

  1. Using middlewares
// I'll get called first
func middleware1(next gorest.HandlerFunc) gorest.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) (int, interface{}) {
		fmt.Println("This is middleware 1")

		return next(w, r)
	}
}

// I'll get called second
func middleware2(next gorest.HandlerFunc) gorest.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) (int, interface{}) {
		fmt.Println("This is middleware 2, let's gamble")
    
    if tossCoin() == "heads" {
      return http.StatusBadRequest, "You lose. No serving cool data for you."
    }

		return next(w, r)
	}
}

// ...

// Execution of middlewares flows from right to left
rt.GetJSON("/user", getUser, middleware2, middleware1)

rtr's People

Contributors

ijsnow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.