Blade Web Framework

repository·v2.1.3·Indexed 26 days ago

https://github.com/lets-blade/blade

A lightweight, high-performance MVC web framework for Java 8+ built on Netty 4. Blade provides an efficient development experience without traditional SSH framework bloat, featuring support for annotation-based controllers, flexible route registration, built-in template rendering, and integrated support for JSON, HTML, and text responses. It includes utilities for parameter mapping, session management, SSL configuration, and basic authentication.

Tokens
8K
Snippets
43
Records
48
Agent score
90%

What's inside Blade

  1. Configure Basic Authentication

    v2.1.3

    Blade provides a BasicAuthMiddleware for simple authentication. To use it, register the middleware and specify credentials in your application.properties file.

    application.properties:

    http.auth.username=admin
    http.auth.password=123456
    Blade.create().use(new BasicAuthMiddleware()).start();
  2. Use the default template engine

    v2.1.3

    By default, Blade looks for template files in the templates directory and uses a built-in template engine. You can pass attributes to the template using ctx.attribute(key, value) and then render the file using ctx.render(templateName).

    // Server setup and template rendering
    public static void main(String[] args) {
        Blade.create().get("/hello", ctx -> {
            ctx.attribute("name", "hellokaton");
            ctx.render("hello.html");
        }).start(Hello.class, args);
    }

    hello.html

    <h1>Hello, ${name}</h1>
  3. Enable HTTP Sessions

    v2.1.3

    Sessions are disabled by default. You can enable them programmatically during application startup or via a configuration file.

    Programmatic approach: Use .http(HttpOptions::enableSession) when creating the Blade instance.

    Configuration approach: Add http.session.enabled=true to your configuration file.

    Blade.create()
         .http(HttpOptions::enableSession)
         .start(Application.class, args);
  4. Install Blade via Maven or Gradle

    v2.1.3

    To use Blade, add the blade-core dependency to your project. Blade is a lightweight web framework based on Java 8 and Netty 4. It does not require a webapp project structure.

    ### Maven
    ```xml
    <dependency>
        <groupId>com.hellokaton</groupId>
        <artifactId>blade-core</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

    Gradle

    compile 'com.hellokaton:blade-core:2.1.2.RELEASE'
  5. Render Templates

    v2.1.3

    Blade uses a built-in template engine by default. Templates are expected to be in the templates directory.

    Default Template Usage

    Use ctx.attribute(key, value) to pass data to the template, then call ctx.render("filename.html").

    Using Jetbrick Template Engine

    To use an alternative engine like Jetbrick, implement the BladeLoader interface and register the engine in the load method.

    // Default usage
    Blade.create().get("/hello", ctx -> {
        ctx.attribute("name", "hellokaton");
        ctx.render("hello.html");
    }).start(Hello.class, args);
    
    // Customizing with Jetbrick
    @Bean
    public class TemplateConfig implements BladeLoader {
        @Override
        public void load(Blade blade) {
            blade.templateEngine(new JetbrickTemplateEngine());
        }
    }
  6. Quick Start: Create a Hello World application

    v2.1.3

    You can start a basic Blade application by creating a Blade instance, defining a route, and calling .start(). By default, the server runs on port 9000.

    public static void main(String[] args) {
        Blade.create().get("/", ctx -> ctx.text("Hello Blade")).start();
    }
  7. Configure SSL

    v2.1.3

    SSL can be configured via application.properties or by providing a custom INettySslCustomizer for advanced Netty-based SSL configurations.

    Via application.properties:

    server.ssl.enable=true
    server.ssl.cert-path=cert.pem
    server.ssl.private-key-path=private_key.pem
    server.ssl.private-key-pass=123456

    Via INettySslCustomizer: Implement INettySslCustomizer and register it using .setNettySslCustomizer(customizer).

    MyNettySslCustomizer nc = new MyNettySslCustomizer();    
    Blade.create()
        .setNettySslCustomizer(nc)
        .start(App.class, args);
  8. Change the Server Port

    v2.1.3

    You can modify the server port using one of three methods:

    1. Hard Coding: Use .listen(port) during creation.
    2. Configuration: Set server.port=PORT in application.properties.
    3. Command Line: Pass --server.port=PORT as a parameter when running the JAR.
  9. Configure Static Resources

    v2.1.3

    Blade automatically serves files from the static directory under the classpath. You can customize the static resource URL via code or configuration.

    Via Code: Blade.create().addStatics("/mydir");

    Via application.properties: mvc.statics=/mydir

    Blade.create().addStatics("/mydir");
    mvc.statics=/mydir
  10. Implement a Custom Exception Handler

    v2.1.3

    To handle specific exceptions globally, extend DefaultExceptionHandler and override the handle(Exception e) method. Register it as a @Bean.

    @Bean
    public class GlobalExceptionHandler extends DefaultExceptionHandler {
        @Override
        public void handle(Exception e) {
            if (e instanceof CustomException) {
                CustomException customException = (CustomException) e;
                String code = customException.getCode();
                // handle custom logic
            } else {
                super.handle(e);
            }
        }
    }