|
5 | 5 | "strings" |
6 | 6 | "sync" |
7 | 7 |
|
| 8 | + "github.com/manyminds/api2go/jsonapi" |
8 | 9 | "github.com/manyminds/api2go/routing" |
9 | 10 | ) |
10 | 11 |
|
@@ -44,10 +45,24 @@ func (api *API) SetContextAllocator(allocator APIContextAllocatorFunc) { |
44 | 45 | api.contextAllocator = allocator |
45 | 46 | } |
46 | 47 |
|
47 | | -//SetRedirectTrailingSlash enables 307 redirects on urls ending with / |
48 | | -//when disabled, an URL ending with / will 404 |
49 | | -//this will and should work only if using the default router |
50 | | -//DEPRECATED |
| 48 | +// AddResource registers a data source for the given resource |
| 49 | +// At least the CRUD interface must be implemented, all the other interfaces are optional. |
| 50 | +// `resource` should be either an empty struct instance such as `Post{}` or a pointer to |
| 51 | +// a struct such as `&Post{}`. The same type will be used for constructing new elements. |
| 52 | +func (api *API) AddResource(prototype jsonapi.MarshalIdentifier, source CRUD) { |
| 53 | + api.addResource(prototype, source, api.marshalers) |
| 54 | +} |
| 55 | + |
| 56 | +// UseMiddleware registers middlewares that implement the api2go.HandlerFunc |
| 57 | +// Middleware is run before any generated routes. |
| 58 | +func (api *API) UseMiddleware(middleware ...HandlerFunc) { |
| 59 | + api.middlewares = append(api.middlewares, middleware...) |
| 60 | +} |
| 61 | + |
| 62 | +// SetRedirectTrailingSlash enables 307 redirects on urls ending with / |
| 63 | +// when disabled, an URL ending with / will 404 |
| 64 | +// this will and should work only if using the default router |
| 65 | +// DEPRECATED |
51 | 66 | func (api *API) SetRedirectTrailingSlash(enabled bool) { |
52 | 67 | if api.router == nil { |
53 | 68 | panic("router must not be nil") |
@@ -86,21 +101,21 @@ func NewAPI(prefix string) *API { |
86 | 101 | return NewAPIWithMarshalers(prefix, "", DefaultContentMarshalers) |
87 | 102 | } |
88 | 103 |
|
89 | | -//NewAPIWithRouting allows you to use a custom URLResolver, marshalers and custom routing |
90 | | -//if you want to use the default routing, you should use another constructor. |
| 104 | +// NewAPIWithRouting allows you to use a custom URLResolver, marshalers and custom routing |
| 105 | +// if you want to use the default routing, you should use another constructor. |
91 | 106 | // |
92 | | -//If you don't need any of the parameters you can skip them with the defaults: |
93 | | -//the default for `prefix` would be `""`, which means there is no namespace for your api. |
94 | | -//although we suggest using one. |
| 107 | +// If you don't need any of the parameters you can skip them with the defaults: |
| 108 | +// the default for `prefix` would be `""`, which means there is no namespace for your api. |
| 109 | +// although we suggest using one. |
95 | 110 | // |
96 | | -//if your api only answers to one url you can use a NewStaticResolver() as `resolver` |
| 111 | +// if your api only answers to one url you can use a NewStaticResolver() as `resolver` |
97 | 112 | // |
98 | | -//if you have no specific marshalling needs, use `DefaultContentMarshalers` |
| 113 | +// if you have no specific marshalling needs, use `DefaultContentMarshalers` |
99 | 114 | func NewAPIWithRouting(prefix string, resolver URLResolver, marshalers map[string]ContentMarshaler, router routing.Routeable) *API { |
100 | 115 | return newAPI(prefix, resolver, marshalers, router) |
101 | 116 | } |
102 | 117 |
|
103 | | -//newAPI is now an internal method that can be changed if params are changing |
| 118 | +// newAPI is now an internal method that can be changed if params are changing |
104 | 119 | func newAPI(prefix string, resolver URLResolver, marshalers map[string]ContentMarshaler, router routing.Routeable) *API { |
105 | 120 | if len(marshalers) == 0 { |
106 | 121 | panic("marshaler map must not be empty") |
@@ -134,8 +149,8 @@ func newAPI(prefix string, resolver URLResolver, marshalers map[string]ContentMa |
134 | 149 | return api |
135 | 150 | } |
136 | 151 |
|
137 | | -//NewAPIWithMarshalers is DEPRECATED |
138 | | -//use NewApiWithMarshalling instead |
| 152 | +// NewAPIWithMarshalers is DEPRECATED |
| 153 | +// use NewApiWithMarshalling instead |
139 | 154 | func NewAPIWithMarshalers(prefix string, baseURL string, marshalers map[string]ContentMarshaler) *API { |
140 | 155 | staticResolver := NewStaticResolver(baseURL) |
141 | 156 | return NewAPIWithMarshalling(prefix, staticResolver, marshalers) |
|
0 commit comments