-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
78 lines (66 loc) · 1.87 KB
/
example.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"log"
"net/http"
"github.com/caas-team/apiserver/pkg/server"
"github.com/caas-team/apiserver/pkg/store/apiroot"
"github.com/caas-team/apiserver/pkg/store/empty"
"github.com/caas-team/apiserver/pkg/types"
"github.com/gorilla/mux"
)
type Foo struct {
Bar string `json:"bar"`
}
type FooStore struct {
empty.Store
}
func (f *FooStore) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
return types.APIObject{
Type: "foos",
ID: id,
Object: Foo{
Bar: "baz",
},
}, nil
}
func (f *FooStore) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
return types.APIObjectList{
Objects: []types.APIObject{
{
Type: "foostore",
ID: "foo",
Object: Foo{
Bar: "baz",
},
},
},
}, nil
}
func main() {
// Create the default server
s := server.DefaultAPIServer()
// Add some types to it and setup the store and supported methods
s.Schemas.MustImportAndCustomize(Foo{}, func(schema *types.APISchema) {
schema.Store = &FooStore{}
schema.CollectionMethods = []string{http.MethodGet}
schema.ResourceMethods = []string{http.MethodGet}
})
// Register root handler to list api versions
apiroot.Register(s.Schemas, []string{"v1", "v2"})
// Setup mux router to assign variables the server will look for (refer to MuxURLParser for all variable names)
router := mux.NewRouter()
router.Handle("/{prefix}/{type}", s)
router.Handle("/{prefix}/{type}/{name}", s)
// When a route is found construct a custom API request to serves up the API root content
router.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
s.Handle(&types.APIRequest{
Request: r,
Response: rw,
Type: "apiRoot",
URLPrefix: "v1",
})
})
// Start API Server
log.Print("Listening on :8080")
log.Fatal(http.ListenAndServe(":8080", router))
}