/
main.go
55 lines (47 loc) · 1010 Bytes
/
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
50
51
52
53
54
55
package main
import (
"github.com/darkhelmet/env"
restful "github.com/emicklei/go-restful"
"github.com/jmcvetta/randutil"
"log"
"net/http"
)
type Crew struct {
Name string
Rank string
}
type Foo struct {
Crew []Crew
}
type Bar struct {
Rand string
}
func getFoo(request *restful.Request, response *restful.Response) {
f := Foo{
Crew: []Crew{
Crew{"Kirk", "Captain"},
Crew{"McCoy", "Doctor"},
},
}
response.WriteEntity(&f)
}
func getBar(request *restful.Request, response *restful.Response) {
var b Bar
b.Rand, _ = randutil.AlphaString(8)
response.WriteEntity(&b)
}
func main() {
port := env.StringDefault("PORT", "8080")
baseUrl := ":" + port
var ws restful.WebService
ws.Path("/api").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("/bar").To(getBar).
Writes(Bar{}))
ws.Route(ws.GET("/foo").To(getFoo).
Writes(Foo{}))
restful.Add(&ws)
log.Printf("Starting webserver on %v...", baseUrl)
log.Fatal(http.ListenAndServe(baseUrl, nil))
}