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