Exemplo n.º 1
0
func main() {
	// Create the event bus that distributes events.
	eventBus := eventhorizon.NewInternalEventBus()
	commandBus := eventhorizon.NewInternalCommandBus()

	newEventStore := func() (eventhorizon.EventStore, error) {
		return eventhorizon.NewMemoryEventStore(eventBus), nil
	}

	newReadRepository := func(app string) (eventhorizon.ReadRepository, error) {
		return eventhorizon.NewMemoryReadRepository(), nil
	}

	common.Run(eventBus, commandBus, newEventStore, newReadRepository)
}
Exemplo n.º 2
0
func main() {
	eventBus := eventhorizon.NewInternalEventBus()
	commandBus := eventhorizon.NewInternalCommandBus()
	addr := "localhost"
	db := "demo"

	newEventStore := func() (eventhorizon.EventStore, error) {
		return eventhorizon.NewMongoEventStore(eventBus, addr, db)
	}

	newReadRepository := func(name string) (eventhorizon.ReadRepository, error) {
		return eventhorizon.NewMongoReadRepository(addr, db, name)
	}

	common.Run(eventBus, commandBus, newEventStore, newReadRepository)
}
Exemplo n.º 3
0
func main() {
	lager.SetLevels(lager.LevelsFromString(os.Getenv("LOG_LEVELS")))

	var eventBus eventhorizon.EventBus
	var commandBus eventhorizon.CommandBus
	if uri := os.Getenv("RABBITMQ_URI"); uri != "" {
		remoteEventBus, err := eventhorizon.NewRabbitMQEventBus(uri, "test", "test")
		if err != nil {
			log.Fatalln("Unable to create rabbitmq event bus:", err)
		}
		defer remoteEventBus.Close()
		eventBus = remoteEventBus

		remoteCommandBus, err := eventhorizon.NewRabbitMQCommandBus(uri, "test", "test")
		if err != nil {
			log.Fatalln("Unable to create rabbitmq command bus:", err)
		}
		defer remoteEventBus.Close()
		commandBus = remoteCommandBus
	} else {
		eventBus = eventhorizon.NewInternalEventBus()
		commandBus = eventhorizon.NewInternalCommandBus()
	}

	conn := os.Getenv("POSTGRES_URL")

	newEventStore := func() (eventhorizon.EventStore, error) {
		return eventhorizon.NewPostgresEventStore(eventBus, conn)
	}

	newReadRepository := func(name string) (eventhorizon.ReadRepository, error) {
		return eventhorizon.NewPostgresReadRepository(conn, name)
	}

	common.Run(eventBus, commandBus, newEventStore, newReadRepository)
}
Exemplo n.º 4
0
func main() {
	// Create the event bus that distributes events.
	eventBus := eventhorizon.NewInternalEventBus()
	eventBus.AddGlobalHandler(&LoggerSubscriber{})

	// Create the event store.
	eventStore, err := eventhorizon.NewMongoEventStore(eventBus, "localhost", "demo")
	if err != nil {
		log.Fatalf("could not create event store: %s", err)
	}

	eventStore.RegisterEventType(&InviteCreated{}, func() eventhorizon.Event { return &InviteCreated{} })
	eventStore.RegisterEventType(&InviteAccepted{}, func() eventhorizon.Event { return &InviteAccepted{} })
	eventStore.RegisterEventType(&InviteDeclined{}, func() eventhorizon.Event { return &InviteDeclined{} })

	// Create the aggregate repository.
	repository, err := eventhorizon.NewCallbackRepository(eventStore)
	if err != nil {
		log.Fatalf("could not create repository: %s", err)
	}

	// Register an aggregate factory.
	repository.RegisterAggregate(&InvitationAggregate{},
		func(id eventhorizon.UUID) eventhorizon.Aggregate {
			return &InvitationAggregate{
				AggregateBase: eventhorizon.NewAggregateBase(id),
			}
		},
	)

	// Create the aggregate command handler.
	handler, err := eventhorizon.NewAggregateCommandHandler(repository)
	if err != nil {
		log.Fatalf("could not create command handler: %s", err)
	}

	// Register the domain aggregates with the dispather. Remember to check for
	// errors here in a real app!
	handler.SetAggregate(&InvitationAggregate{}, &CreateInvite{})
	handler.SetAggregate(&InvitationAggregate{}, &AcceptInvite{})
	handler.SetAggregate(&InvitationAggregate{}, &DeclineInvite{})

	// Create the command bus and register the handler for the commands.
	commandBus := eventhorizon.NewInternalCommandBus()
	commandBus.SetHandler(handler, &CreateInvite{})
	commandBus.SetHandler(handler, &AcceptInvite{})
	commandBus.SetHandler(handler, &DeclineInvite{})

	// Create and register a read model for individual invitations.
	invitationRepository, err := eventhorizon.NewMongoReadRepository("localhost", "demo", "invitations")
	if err != nil {
		log.Fatalf("could not create invitation repository: %s", err)
	}
	invitationRepository.SetModel(func() interface{} { return &Invitation{} })
	invitationProjector := NewInvitationProjector(invitationRepository)
	eventBus.AddHandler(invitationProjector, &InviteCreated{})
	eventBus.AddHandler(invitationProjector, &InviteAccepted{})
	eventBus.AddHandler(invitationProjector, &InviteDeclined{})

	// Create and register a read model for a guest list.
	eventID := eventhorizon.NewUUID()
	guestListRepository, err := eventhorizon.NewMongoReadRepository("localhost", "demo", "guest_lists")
	if err != nil {
		log.Fatalf("could not create guest list repository: %s", err)
	}
	guestListRepository.SetModel(func() interface{} { return &GuestList{} })
	guestListProjector := NewGuestListProjector(guestListRepository, eventID)
	eventBus.AddHandler(guestListProjector, &InviteCreated{})
	eventBus.AddHandler(guestListProjector, &InviteAccepted{})
	eventBus.AddHandler(guestListProjector, &InviteDeclined{})

	// Clear DB collections.
	eventStore.Clear()
	invitationRepository.Clear()
	guestListRepository.Clear()

	// Issue some invitations and responses.
	// Note that Athena tries to decline the event, but that is not allowed
	// by the domain logic in InvitationAggregate. The result is that she is
	// still accepted.
	athenaID := eventhorizon.NewUUID()
	commandBus.HandleCommand(&CreateInvite{InvitationID: athenaID, Name: "Athena", Age: 42})
	commandBus.HandleCommand(&AcceptInvite{InvitationID: athenaID})
	err = commandBus.HandleCommand(&DeclineInvite{InvitationID: athenaID})
	if err != nil {
		fmt.Printf("error: %s\n", err)
	}

	hadesID := eventhorizon.NewUUID()
	commandBus.HandleCommand(&CreateInvite{InvitationID: hadesID, Name: "Hades"})
	commandBus.HandleCommand(&AcceptInvite{InvitationID: hadesID})

	zeusID := eventhorizon.NewUUID()
	commandBus.HandleCommand(&CreateInvite{InvitationID: zeusID, Name: "Zeus"})
	commandBus.HandleCommand(&DeclineInvite{InvitationID: zeusID})

	// Read all invites.
	invitations, _ := invitationRepository.FindAll()
	for _, i := range invitations {
		fmt.Printf("invitation: %#v\n", i)
	}

	// Read the guest list.
	guestList, _ := guestListRepository.Find(eventID)
	fmt.Printf("guest list: %#v\n", guestList)

	// records := eventStore.FindAllEventRecords()
	// fmt.Printf("event records:\n")
	// for _, r := range records {
	// 	fmt.Printf("%#v\n", r)
	// }
}