Java Code Examples for org.rapidoid.setup.App

The following examples show how to use org.rapidoid.setup.App. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: apollo   Source File: RestServer.java    License: Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void start() {
    if (configuration.getApi().isDisableApiServer()) {
        logger.info("Not starting the API...");
        return;
    }

    registerLoginProvider();
    registerRolesProvider();

    ApiConfiguration apiConfiguration = configuration.getApi();
    String[] args = new String[] {
            "secret=" + apiConfiguration.getSecret(),
            "on.address=" + apiConfiguration.getListen(),
            "on.port=" + apiConfiguration.getPort()
    };

    // Initialize the REST API server
    On.changes().ignore();
    GuiceBeans beans = Integrate.guice(injector);
    App.run(args).auth();
    App.register(beans);
}
 
Example 2
Source Project: rapidoid   Source File: GuiceIntegrationExample.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	Log.info("Starting application");

	// disable Rapidoid's hot class reloading, it doesn't play well with Guice
	On.changes().ignore();

	GuiceBeans beans = Integrate.guice(new WebModule());
	App.register(beans);

	// test the RESTful service
	Self.get("/add?x=6&y=4").print();
	Self.get("/add?x=1&y=22").expect().entry("sum", 23);

	// usually you wouldn't shutdown the application
	App.shutdown();
}
 
Example 3
Source Project: rapidoid   Source File: Main.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	App.run(args);

	On.get("/hello").plain((req, resp) -> {

		req.async(); // mark asynchronous request processing

		// send part 1
		resp.chunk("part 1".getBytes());

		// after some time, send part 2 and finish
		Jobs.after(100).milliseconds(() -> {
			resp.chunk(" & part 2".getBytes());
			resp.done();
		});

		return resp;
	});
}
 
Example 4
Source Project: rapidoid   Source File: Main.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

		// first thing to do - initializing Rapidoid, without bootstrapping anything at the moment
		App.run(args); // instead of App.bootstrap(args), which might start the server

		// customizing the server address and port - before the server is bootstrapped
		On.address("0.0.0.0").port(9998);
		Admin.address("127.0.0.1").port(9999);

		// fine-tuning the HTTP server
		Conf.HTTP.set("maxPipeline", 32);
		Conf.NET.set("bufSizeKB", 16);

		// now bootstrap some components, e.g. classpath scanning (beans)
		App.scan();

		Boot.jmx()
			.adminCenter();

		// continue with normal setup
		On.get("/x").json("x");
	}
 
Example 5
Source Project: rapidoid   Source File: JPAInjectionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testJPAInjection() {
	JPA.bootstrap(path());
	App.path(path());
	App.scan();

	postData("/books?title=a", U.map("title", "My Book 1"));
	postData("/books?title=b", U.map("title", "My Book 2"));
	postData("/books?title=c", U.map("title", "My Book 3"));

	onlyGet("/allBooks");

	onlyGet("/del?id=1");
	getAndPost("/del2?id=2");
	onlyPost("/del3?id=3");
	onlyPost("/del4?id=3");

	onlyGet("/allBooks?finally");
}
 
Example 6
Source Project: rapidoid   Source File: HttpTestCommons.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void openContext() {
	Msc.reset();

	ClasspathUtil.setRootPackage("some.nonexisting.app");

	JPAUtil.reset();
	Conf.ROOT.setPath(getTestNamespace());
	IoC.reset();

	App.resetGlobalState();
	On.changes().ignore();

	On.setup().activate();
	On.setup().reload();

	verifyNoRoutes();
}
 
Example 7
Source Project: rapidoid   Source File: HttpPojoControllerTest.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testPojoHandlersWithIoC() {
	notFound("/b");

	List<String> ctrls = Scan.annotated(MyTestController.class).in(U.list("pkg1", "pkg2")).getAll();
	isTrue(ctrls.isEmpty());

	List<String> ctrls2 = Scan.annotated(MyTestController.class, Generated.class).in(App.path()).getAll();
	eq(ctrls2, U.list(Ff.class.getName()));

	Scan.annotated(MyTestController.class, MyTestController.class).in(App.path()).forEach(cls -> App.beans(IoC.singleton(cls)));

	onlyGet("/b");
	onlyGet("/x");
	notFound("/x");
}
 
Example 8
Source Project: rapidoid   Source File: OverviewHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object call() {
	List<Object> info = U.list();

	info.add(h3(center("Basic overview:")));

	Map<String, Object> appInfo = U.map();

	if (!Msc.isPlatform()) {
		appInfo.put("Application JAR", ClasspathUtil.appJar());
		appInfo.put("Application path (root packages)", App.path());
	}

	appInfo.put("Active profiles", Env.profiles());
	appInfo.put("Command line arguments", Env.args());

	info.add(grid(appInfo));

	info.add(h3(center("System metrics:")));
	info.add(GraphsHandler.graphs(3));

	return multi(info);
}
 
