Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/weos 1327 - As a developer I should be able to re-create the model from events #92

Merged
merged 25 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions controllers/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type RESTAPI struct {
registeredPrePathInitializers map[reflect.Value]int
postPathInitializers []PathInitializer
registeredPostPathInitializers map[reflect.Value]int
entityFactories map[string]model.EntityFactory
}

type schema struct {
Expand Down Expand Up @@ -172,6 +173,14 @@ func (p *RESTAPI) RegisterProjection(name string, projection projections.Project
p.projections[name] = projection
}

//RegisterEntityFactory Adds entity factory so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterEntityFactory(name string, factory model.EntityFactory) {
if p.entityFactories == nil {
p.entityFactories = make(map[string]model.EntityFactory)
}
p.entityFactories[name] = factory
}

//GetMiddleware get middleware by name
func (p *RESTAPI) GetMiddleware(name string) (Middleware, error) {
if tmiddleware, ok := p.middlewares[name]; ok {
Expand Down Expand Up @@ -253,6 +262,11 @@ func (p *RESTAPI) GetSchemas() (map[string]interface{}, error) {
return schemes, nil
}

//GetEntityFactories get event factories
func (p *RESTAPI) GetEntityFactories() map[string]model.EntityFactory {
return p.entityFactories
}

//Initialize and setup configurations for RESTAPI
func (p *RESTAPI) Initialize(ctxt context.Context) error {
//register standard controllers
Expand Down
17 changes: 15 additions & 2 deletions controllers/rest/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package rest

import (
"context"
"encoding/json"
"github.com/getkin/kin-openapi/openapi3"
"github.com/labstack/echo/v4"
"github.com/wepala/weos/model"
"io/ioutil"
"os"
"strings"
"time"
)

//New instantiates and initializes the api
Expand All @@ -21,8 +24,7 @@ func New(apiConfig string) (*RESTAPI, error) {
return api, err
}

//Start API
func Start(port string, apiConfig string) *RESTAPI {
func Start(port string, apiConfig string, replay bool) *RESTAPI {
api, err := New(apiConfig)
if err != nil {
api.EchoInstance().Logger.Error(err)
Expand All @@ -31,6 +33,17 @@ func Start(port string, apiConfig string) *RESTAPI {
if err != nil {
api.EchoInstance().Logger.Fatal(err)
}

if replay == true {
e, _ := api.GetEventStore("Default")
eventRepo := e.(*model.EventRepositoryGorm)
projection, _ := api.GetProjection("Default")
factories := api.GetEntityFactories()

total, success, failed, err := eventRepo.ReplayEvents(context.Background(), time.Time{}, factories, projection)
api.EchoInstance().Logger.Debugf("total: %d, success: %d, failed: %d, err: %s", total, success, failed, err)
}

api.EchoInstance().Logger.Fatal(api.EchoInstance().Start(":" + port))
return api
}
Expand Down
6 changes: 6 additions & 0 deletions controllers/rest/operation_initializers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}

Expand All @@ -55,6 +56,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
break
Expand All @@ -65,6 +67,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
}
Expand All @@ -79,6 +82,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
}
Expand All @@ -88,6 +92,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
} else {
Expand All @@ -104,6 +109,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
}
Expand Down
Loading