ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - ungerik/go-rest: A small and evil REST framework for Go
A small and evil REST framework for Go. Contribute to ungerik/go-rest development by creating an account on GitHub.
Visit Site

GitHub - ungerik/go-rest: A small and evil REST framework for Go

GitHub - ungerik/go-rest: 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)

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory