Added a few things and fixed some others
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
@@ -5,12 +5,14 @@
|
||||
"items": [
|
||||
{ "text": "Getting Started", "link": "/flash" },
|
||||
{ "text": "Installation", "link": "/flash/installation" },
|
||||
{ "text": "Handlers", "link": "/flash/handlers" },
|
||||
{ "text": "RequestHandler", "link": "/flash/request-handler"},
|
||||
{ "text": "Request and Response", "link": "/flash/request-response"},
|
||||
{ "text": "Server Router", "link": "/flash/server-router" },
|
||||
{ "text": "Handler Default Implementations", "link": "/flash/handler-default-implementations" },
|
||||
{ "text": "Websockets", "link": "/flash/websockets" },
|
||||
{ "text": "Static Files Server", "link": "/flash/static-file-server" }
|
||||
{ "text": "Static Files Server", "link": "/flash/static-file-server" },
|
||||
{ "text": "Dynamic Files Server", "link": "/flash/dynamic-file-server" }
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
---
|
||||
banner_title: "Flash - Dynamic File Server"
|
||||
banner_description: "Learn how to serve files in a dynamic context."
|
||||
---
|
||||
|
||||
# 📁 Dynamic File Server
|
||||
|
||||
Sometimes we need to serve files in a dynamic context, in this sense a static file server is limiting.
|
||||
Imagine we have a frontend application that is compiled down to a single `index.html` file, and we want to serve it with Flash
|
||||
alongside it's javascript and css bundles. The way these compiled applications work is that they rely heavily on
|
||||
client-side routing, so when the user navigates to a different page, the frontend application will try to fetch the
|
||||
corresponding file from the server. This is where a dynamic file server comes in handy, as no route is pre-registered.
|
||||
|
||||
::: warning
|
||||
The dynamic file server relies heavily on the concept of dynamic handlers, which are handlers that will resolve for
|
||||
any subpath of the endpoint they are registered to (see [Handler Types](/flash/dynamic-handlers) for more info).
|
||||
:::
|
||||
|
||||
## Usage
|
||||
|
||||
To serve static files in Flash, you need to call the `server.serveDynamic()` method with the endpoint path and an instance of `DynamicFileServerConfig`.
|
||||
The configuration object is instanced like so :
|
||||
|
||||
```java
|
||||
DynamicFileServerConfiguration(
|
||||
boolean enableFileWatcher,
|
||||
String destinationPath,
|
||||
String dynamicEntrypoint,
|
||||
SourceType sourceType
|
||||
)
|
||||
```
|
||||
|
||||
- `enableFileWatcher` : If set to `true`, the server will watch for changes in the served files and reload them automatically.
|
||||
- `destinationPath` : The path to the directory containing the files to be served.
|
||||
- `dynamicEntrypoint` : The path to the file that will be served when the client navigates to the endpoint eg. `index.html`.
|
||||
- `sourceType` : The type of source to serve files from. It can be either `FILESYSTEM` or `RESOURCESTREAM`.
|
||||
|
||||
Registering the dynamic file server is as simple as calling the `server.serveDynamic()` method with the desired path and configuration object:
|
||||
|
||||
```java
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
FlashServer server = new FlashServer(8080);
|
||||
|
||||
server.serveDynamic("/*", new DynamicFileServerConfiguration(
|
||||
true,
|
||||
"path/to/my/files",
|
||||
"index.html",
|
||||
SourceType.FILESYSTEM
|
||||
));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now you can access the files (or frontend) in the specified directory by navigating to `http://localhost:8080/<file-name>`.
|
||||
|
||||
Similarly, you can serve the same content from the jar's resources folder by setting the `sourceType` to `RESOURCESTREAM`:
|
||||
|
||||
```java
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
FlashServer server = new FlashServer(8080);
|
||||
|
||||
server.serveStatic("/static", new DynamicFileServerConfiguration(
|
||||
true,
|
||||
"path/to/my/files",
|
||||
"index.html",
|
||||
SourceType.RESOURCESTREAM
|
||||
));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 💻 Note on Fullstack Development
|
||||
|
||||

|
||||
|
||||
The dynamic file server opens up the possibility for fullstack development with Flash.
|
||||
You can easily serve your frontend application alongside your backend, and build the entire application in a single jarfile.
|
||||
|
||||
This is possible thanks to the `RESOURCESTREAM` source type, which allows you to serve files from the jar's resources folder:
|
||||
the ideal cycle for packaging your app would be to compile the frontend application and place the compiled files in the resources folder (or setup the build script to do it for you),
|
||||
then build the jarfile with the compiled frontend and the backend code.
|
||||
|
||||
This approach works for any frontend framework that can compile down to static files (React, Angular, Vue, etc.).
|
||||
|
||||
For more in-depth information on how to build a fullstack application with Flash, check out the [Fullstack Development](/flash/fullstack-development) guide.
|
||||
|
||||
@@ -30,6 +30,7 @@ public abstract class APIKeyProtectedHandler extends RequestHandler {
|
||||
|
||||
if (apiKey == null || !isValidApiKey(apiKey)) {
|
||||
res.status(403);
|
||||
res.type("application/json");
|
||||
return "{\"error\":\"Invalid API Key\"}";
|
||||
}
|
||||
|
||||
@@ -58,6 +59,7 @@ public class GetDataHandler extends APIKeyProtectedHandler {
|
||||
|
||||
@Override
|
||||
protected Object handleAuthorized() {
|
||||
res.type("application/json");
|
||||
return "{\"data\":\"Your API response here\"}";
|
||||
}
|
||||
}
|
||||
@@ -109,6 +111,7 @@ public abstract class AuthenticatedHandler extends ProtectedHandler {
|
||||
user = getUserFromDatabase(authToken);
|
||||
if (user == null) {
|
||||
res.status(403);
|
||||
res.type("application/json");
|
||||
return "{\"error\":\"User not found\"}";
|
||||
}
|
||||
return handleWithUser();
|
||||
@@ -132,6 +135,7 @@ public class UserProfileHandler extends AuthenticatedHandler {
|
||||
|
||||
@Override
|
||||
protected Object handleWithUser() {
|
||||
res.type("application/json");
|
||||
return "{\"username\":\"" + username + "\"}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
---
|
||||
banner-title: "Flash - Handlers"
|
||||
banner-description: "Learn about handlers in Flash and the different types available."
|
||||
---
|
||||
|
||||
# 📚 Handlers
|
||||
|
||||
In Flash, handlers are the building blocks of your application logic. They are responsible for processing incoming requests, executing the necessary logic, and generating the appropriate response.
|
||||
|
||||
There are several types of handlers in Flash, each serving a specific purpose and providing a different level of control over the request lifecycle. Understanding the different handler types will help you structure your application logic more effectively and make the most out of Flash's powerful routing system.
|
||||
|
||||
## 📦 Routing Behavior
|
||||
|
||||
Before diving into the different handler types, it's essential to understand how routing works in Flash. When a request is received by the server, Flash matches the request path and method against the registered routes to find the appropriate handler. The handler is then executed, and its response is sent back to the client.
|
||||
|
||||
Flash supports 3 main types of routing behaviors:
|
||||
|
||||
- **Literal Routing**: Matches the exact path specified in the route definition.
|
||||
- **Parametrized Routing**: Matches paths with dynamic segments that are extracted as route parameters.
|
||||
- **Dynamic Routing**: Matches any path that starts with the specified prefix and is flagged with a wildcard "*" character.
|
||||
|
||||
## 📌 Handler Types
|
||||
|
||||
### 1. RequestHandler
|
||||
|
||||
The `RequestHandler` is the "standard" type of handler in Flash, it provides the most control over the request lifecycle and allows you to define custom logic for handling requests.
|
||||
You can extend the `RequestHandler` class to create custom handlers that process incoming requests and generate responses.
|
||||
|
||||
Because `RequestHandler` is an abstract class, you need to implement both the `handle()` method and the `super` constructor in your custom handler to define the logic that should be executed
|
||||
when a request is received.
|
||||
|
||||
Since `RequestHandler` is an abstract class, you can leverage and chain HDI's to create cleaner and more maintainable route logic (more on that in the [Handler Default Implementations](/flash/handler-default-implementations) section).
|
||||
|
||||
### 2. SimpleHandler
|
||||
|
||||
The `SimpleHandler` is a lightweight handler that allows you to define request handling logic in a single method using lambda notation.
|
||||
It is useful for simple request processing tasks that don't require the full lifecycle control provided by `RequestHandler`.
|
||||
|
||||
To create a `SimpleHandler`, you can use the `server.get()`, `server.post()`, `server.put()`, `server.delete()` etc.
|
||||
in general, you can use the `server.<METHOD>()` methods to register the handler with the server.
|
||||
|
||||
The arguments for these methods are the route path and a lambda expression that provides
|
||||
the request and response objects and defines the request handling logic.
|
||||
|
||||
```java[Example]
|
||||
server.get("/hello", (req, res) -> {
|
||||
return "Hello, World!";
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Both `RequestHandler` and `SimpleHandler` can specify the router behavior
|
||||
by the naming convention of the endpoint used to register the handler.
|
||||
|
||||
- Literal Routing: `/hello` <br>
|
||||
Will match exactly `/hello`
|
||||
|
||||
- Parametrized Routing: `/hello/:name` <br>
|
||||
Will match `/hello/John`, `/hello/Alice`, etc.
|
||||
|
||||
- Dynamic Routing: `/hello/*` <br>
|
||||
Will match `/hello/../..`
|
||||
@@ -6,6 +6,7 @@ banner_description: "Learn how to create and manage Request Handlers in Flash."
|
||||
# ⚙️ Request Handler
|
||||
|
||||
In this section, we illustrate the powerful concept of `RequestHandler` in Flash, which are used to handle incoming requests and generate responses.
|
||||
RequestHandler classes provide the most control over the request lifecycle and allow you to use routing, expected operators and HDI's to create custom logic for handling requests.
|
||||
|
||||
## Creating a Request Handler
|
||||
|
||||
@@ -18,7 +19,7 @@ The `req` (request) and `res` (response) objects are available in the handler to
|
||||
You must call the super constructor with the `req` and `res` objects to initialize the handler.
|
||||
|
||||
```java{1,4}
|
||||
@RouteInfo(method = HttpMethod.GET, path = "/hello")
|
||||
@RouteInfo(endpoint="/hello", method = HttpMethod.GET)
|
||||
public class MyHandler extends RequestHandler {
|
||||
public MyHandler(Request req, Response res) {
|
||||
super(req, res);
|
||||
|
||||
@@ -16,7 +16,7 @@ To access the router instance, you can call the `route()` method on the `FlashSe
|
||||
To create a route, you need to call the `route()` method on your server's instance (in this case for simplicity, on the InternalFlashServer)
|
||||
and specify the base path of the route, followed by your handler class,
|
||||
|
||||
```java{9}
|
||||
```java{6}
|
||||
// Example.java
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
@@ -30,11 +30,8 @@ public class Example {
|
||||
}
|
||||
```
|
||||
|
||||
```java{6}
|
||||
```java{3}
|
||||
// MyHandler.java
|
||||
import flash.Request;
|
||||
import flash.Response;
|
||||
import flash.models.RequestHandler;
|
||||
|
||||
@RouteInfo(method = HttpMethod.GET, path = "/hello")
|
||||
public class MyHandler extends RequestHandler {
|
||||
@@ -50,8 +47,8 @@ public class MyHandler extends RequestHandler {
|
||||
}
|
||||
```
|
||||
|
||||
In the example above, we create a route `/api` and register the `MyHandler` class to handle requests on the `/api/hello` endpoint.
|
||||
In the example above, we create an `/api` router and register the `MyHandler` class to handle requests on the `/api/hello` endpoint.
|
||||
|
||||
This is because the `path` property of the `RouteInfo` annotation is relative to the base path of the route, which in this case is `/api`.
|
||||
This is because the `path` property of the `RouteInfo` annotation is relative to the base path of the router, which in this case is `/api`.
|
||||
|
||||
Visiting `/api/hello` from your browser will result in the response `Hello, world!`.
|
||||
Reference in New Issue
Block a user