Example 9
Source Project: rapidoid   Source File: HttpBeanParamsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@ExpectErrors
public void testBeanParams() {
	App.beans(new Object() {

		@GET
		@SuppressWarnings("unchecked")
		public List<Object> pers(String name, Person person, int id, Integer xx) {
			return U.list(id, name, person, xx);
		}

	});

	onlyGet("/pers?name=Einstein&id=1000");
	onlyGet("/pers?name=Mozart");
	onlyGet("/pers?id=200");
}
 
Example 10
Source Project: rapidoid   Source File: HttpModule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void cleanUp() {
	My.reset();
	App.resetGlobalState();
	On.changes().ignore();

	Setups.clear();

	Env.reset();
}
 
Example 11
Source Project: rapidoid   Source File: HttpMainEntryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSequentialControllerRegistration() {
	App.path(path());
	App.scan();
	On.get("/a").plain("A");

	onlyGet("/a");
	onlyGet("/b");
}
 
Example 12
Source Project: rapidoid   Source File: Main.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	App.bootstrap(args);

	On.get("/a*").json(Req::uri);

	Reverse.proxy("/g")
		.roles("administrator")
		.cacheTTL(1000)
		.to("http://upstream1:8080", "http://upstream2:8080")
		.add();
}
 
Example 13
public static void main(String[] args) {
	App.run(args);

	Conf.HTTP.set("maxPipeline", 128);
	Conf.HTTP.set("timeout", 0);

	new PlaintextAndJsonServer().listen(8080);
}
 
Example 14
Source Project: rapidoid   Source File: Main.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	App.gui()
		.brand("Cool app!")
		.title("the head title")
		.search(true)
		.addMenuItem("/", 10, "Home")
		.addMenuItem("/portfolio", 20, "Portfolio")
		.addSubMenuItem("/about", 30, "About", 1, "About Us")
		.addSubMenuItem("/", 30, "About", 2, "About You");

	/* On [GET /hi] or [POST /hi] return a "Hello World" web page */

	On.page("/hi").mvc("Hello <b>world</b>!");
}
 
Example 15
Source Project: rapidoid   Source File: Main.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	App.run(args);

	Setup setup1 = Setups.create("foo").port(2222);
	Setup setup2 = Setups.create("bar").port(3333);

	setup1.scan(FooSetupCtrl.class.getPackage().getName());
	setup2.scan(BarSetupCtrl.class.getPackage().getName());

	// demo
	HTTP.get("localhost:2222/foo").print();
	HTTP.get("localhost:3333/bar").print();
}
 
Example 16
Source Project: rapidoid   Source File: Main.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	App.bootstrap(args);

	LoadBalancer lb = (Req req, List<ProxyUpstream> candidates) -> {
		int index = 0; // FIXME implement load balancing strategy
		return candidates.get(index);
	};

	Reverse.proxy("/")
		.to("http://upstream1:8080", "http://upstream2:8080")
		.loadBalancer(lb)
		.add();
}
 
Example 17
Source Project: rapidoid   Source File: Main.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	App.bootstrap(args);

	String fooUpstream = "localhost:8080/foo";

	Reverse.proxy("/bar").to(fooUpstream).add();
	Reverse.proxy("/").to(fooUpstream).add();

	On.get("/foo").html("FOO");
	On.get("/foo/hi").html("FOO HI");
	On.get("/foo/hello").html("FOO HELLO");
	On.get("/bar/hi").html("BAR HI");
}
 
Example 18
Source Project: rapidoid   Source File: HttpBeanValidationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testValidation() {
	App.path("org.rapidoid.validation");
	App.bootstrap(new String[0]);
	JPA.bootstrap(App.path());

	onlyGet("/echo?num=123");
	onlyGet("/echo");

	onlyGet("/validating?num=123");
	onlyGet("/validating");

	onlyPost("/save?num=123");
	onlyPost("/save");
}
 
Example 19
Source Project: rapidoid   Source File: WebShutdownTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void step2() {
	App.run(new String[0]);

	On.get("/x").plain("X");

	onlyGet("/x");
}
 
Example 20
Source Project: rapidoid   Source File: GuiceIntegrationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGuiceIntegration() {
	Beans beans = Integrate.guice(new MathModule());
	App.register(beans);

	Self.get("/add?x=6&y=4").expect().entry("sum", 10);
	Self.get("/add?x=1&y=22").expect().entry("sum", 23);
}
 
Example 21
Source Project: rapidoid   Source File: HttpRecursiveMainEntryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSequentialControllerRegistration() {
	App.path(path());

	App.scan();
	On.get("/a").plain("A");

	onlyGet("/a");
	onlyGet("/b");
	onlyGet("/c");
}
 
