Exemple #1
0
func TestMessageStream(t *testing.T) {
	memberCache, err := db.NewIdMultiMap()
	if err != nil {
		panic(err)
	}
	members, err := stores.NewMembershipStore(memberCache)
	if err != nil {
		t.Fatal(err)
	}
	streamMux, err := NewStreamMux()
	if err != nil {
		t.Fatal(err)
	}
	_es, err := NewMessageStream(members, streamMux)
	if err != nil {
		t.Fatal(err)
	}
	es := MessageStreamTest{_es, t}
	es.push(message("event1", "user1"), 0)
	es.push(message("event1", "user2"), 1)
	es.push(message("event1", "user3"), 2)
	es.check(0, 3, 5, "user3")
	es.check(0, 3, 3, "user3")
	es.check(0, 3, 1, "user3")
	es.check(0, 3, 0)
	es.check(1, 3, 1, "user3")
	es.check(2, 3, 1, "user3")
	es.check(3, 2, 1, "user3")
	es.check(3, 0, 3, "user3")
	es.check(3, 0, 0)
	es.push(message("event2", "user4"), 3)
	es.push(message("event2", "user5"), 4)
	es.push(message("event2", "user6"), 5)
	es.check(0, 6, 2, "user3", "user6")
	es.check(0, 6, 1, "user3")
	es.check(0, 6, 0)
	es.check(0, 5, 1, "user3")
	es.push(message("event7", "user7"), 6)
	es.check(2, 7, 5, "user3", "user6", "user7")
	es.check(3, 7, 5, "user6", "user7")
}
Exemple #2
0
func setup() services {
	stateStore, err := db.NewStateStore()
	if err != nil {
		panic(err)
	}
	roomStore, err := db.NewRoomDb()
	if err != nil {
		panic(err)
	}
	userStore, err := stores.NewUserDb(stateStore)
	if err != nil {
		panic(err)
	}
	aliasCache, err := db.NewIdMap()
	if err != nil {
		panic(err)
	}
	aliasStore, err := stores.NewAliasStore(aliasCache)
	if err != nil {
		panic(err)
	}
	memberCache, err := db.NewIdMultiMap()
	if err != nil {
		panic(err)
	}
	memberStore, err := stores.NewMembershipStore(memberCache)
	if err != nil {
		panic(err)
	}
	streamMux, err := events.NewStreamMux()
	if err != nil {
		panic(err)
	}
	messageStream, err := events.NewMessageStream(memberStore, streamMux)
	if err != nil {
		panic(err)
	}
	presenceStream, err := events.NewPresenceStream(memberStore, streamMux)
	if err != nil {
		panic(err)
	}
	typingStream, err := events.NewTypingStream(memberStore, streamMux)
	if err != nil {
		panic(err)
	}

	roomService, err := service.CreateRoomService(
		roomStore,
		aliasStore,
		memberStore,
		messageStream,
		presenceStream,
		typingStream,
		typingStream,
	)
	if err != nil {
		panic(err)
	}
	userService, err := service.CreateUserService(userStore)
	if err != nil {
		panic(err)
	}
	profileService, err := service.NewProfileService(
		presenceStream,
		presenceStream,
		memberStore,
		roomStore,
		messageStream,
	)
	if err != nil {
		panic(err)
	}
	presenceService, err := service.NewPresenceService(presenceStream, presenceStream)
	if err != nil {
		panic(err)
	}
	tokenService, err := service.CreateTokenService()
	if err != nil {
		panic(err)
	}
	eventService, err := service.NewEventService(
		messageStream,
		presenceStream,
		typingStream,
		streamMux,
		messageStream,
		memberStore,
	)
	if err != nil {
		panic(err)
	}
	syncService, err := service.NewSyncService(
		messageStream,
		presenceStream,
		typingStream,
		roomStore,
		memberStore,
	)
	if err != nil {
		panic(err)
	}
	return services{
		roomService,
		userService,
		profileService,
		presenceService,
		tokenService,
		eventService,
		syncService,
	}
}
Exemple #3
0
func setupApiEndpoint() http.Handler {
	stateStore, err := db.NewStateStore()
	if err != nil {
		panic(err)
	}
	roomStore, err := db.NewRoomDb()
	if err != nil {
		panic(err)
	}
	userStore, err := stores.NewUserDb(stateStore)
	if err != nil {
		panic(err)
	}
	aliasCache, err := db.NewIdMap()
	if err != nil {
		panic(err)
	}
	aliasStore, err := stores.NewAliasStore(aliasCache)
	if err != nil {
		panic(err)
	}
	memberCache, err := db.NewIdMultiMap()
	if err != nil {
		panic(err)
	}
	memberStore, err := stores.NewMembershipStore(memberCache)
	if err != nil {
		panic(err)
	}
	streamMux, err := events.NewStreamMux()
	if err != nil {
		panic(err)
	}
	messageStream, err := events.NewMessageStream(memberStore, streamMux)
	if err != nil {
		panic(err)
	}
	presenceStream, err := events.NewPresenceStream(memberStore, streamMux)
	if err != nil {
		panic(err)
	}
	typingStream, err := events.NewTypingStream(memberStore, streamMux)
	if err != nil {
		panic(err)
	}

	roomService, err := service.CreateRoomService(
		roomStore,
		aliasStore,
		memberStore,
		messageStream,
		presenceStream,
		typingStream,
		typingStream,
	)
	if err != nil {
		panic(err)
	}
	userService, err := service.CreateUserService(userStore)
	if err != nil {
		panic(err)
	}
	profileService, err := service.NewProfileService(
		presenceStream,
		presenceStream,
		memberStore,
		roomStore,
		messageStream,
	)
	if err != nil {
		panic(err)
	}
	presenceService, err := service.NewPresenceService(presenceStream, presenceStream)
	if err != nil {
		panic(err)
	}
	tokenService, err := service.CreateTokenService()
	if err != nil {
		panic(err)
	}
	eventService, err := service.NewEventService(
		messageStream,
		presenceStream,
		typingStream,
		streamMux,
		messageStream,
		memberStore,
	)
	if err != nil {
		panic(err)
	}
	syncService, err := service.NewSyncService(
		messageStream,
		presenceStream,
		typingStream,
		roomStore,
		memberStore,
	)
	if err != nil {
		panic(err)
	}

	mux := httprouter.New()
	api.NewAuthEndpoint(userService, tokenService).Register(mux)
	api.NewProfileEndpoint(userService, tokenService, profileService).Register(mux)
	api.NewPresenceEndpoint(userService, tokenService, presenceService).Register(mux)
	api.NewRoomsEndpoint(userService, tokenService, roomService, syncService, eventService).Register(mux)
	api.NewEventsEndpoint(userService, tokenService, eventService, syncService).Register(mux)

	mux.NotFound = http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		api.WriteJsonResponseWithStatus(rw, types.DefaultUnrecognizedError)
	})

	mux.OPTIONS("/*path", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
	})

	corsHandler := http.NewServeMux()
	corsHandler.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
		rw.Header().Set("Access-Control-Allow-Origin", "*")
		rw.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE")
		rw.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding")
		mux.ServeHTTP(rw, req)
	})

	return corsHandler
}