-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Dagger for Dependency Injection Framework (#118)
Signed-off-by: Alfredo Gutierrez <[email protected]>
- Loading branch information
1 parent
f148832
commit d53758b
Showing
13 changed files
with
360 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
server/src/main/java/com/hedera/block/server/BlockNodeApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.block.server; | ||
|
||
import static java.lang.System.Logger; | ||
import static java.lang.System.Logger.Level.INFO; | ||
|
||
import com.hedera.block.server.config.BlockNodeContext; | ||
import com.hedera.block.server.data.ObjectEvent; | ||
import com.hedera.block.server.health.HealthService; | ||
import com.hedera.block.server.mediator.LiveStreamMediatorBuilder; | ||
import com.hedera.block.server.mediator.StreamMediator; | ||
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig; | ||
import com.hedera.block.server.persistence.storage.read.BlockAsDirReaderBuilder; | ||
import com.hedera.block.server.persistence.storage.read.BlockReader; | ||
import com.hedera.block.server.persistence.storage.write.BlockAsDirWriterBuilder; | ||
import com.hedera.block.server.persistence.storage.write.BlockWriter; | ||
import com.hedera.hapi.block.SubscribeStreamResponse; | ||
import com.hedera.hapi.block.stream.Block; | ||
import com.hedera.hapi.block.stream.BlockItem; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import io.helidon.webserver.WebServer; | ||
import io.helidon.webserver.grpc.GrpcRouting; | ||
import io.helidon.webserver.http.HttpRouting; | ||
import java.io.IOException; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
/** | ||
* The main class for the Block Node application. This class is responsible for starting the server | ||
* and initializing the context. | ||
*/ | ||
@Singleton | ||
public class BlockNodeApp { | ||
|
||
private static final Logger LOGGER = System.getLogger(BlockNodeApp.class.getName()); | ||
|
||
private final ServiceStatus serviceStatus; | ||
private final HealthService healthService; | ||
private final BlockNodeContext blockNodeContext; | ||
|
||
/** | ||
* Has all needed dependencies to start the server and initialize the context. | ||
* | ||
* @param serviceStatus the status of the service | ||
* @param healthService the health service | ||
* @param blockNodeContext the context of the block node | ||
*/ | ||
@Inject | ||
public BlockNodeApp( | ||
@NonNull ServiceStatus serviceStatus, | ||
@NonNull HealthService healthService, | ||
@NonNull BlockNodeContext blockNodeContext) { | ||
this.serviceStatus = serviceStatus; | ||
this.healthService = healthService; | ||
this.blockNodeContext = blockNodeContext; | ||
} | ||
|
||
/** | ||
* Starts the server and binds to the specified port. | ||
* | ||
* @throws IOException if the server cannot be started | ||
*/ | ||
public void start() throws IOException { | ||
|
||
final BlockWriter<BlockItem> blockWriter = | ||
BlockAsDirWriterBuilder.newBuilder(blockNodeContext).build(); | ||
final StreamMediator<BlockItem, ObjectEvent<SubscribeStreamResponse>> streamMediator = | ||
LiveStreamMediatorBuilder.newBuilder(blockWriter, blockNodeContext, serviceStatus) | ||
.build(); | ||
|
||
final BlockReader<Block> blockReader = | ||
BlockAsDirReaderBuilder.newBuilder( | ||
blockNodeContext | ||
.configuration() | ||
.getConfigData(PersistenceStorageConfig.class)) | ||
.build(); | ||
|
||
final BlockStreamService blockStreamService = | ||
buildBlockStreamService( | ||
streamMediator, blockReader, serviceStatus, blockNodeContext); | ||
|
||
final GrpcRouting.Builder grpcRouting = GrpcRouting.builder().service(blockStreamService); | ||
|
||
final HttpRouting.Builder httpRouting = | ||
HttpRouting.builder().register(healthService.getHealthRootPath(), healthService); | ||
|
||
// Build the web server | ||
// TODO: make port server a configurable value. | ||
final WebServer webServer = | ||
WebServer.builder() | ||
.port(8080) | ||
.addRouting(grpcRouting) | ||
.addRouting(httpRouting) | ||
.build(); | ||
|
||
// Update the serviceStatus with the web server | ||
serviceStatus.setWebServer(webServer); | ||
|
||
// Start the web server | ||
webServer.start(); | ||
|
||
// Log the server status | ||
LOGGER.log(INFO, String.format("Block Node Server started at port: %d", webServer.port())); | ||
} | ||
|
||
@NonNull | ||
private static BlockStreamService buildBlockStreamService( | ||
@NonNull | ||
final StreamMediator<BlockItem, ObjectEvent<SubscribeStreamResponse>> | ||
streamMediator, | ||
@NonNull final BlockReader<Block> blockReader, | ||
@NonNull final ServiceStatus serviceStatus, | ||
@NonNull final BlockNodeContext blockNodeContext) { | ||
|
||
return new BlockStreamService(streamMediator, blockReader, serviceStatus, blockNodeContext); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
server/src/main/java/com/hedera/block/server/BlockNodeAppInjectionComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.block.server; | ||
|
||
import com.hedera.block.server.health.HealthInjectionModule; | ||
import dagger.Component; | ||
import javax.inject.Singleton; | ||
|
||
/** The infrastructure used to manage the instances and inject them using Dagger */ | ||
@Singleton | ||
@Component( | ||
modules = { | ||
BlockNodeAppInjectionModule.class, | ||
HealthInjectionModule.class, | ||
}) | ||
public interface BlockNodeAppInjectionComponent { | ||
/** | ||
* Get the block node app server. | ||
* | ||
* @return the block node app server | ||
*/ | ||
BlockNodeApp getBlockNodeApp(); | ||
} |
58 changes: 58 additions & 0 deletions
58
server/src/main/java/com/hedera/block/server/BlockNodeAppInjectionModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.block.server; | ||
|
||
import com.hedera.block.server.config.BlockNodeContext; | ||
import com.hedera.block.server.config.BlockNodeContextFactory; | ||
import dagger.Binds; | ||
import dagger.Module; | ||
import dagger.Provides; | ||
import java.io.IOException; | ||
import javax.inject.Singleton; | ||
|
||
/** | ||
* A Dagger Module for interfaces that are at the BlockNodeApp Level, should be temporary and | ||
* everything should be inside its own modules. | ||
*/ | ||
@Module | ||
public interface BlockNodeAppInjectionModule { | ||
|
||
/** | ||
* Binds the service status to the service status implementation. | ||
* | ||
* @param serviceStatus needs a service status implementation | ||
* @return the service status implementation | ||
*/ | ||
@Singleton | ||
@Binds | ||
ServiceStatus bindServiceStatus(ServiceStatusImpl serviceStatus); | ||
|
||
/** | ||
* Provides a block node context singleton using the factory. | ||
* | ||
* @return a block node context singleton | ||
*/ | ||
@Provides | ||
@Singleton | ||
static BlockNodeContext provideBlockNodeContext() { | ||
try { | ||
return BlockNodeContextFactory.create(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.