Example 22
Source Project: rapidoid   Source File: HttpTemplatesPathTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTemplatesPath1() {
	App.custom().templatesPath("test-templates");

	Templates.setPath("something-different");
	eq(Templates.getPath(), U.array("something-different"));

	setupAndTest();
}
 
Example 23
Source Project: rapidoid   Source File: HttpStaticFilesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void serveStaticFiles() {
	App.custom().staticFilesPath("static1", "non-existing-location", "static2");

	On.get("/c").managed(false).contentType(MediaType.JSON).serve("override");

	onlyGet("/"); // home page
	onlyGet("/index"); // home page
	onlyGet("/index.html"); // home page

	onlyGet("/a");
	onlyGet("/a.html");

	onlyGet("/b");
	onlyGet("/c");

	onlyGet("/dir1/sub1.txt");

	// no private files (starting with '.')
	notFound("/dir1/.sub2.txt");
	notFound("/.priv.txt");

	// no folders
	Res dir1 = Res.from("dir1", "static2");
	isFalse(dir1.exists());
	notFound("/dir1");

	notFound("/xx");
	notFound("/page1");
	notFound("/page2");
}
 
Example 24
Source Project: rapidoid   Source File: HttpPojoControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testPojoHandlers() {
	App.beans(new Object() {

		@GET(uri = "/a")
		public Object theFoo() {
			return "foo";
		}

		@POST(uri = "/x")
		public Object x(Req req, Resp resp) {
			return "x";
		}
	});

	onlyGet("/a");
	onlyPost("/x");
	notFound("/b");

	List<String> ctrls = Scan.annotated(MyTestController.class).in("pkg1", "pkg2").getAll();
	isTrue(ctrls.isEmpty());

	List<String> ctrls2 = Scan.annotated(MyTestController.class).in("non-existing-pkg", "").getAll();
	eq(ctrls2, U.list(Ff.class.getName()));

	Scan.annotated(MyTestController.class, MyTestController.class).in(App.path()).forEach(App::beans);

	onlyGet("/a");
	onlyGet("/b");
	onlyPost("/x");
}
 
Example 25
public static void main(String[] args) {
	App.run(args);

	Conf.HTTP.set("maxPipeline", 128);
	Conf.HTTP.set("timeout", 0);
	Conf.HTTP.sub("mandatoryHeaders").set("connection", false);

	On.port(8080);

	if (Env.hasAnyProfile("mysql", "postgres")) {
		setupDbHandlers();
	} else {
		setupSimpleHandlers();
	}
}
 
Example 26
Source Project: rapidoid   Source File: HttpReregistrationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerDeregistration() {
	Object ctrl1 = ctrl1("next");
	Object ctrl2 = ctrl2();

	notFound("/inc");
	notFound("/dec");

	App.beans(ctrl1);
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=5");
	notFound("/dec");

	App.setup().deregister(ctrl1);
	verifyNoRoutes();

	App.beans(ctrl2);
	verifyRoutes("ctrl2");

	onlyPost("/dec?x=12");
	notFound("/inc");

	App.setup().deregister(ctrl2.getClass());
	verifyNoRoutes();

	notFound("/inc");
	notFound("/dec");
}
 
Example 27
Source Project: rapidoid   Source File: HttpReregistrationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerReregistration() {
	notFound("/inc");
	notFound("/dec");

	App.beans(ctrl1("nextA"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=100");

	App.beans(ctrl1("nextB"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=200");

	App.beans(ctrl1("nextC"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=300");

	// can deregister with other instance, only the class matters for deregistration, not the instance
	App.setup().deregister(ctrl1("invisible"));
	verifyNoRoutes();

	notFound("/inc");
	notFound("/dec");
}
 
Example 28
Source Project: rapidoid   Source File: HttpReregistrationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLambdaDeregistration() {
	notFound("/foo");

	On.page("/foo").html((Req req, Integer x) -> req.data() + ":" + x);
	verifyRoutes("foo");

	getAndPost("/foo?a=12&x=3");

	App.setup().deregister("GET,POST", "/foo");
	verifyNoRoutes();

	notFound("/foo");
}
 
Example 29
Source Project: rapidoid   Source File: HttpWrapTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWrap() {
	App.defaults().wrappers((req, invocation) -> invocation.invokeAndTransformResult(x -> x + "!"));
	App.defaults().contentType(MediaType.BINARY);

	On.get("/").html(() -> "a");

	onlyGet("/");
}
 
Example 30
Source Project: rapidoid   Source File: HttpErrorHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorHandler1() {
	App.custom().errorHandler((req, resp, e) -> {
		if (e instanceof NotFound)
			return Defaults.errorHandler().handleError(req, resp, e); // default error processing
		return req + ":err:" + e;
	});

	On.get("/err").html((ReqHandler) req -> {
		String s = null;
		return s.toString(); // NPE
	});

	onlyGet("/err?x=1");
}