Ejemplo n.º 1
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,
	}
}
Ejemplo n.º 2
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
}