Skip to content

Commit 0b829b4

Browse files
committed
Extract router to be an interface
this is particulary useful since more and more people are trying to use api2go with gin router.
1 parent 590b9e0 commit 0b829b4

File tree

10 files changed

+316
-155
lines changed

10 files changed

+316
-155
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ go get github.com/manyminds/api2go/jsonapi
3232
- [Ignoring fields](#ignoring-fields)
3333
- [Manual marshaling / unmarshaling](#manual-marshaling--unmarshaling)
3434
- [SQL Null-Types](#sql-null-types)
35+
- [Using api2go with the gin framework](#api2go-with-gin)
3536
- [Building a REST API](#building-a-rest-api)
3637
- [Query Params](#query-params)
3738
- [Using Pagination](#using-pagination)
@@ -318,6 +319,47 @@ these values, it is required to implement the `json.Marshaller` and `json.Unmars
318319
But you dont have to do this by yourself! There already is a library that did the work for you. We recommend that you use the types
319320
of this library: http://gopkg.in/guregu/null.v2/zero
320321

322+
## Using api2go with the gin framework
323+
324+
If you want to use api2go with [gin](https://github.com/gin-gonic/gin) you need to use a different router than the default one.
325+
Get the according adapter using:
326+
327+
```go get github.com/manyminds/api2go-adapter/gingonic```
328+
329+
After that you can bootstrap api2go the following way:
330+
```go
331+
import (
332+
"github.com/gin-gonic/gin"
333+
"github.com/manyminds/api2go"
334+
"github.com/manyminds/api2go-adapter/gingonic"
335+
"github.com/manyminds/api2go/examples/model"
336+
"github.com/manyminds/api2go/examples/resource"
337+
"github.com/manyminds/api2go/examples/storage"
338+
)
339+
340+
func main() {
341+
r := gin.Default()
342+
api := api2go.NewAPIWithRouting(
343+
"api",
344+
api2go.NewStaticResolver("/"),
345+
api2go.DefaultContentMarshalers,
346+
gingonic.New(r),
347+
)
348+
349+
userStorage := storage.NewUserStorage()
350+
chocStorage := storage.NewChocolateStorage()
351+
api.AddResource(model.User{}, resource.UserResource{ChocStorage: chocStorage, UserStorage: userStorage})
352+
api.AddResource(model.Chocolate{}, resource.ChocolateResource{ChocStorage: chocStorage, UserStorage: userStorage})
353+
354+
r.GET("/ping", func(c *gin.Context) {
355+
c.String(200, "pong")
356+
})
357+
r.Run(":8080")
358+
}
359+
```
360+
361+
Keep in mind that you absolutely should map api2go under its own namespace to not get conflicts with your normal routes.
362+
321363
## Building a REST API
322364

323365
First, write an implementation of `api2go.CRUD`. You have to implement at least these 4 methods:

0 commit comments

Comments
 (0)