-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
49 lines (37 loc) · 1.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"errors"
"fmt"
"net/url"
"github.com/ungerik/go-rest"
)
func main() {
// Make debugging easier
rest.DontCheckRequestMethod = true
rest.IndentJSON = " "
// See RunServer below
stopServerChan := make(chan bool)
rest.HandleGET("/struct.json", func() *Struct {
return NewStruct()
})
rest.HandleGET("/get-method", (*Struct).Get, NewStruct())
rest.HandleGET("/index.html", func() string {
return "<!doctype html><p>Hello World!"
})
rest.HandleGET("/error", func() (*Struct, error) {
return nil, errors.New("This is an error!")
})
rest.HandleGET("/close", func() string {
stopServerChan <- true
return "stoping server..."
})
// Try: http://0.0.0.0:8080/post/struct.json?Int=66&Bool=true
rest.HandlePOST("/post/struct.json", func(in *Struct) *Struct {
return in
})
// Try: http://0.0.0.0:8080/post/values?Int=66&Bool=true
rest.HandlePOST("/post/values", func(in url.Values) string {
return fmt.Sprintf("%v", in)
})
rest.RunServer("0.0.0.0:8080", stopServerChan)
}