Created
March 7, 2016 16:22
-
-
Save childnode/3af9787dc865156c46eb to your computer and use it in GitHub Desktop.
Example Ratpack Server start chain with guice registry and
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
package de.childno.example.ratpack; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Inject; | |
import com.google.inject.Provides; | |
public class ServiceModule extends AbstractModule { | |
@Override | |
protected void configure() { } | |
@Provides | |
@Inject | |
IService provideService() { | |
return new ServiceImpl(); | |
} | |
} |
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
package de.childno.example.ratpack; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Inject; | |
import com.google.inject.Provides; | |
public class SimServiceModule extends AbstractModule { | |
@Override | |
protected void configure() { } | |
@Provides | |
@Inject | |
IService provideService() { | |
return new SimServiceModule(); | |
} | |
} |
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
package de.childno.example.ratpack; | |
import com.google.inject.Module; | |
import com.google.inject.util.Modules; | |
import ratpack.guice.BindingsSpec; | |
import ratpack.guice.Guice; | |
import ratpack.handling.Chain; | |
import ratpack.server.BaseDir; | |
import ratpack.server.RatpackServer; | |
import ratpack.server.ServerConfigBuilder; | |
import java.util.List; | |
import static java.util.Arrays.asList; | |
public class WsServerApp { | |
private static final boolean SIMULATION = Boolean.getBoolean("useSimulation"); | |
public static void main(String[] args) throws Exception { | |
RatpackServer.start(server -> server | |
.serverConfig(WsServerApp::config) | |
.registry(Guice.registry(WsServerApp::bindModules)) | |
.handlers(chain -> chain | |
.get("hello", Hello.class) | |
.all(ctx -> ctx.redirect(301, "/hello")) | |
) | |
); | |
} | |
static ServerConfigBuilder config(ServerConfigBuilder config) { | |
return config | |
.baseDir(BaseDir.find()) | |
.env(); | |
} | |
private static List<Module> productionModules() { | |
return asList( | |
new HelloModule(), | |
new ServiceModule()); | |
} | |
private static List<Module> simulationModules() { | |
return asList( | |
new SimServiceModule()); | |
} | |
static BindingsSpec bindModules(BindingsSpec bindingsSpec) | |
{ | |
Module modulesToBeUsed = Modules.combine(productionModules()); | |
if (SIMULATION) { | |
modulesToBeUsed = Modules.override(modulesToBeUsed).with(simulationModules()); | |
} | |
return bindingsSpec.module(modulesToBeUsed); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment