modelRegistry per api
This commit is contained in:
17
register.go
17
register.go
@@ -12,6 +12,7 @@ type API struct {
|
||||
routerGroup *gin.RouterGroup
|
||||
jwtSecret []byte
|
||||
authenticationHeader string
|
||||
modelRegistry []mgocrud.ModelInterface
|
||||
}
|
||||
|
||||
// Context is gin.Context with extras
|
||||
@@ -23,19 +24,19 @@ type Context struct {
|
||||
|
||||
// New returns new instance of the API
|
||||
func New(connection mgocrud.Connection, dbname string, routerGroup *gin.RouterGroup) *API {
|
||||
modelRegistry := make([]mgocrud.ModelInterface, 0)
|
||||
return &API{
|
||||
DBConnection: connection,
|
||||
DBName: dbname,
|
||||
routerGroup: routerGroup,
|
||||
DBConnection: connection,
|
||||
DBName: dbname,
|
||||
routerGroup: routerGroup,
|
||||
modelRegistry: modelRegistry,
|
||||
}
|
||||
}
|
||||
|
||||
var modelRegistry = make([]mgocrud.ModelInterface, 0)
|
||||
|
||||
// RegisterModel setups gin routes for model GET, POST, PUT and DELETE
|
||||
func (api *API) RegisterModel(m mgocrud.ModelInterface) {
|
||||
|
||||
modelRegistry = append(modelRegistry, m)
|
||||
api.modelRegistry = append(api.modelRegistry, m)
|
||||
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
@@ -75,7 +76,7 @@ func (api *API) AddMetaRoute(route string) {
|
||||
api.routerGroup.GET(route, func(c *gin.Context) {
|
||||
modelMeta := make(map[string]interface{})
|
||||
|
||||
for _, m := range modelRegistry {
|
||||
for _, m := range api.modelRegistry {
|
||||
modelMeta[mgocrud.GetCollectionName(m)] = getModelMeta(m)
|
||||
}
|
||||
|
||||
@@ -84,7 +85,7 @@ func (api *API) AddMetaRoute(route string) {
|
||||
})
|
||||
})
|
||||
|
||||
for _, m := range modelRegistry {
|
||||
for _, m := range api.modelRegistry {
|
||||
api.routerGroup.GET("/_meta/"+mgocrud.GetCollectionName(m), api.collectionGetMetaHandler(m))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user