• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    Go
  • Created over 12 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small and evil REST framework for Go

go-rest A small and evil REST framework for Go

Reflection, Go structs, and JSON marshalling FTW!

Download, build and run example:

go get github.com/ungerik/go-rest
go install github.com/ungerik/go-rest/example && example

Small?

Yes, the framework consists of only three functions: HandleGET, HandlePOST, RunServer.

Evil?

Well, this package can be considered bad design because HandleGET and HandlePOST use dynamic typing to hide 36 combinations of handler function types to make the interface easy to use. 36 static functions would have been more lines of code but dramatic simpler in their individual implementations. So simple in fact, that there wouldn't be a point in abstracting them away in an extra framework. See this great talk about easy vs. simple: http://www.infoq.com/presentations/Simple-Made-Easy Rob Pike may also dislike this approach: https://groups.google.com/d/msg/golang-nuts/z4T_n4MHbXM/jT9PoYc6I1IJ So yes, this package can be called evil because it is an anti-pattern to all that is good and right about Go.

Why use it then? By maximizing dynamic code it is easy to use and reduces code. Yes, that introduces some internal complexity, but this complexity is still very low in absolute terms and thus easy to control and debug. The complexity of the dynamic code also does not spill over into the package users' code, because the arguments and results of the handler functions must be static typed and can't be interface{}.

Now let's have some fun:

HandleGET uses a handler function that returns a struct or string to create the GET response. Structs will be marshalled as JSON, strings will be used as body with auto-detected content type.

Format of GET handler:

func([url.Values]) ([struct|*struct|string][, error]) {}

Example:

type MyStruct struct {
	A in
	B string
}

rest.HandleGET("/data.json", func() *MyStruct {
	return &MyStruct{A: 1, B: "Hello World"}
})

rest.HandleGET("/index.html", func() string {
	return "<!doctype html><p>Hello World"
})

The GET handler function can optionally accept an url.Values argument and return an error as second result value that will be displayed as 500 internal server error if not nil.

Example:

rest.HandleGET("/data.json", func(params url.Values) (string, error) {
	v := params.Get("value")
	if v == "" {
		return nil, errors.New("Expecting GET parameter 'value'")
	}
	return "value = " + v, nil
})

HandlePOST maps POST form data or a JSON document to a struct that is passed to the handler function. An error result from handler will be displayed as 500 internal server error message. An optional first string result will be displayed as a 200 response body with auto-detected content type.

Format of POST handler:

func([*struct|url.Values]) ([struct|*struct|string],[error]) {}

Example:

rest.HandlePOST("/change-data", func(data *MyStruct) (err error) {
	// save data
	return err
})

Both HandleGET and HandlePOST also accept one optional object argument. In that case handler is interpreted as a method of the type of object and called accordingly.

Example:

rest.HandleGET("/method-call", (*myType).MethodName, myTypeObject)

More Repositories

1

go-dry

DRY (don't repeat yourself) package for Go
Go
488
star
2

go3d

A performance oriented 2D/3D math package for Go
Go
310
star
3

go-start

A high level web-framework for Go
Go
266
star
4

go-cairo

Go binding for the cairo graphics library
Go
143
star
5

pkgreflect

A Go preprocessor for package scoped reflection
Go
105
star
6

go-rss

Simple RSS parser for Go
Go
73
star
7

go-mail

Email utilities for Go
Go
31
star
8

go-gravatar

Go wrapper for the Gravatar API
Go
24
star
9

http

A HTTP command line interface for humans
Go
17
star
10

go-mavlink

MAVLink protocol implementation for Go
Go
17
star
11

mdgo

Markdown Go: Literate Programming for Go
Go
16
star
12

go-sysfs

Go package for Linux sysfs
Go
10
star
13

go-json-tree

JSON tree representation for Go
Go
6
star
14

ephesoft

Ephesoft Community, Linux Edition Version 4.0
HTML
6
star
15

go-fs

Unified file system for Go
Go
6
star
16

react-dmodel

Exploit JSX syntax to define data models and auto generate form UI for it
JavaScript
6
star
17

go-pool

Go sync.Pool applications
Go
5
star
18

BaseLib

C++ BaseLib
C++
5
star
19

go-email

Sending Emails with Go
Go
5
star
20

react-inputs

Input components for react
JavaScript
4
star
21

go-bbio

Go package for Beagle Bone IO (port of Adafruit_BBIO)
Go
4
star
22

gosync

Go
1
star
23

ungerik.github.io

HTML
1
star
24

go-reflection

Utilities extending the Go reflect package
Go
1
star
25

go-structflag

Use the Go flag package on config structs
Go
1
star
26

go-amiando

Go library for the Amiando event API
Go
1
star
27

html5-info

Collection of HTML5 related development information
1
star
28

go-command

Wrap Go functions as commands callable from CLI and REST endpoints
Go
1
star
29

gitstat

Global git status command
Go
1
star
30

chrome-netconn

ES6 classes for Chrome socket, serial, and bluetooth APIs
JavaScript
1
star
31

gen-version-info

Creates a Go source file with a constant containing the source version taken from Git or SVN
Go
1
star
32

go-string

Defines the type String with methods from the strings package
Go
1
star