From 8912f7e738dc3e6f07a2b0adc0403e4bcb5c47ab Mon Sep 17 00:00:00 2001 From: Relism Date: Thu, 27 Feb 2025 22:44:18 +0100 Subject: [PATCH] Refactoring and additions --- flash/advanced/fullstack-development.md | 121 ++++++++++++++++++ .../handler-default-implementations.md | 0 flash/config/sidebar.json | 40 ++++-- flash/{ => core-concepts}/handlers.md | 0 flash/{ => core-concepts}/request-handler.md | 0 flash/{ => core-concepts}/request-response.md | 0 flash/{ => core-concepts}/server-router.md | 0 flash/{ => core-concepts}/websockets.md | 0 .../{ => file-serving}/dynamic-file-server.md | 17 +-- .../{ => file-serving}/static-file-server.md | 0 flash/{ => introduction}/installation.md | 0 11 files changed, 152 insertions(+), 26 deletions(-) create mode 100644 flash/advanced/fullstack-development.md rename flash/{ => advanced}/handler-default-implementations.md (100%) rename flash/{ => core-concepts}/handlers.md (100%) rename flash/{ => core-concepts}/request-handler.md (100%) rename flash/{ => core-concepts}/request-response.md (100%) rename flash/{ => core-concepts}/server-router.md (100%) rename flash/{ => core-concepts}/websockets.md (100%) rename flash/{ => file-serving}/dynamic-file-server.md (73%) rename flash/{ => file-serving}/static-file-server.md (100%) rename flash/{ => introduction}/installation.md (100%) diff --git a/flash/advanced/fullstack-development.md b/flash/advanced/fullstack-development.md new file mode 100644 index 0000000..13803c3 --- /dev/null +++ b/flash/advanced/fullstack-development.md @@ -0,0 +1,121 @@ +--- +banner_title: "Flash - Fullstack Development" +banner_description: "Build fullstack apps with Flash, frontend and backend in one jar." +--- + +# 🌐 Fullstack Development with Flash + +Fullstack development involves building both the **front-end** (user interface) and **back-end** (server logic, database) of a web application. A fullstack developer is responsible for the entire application, ensuring a smooth connection between the two layers. + +Popular fullstack frameworks include: +- [Ruby on Rails](https://rubyonrails.org) (Ruby) +- [Django](https://www.djangoproject.com) (Python) +- [Laravel](https://laravel.com) (PHP) + +## ⚑ Fullstack Development with Flash + +Developing and packaging a fullstack application can be complex, but **Flash** simplifies the process with built-in tools and **quality-of-life features**. With Flash, you can: +- Serve both **frontend** and **backend** from a single application. +- Bundle everything into a **single JAR file** for easy deployment. +- Leverage HDIs for **clean, modular and maintainable route logic**. + +![Flash Fullstack Development](../assets/flash-fullstack.png) + +## πŸš€ Serving Frontend with Flash + +Flash’s **dynamic file server** enables seamless fullstack development by allowing you to serve frontend assets alongside backend logic. This is made possible by the `RESOURCESTREAM` source type, which serves files from the JAR’s resources folder. + +### βœ… Deployment Workflow +The recommended workflow for packaging a fullstack Flash application: +1. **Compile** the frontend application (React, Vue, Angular, etc.). +2. **Place** the compiled files inside the `resources` folder. +3. **Build** the JAR file with both frontend and backend code. +4. **Deploy** the JAR, serving both frontend and backend seamlessly. + +This approach works for any frontend framework that compiles to static files, such as: +- **React** (`npm run build`) +- **Vue.js** (`npm run build`) +- **Angular** (`ng build --prod`) + +## ❔ Setting up a Fullstack Flash Application + +To get started with fullstack development using Flash, you will first need to choose a frontend framework and set up the build process to compile the frontend assets. +This guide will cover only some of the most popular javascript frontend build tools, but the process is similar for others. + +### πŸ› οΈ Setting up the Frontend Build Process + +:::warning +When building a frontend application with a javascript framework that will be served from a subdirectory (e.g., `/app`), you need to specify the homepage in the `package.json` file. This ensures that the frontend assets are correctly loaded from the subdirectory. +Using a subdirectory and forgetting to set the homepage will result in broken asset links and a potentially non-functional frontend. +```json +{ + "homepage": "/your-subdirectory" +} +``` +::: + +- `Vite` : + ::: details Click to expand + 1. Install Vite on your project if it's not already installed or scaffold a new Vite project (follow the [official guide](https://vite.dev/guide/#scaffolding-your-first-vite-project)). + 2. Edit the `vite.config.js` or `vite.config.ts` build section to output the compiled files to the `resources/frontend` folder of your Flash project (see example below). + ```typescript + // ... + import path from 'path' // [!code ++] + export default defineConfig({ + // ... + + build: { // [!code ++] + outDir: path.resolve(__dirname, '..path/to/your/src/main/resources/frontend'), // [!code ++] + emptyOutDir: true // [!code ++] + }, // [!code ++] + }); + ``` + 3. The output of your Vite build will be placed in the `resources/frontend` folder of your Flash project, ready to be packaged into the JAR file. + ::: + +- `Parcel` + ::: details Click to expand + 1. Install Parcel on your project if it's not already installed or scaffold a new Parcel project (follow the [official guide](https://parceljs.org/getting-started/webapp/)). + 2. Edit the Parcel build command to output the compiled files to the `resources/frontend` folder of your Flash project (see example below). + ```json + { + "scripts": { + "build": "parcel build src/index.html --out-dir ../path/to/your/src/main/resources/frontend" + } + } + ``` + 3. The output of your Parcel build will be placed in the `resources/frontend` folder of your Flash project, ready to be packaged into the JAR file. + ::: + +### πŸ›œ Serve the Frontend with Flash + +Once you have compiled the frontend assets and placed them in the `resources/frontend` folder, you can serve them using Flash's dynamic file server. This server will serve the frontend assets from the JAR's resources folder. +::: info +NOTE: The following example is assuming you plan to serve the frontend from the root path (`/`). +If you plan to serve the frontend from a subdirectory (e.g., `/app`), you will need to adjust the endpoint path accordingly by adding a trailing `/*`. +::: + +```java +public class Example { + public static void main(String[] args) { + FlashServer server = new FlashServer(8080); + + server.serveDynamic("/*", new DynamicFileServerConfiguration( + true, + "frontend", // points to the resources/frontend folder + "index.html", + SourceType.RESOURCESTREAM + )); + + server.start(); + } +} +``` + +### πŸš€ Package and Deploy your app! + +With the frontend and backend code in place, you can now build the JAR file and deploy it to your server. The JAR file will contain both the frontend and backend code, making it easy to deploy and run your fullstack application. + +All you've left to do is run the jarfile on any machine that has Java installed, and your fullstack application will be up and running! + + diff --git a/flash/handler-default-implementations.md b/flash/advanced/handler-default-implementations.md similarity index 100% rename from flash/handler-default-implementations.md rename to flash/advanced/handler-default-implementations.md diff --git a/flash/config/sidebar.json b/flash/config/sidebar.json index 00bfd67..47caa7c 100644 --- a/flash/config/sidebar.json +++ b/flash/config/sidebar.json @@ -1,18 +1,38 @@ { "/flash/": [ { - "text": "Flash Documentation", + "text": "Introduction", + "collapsed": false, "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": "Dynamic Files Server", "link": "/flash/dynamic-file-server" } + { "text": "Installation", "link": "/flash/introduction/installation" } + ] + }, + { + "text": "Core Concepts", + "collapsed": false, + "items": [ + { "text": "Handlers", "link": "/flash/core-concepts/handlers" }, + { "text": "RequestHandler", "link": "/flash/core-concepts/request-handler" }, + { "text": "Request and Response", "link": "/flash/core-concepts/request-response" }, + { "text": "Server Router", "link": "/flash/core-concepts/server-router" }, + { "text": "Websockets", "link": "/flash/core-concepts/websockets" } + ] + }, + { + "text": "File Serving", + "collapsed": false, + "items": [ + { "text": "Static Files Server", "link": "/flash/file-serving/static-file-server" }, + { "text": "Dynamic Files Server", "link": "/flash/file-serving/dynamic-file-server" } + ] + }, + { + "text": "Advanced", + "collapsed": false, + "items": [ + { "text": "Handler Default Implementations", "link": "/flash/advanced/handler-default-implementations" }, + { "text": "Fullstack Development", "link": "/flash/advanced/fullstack-development" } ] } ] diff --git a/flash/handlers.md b/flash/core-concepts/handlers.md similarity index 100% rename from flash/handlers.md rename to flash/core-concepts/handlers.md diff --git a/flash/request-handler.md b/flash/core-concepts/request-handler.md similarity index 100% rename from flash/request-handler.md rename to flash/core-concepts/request-handler.md diff --git a/flash/request-response.md b/flash/core-concepts/request-response.md similarity index 100% rename from flash/request-response.md rename to flash/core-concepts/request-response.md diff --git a/flash/server-router.md b/flash/core-concepts/server-router.md similarity index 100% rename from flash/server-router.md rename to flash/core-concepts/server-router.md diff --git a/flash/websockets.md b/flash/core-concepts/websockets.md similarity index 100% rename from flash/websockets.md rename to flash/core-concepts/websockets.md diff --git a/flash/dynamic-file-server.md b/flash/file-serving/dynamic-file-server.md similarity index 73% rename from flash/dynamic-file-server.md rename to flash/file-serving/dynamic-file-server.md index cb2576e..71cf8e7 100644 --- a/flash/dynamic-file-server.md +++ b/flash/file-serving/dynamic-file-server.md @@ -13,7 +13,7 @@ corresponding file from the server. This is where a dynamic file server comes in ::: 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). +any subpath of the endpoint they are registered to (see [Handler Types](/flash/handlers) for more info). ::: ## Usage @@ -71,18 +71,3 @@ public class Example { } ``` -## πŸ’» Note on Fullstack Development - -![flash-fullstack.png](assets/flash-fullstack.png) - -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. - diff --git a/flash/static-file-server.md b/flash/file-serving/static-file-server.md similarity index 100% rename from flash/static-file-server.md rename to flash/file-serving/static-file-server.md diff --git a/flash/installation.md b/flash/introduction/installation.md similarity index 100% rename from flash/installation.md rename to flash/introduction/installation.md