Giter Site home page Giter Site logo

rest's Introduction

rest

import "github.com/tmc/rest"

Package rest provides a micro framework for writing json HTTP endpoints

Inspiration from dougblack's sleepy

Example:

	package main
	
	import (
		"fmt"
		"io/ioutil"
		"net/http"
		"net/url"
	
		"github.com/tmc/rest"
	)
	
	type User struct {
		Name string
	}
	
	type UserRepository struct {
		users []User
	}
	
	func (ur UserRepository) AllUsers() []User {
		return ur.users
	}
	
	type UserList struct {
		repo UserRepository
	}
	
	func (ul UserList) Get(values url.Values) (int, interface{}) {
		return http.StatusOK, ul.repo.AllUsers()
	}
	
	func main() {
		a := rest.API{}
		a.AddResource("/users", UserList{UserRepository{[]User{{"joe"}, {"sally"}}}})
	
		go a.Start(8080)
	
		resp, err := http.Get("http://127.0.0.1:8080/users")
		if err != nil {
			panic(err)
		}
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
	
		fmt.Println(resp.Status, string(body))
        // Output:
    	// 200 OK [{"Name":"joe"},{"Name":"sally"}]
	}

Constants

const (
    GET    = "GET"
    POST   = "POST"
    PUT    = "PUT"
    DELETE = "DELETE"
)

type API

type API struct{}

func (*API) Abort

func (api *API) Abort(rw http.ResponseWriter, statusCode int)

func (*API) AddResource

func (api *API) AddResource(path string, resource Resource)

func (*API) Start

func (api *API) Start(port int)

type BaseResource

type BaseResource struct{}

func (BaseResource) Delete

func (BaseResource) Delete(values url.Values) (int, interface{})

func (BaseResource) Get

func (BaseResource) Get(values url.Values) (int, interface{})

func (BaseResource) Post

func (BaseResource) Post(values url.Values) (int, interface{})

func (BaseResource) Put

func (BaseResource) Put(values url.Values) (int, interface{})

type Resource

type Resource interface{}

rest's People

Contributors

tmc avatar

Stargazers

Angus H. avatar Daniel Lindsley avatar Matt Croydon avatar

Watchers

 avatar James Cloos avatar  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.