Skip to content

tmc/rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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{}

About

Small resource-oriented http library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages