-
Notifications
You must be signed in to change notification settings - Fork 16
Static Resources Bld
RIFE2's main intent is to generate content and responses to interactions for dynamic web application resources.
Most web applications are however a combination of dynamic resources and static resources like images, CSS stylesheets, JavaScript files, fonts, videos, audio, ... Since RIFE2 runs inside regular Java servlet containers like Jetty or Tomcat, you can leverage the static file handling that is already implemented.
The project structure that RIFE2's bld
has created is already set up to
provide your webapp resources alongside your code. It knows how to include these
inside the war archive for deployment. You're free to organize any static file
you need inside the webapp
directory.
For instance:
└── src/
├── bld/
├── main/
│ ├── java/
│ ├── resources/
│ └── webapp/
│ ├── css/
│ │ └── main.css
│ ├── favicon.ico
│ ├── images/
│ │ ├── logo.png
│ │ └── photo.jpg
│ ├── js/
│ │ └── main.js
│ ├── robots.txt
│ └── WEB-INF/
│ └── web.xml
└── test/
├── java/
└── resources/
All that's now left to do, is to tell the RIFE2 embedded web server to use that same directory when you run your application that way:
public class AppSite extends Site {
// ...
public static void main(String[] args) {
new Server()
.staticResourceBase("src/main/webapp")
.start(new AppSite());
}
}
The reason why the approach above works, is that RIFE2 is set up as a filter and automatically passes through any request that matches a collection of suffixes.
This is the default list:
aac, arj, avi, bmp, class, css, flac, gif, gz, htc, htm, html, ico, jar, jpeg, jpg, js, m4a, mov, mp3, mp4, mpg, ogg, otf, png, svg, swf, ttf, txt, wav, wma, z, zip
If you need to add more types or remove some, you can tweak this list yourself
by using RifeConfig.engine().setPassThroughSuffixes()
When you choose to deploy your application in an UberJar, you'll want to include
all the static resources also. The bld
project already does this as long
as you put them in the standard webapp
directory.
In order to have the server look those resource up from the UberJar, I recommend
that you create a dedicated main class, called AppSiteUber
. You can tell the
bld
about it by editing src/bld/java/hello/AppBuild.java
as such:
public class AppBuild extends WebProject {
public AppBuild() {
// ...
uberJarMainClass = "hello.AppSiteUber";
// ...
}
// public static void main ...
}
The dedicated main class allows you to continue to run your application in
development mode with bld
and also have the embedded Jetty container find the
static resources when they're included in a jar, you'll just have to tell RIFE2
that the static resource base is different.
The src/main/java/hello/AppSiteUber.java
file extends your existing site and
merely provides a main method that is configured for UberJar deployment:
package hello;
import rife.engine.Server;
public class AppSiteUber extends AppSite {
public static void main(String[] args) {
new Server()
.staticUberJarResourceBase("webapp")
.start(new AppSiteUber());
}
}
Since the UberJar is likely intended for production deployment, you can further configure the Jetty server in this class also, for instance by specifying a hostname, port number, configuring the thread pool or even set up SSL.
Next learn more about Routing