From ba7f6cb2bc7f4c7244648b61df90358c059dd34e Mon Sep 17 00:00:00 2001 From: scott Date: Sat, 27 Jul 2024 20:19:35 -0400 Subject: [PATCH] replace usage of Spark Java with Javalin --- .../templates/rt/runtime/BaseTemplate.java | 2 +- .../templates/rt/sparkjava/SparkTemplate.java | 110 ------------------ .../manifold-templates/README.md | 39 +++---- 3 files changed, 16 insertions(+), 135 deletions(-) delete mode 100644 manifold-deps-parent/manifold-templates-rt/src/main/java/manifold/templates/rt/sparkjava/SparkTemplate.java diff --git a/manifold-deps-parent/manifold-templates-rt/src/main/java/manifold/templates/rt/runtime/BaseTemplate.java b/manifold-deps-parent/manifold-templates-rt/src/main/java/manifold/templates/rt/runtime/BaseTemplate.java index 2d879d870..7be065b32 100644 --- a/manifold-deps-parent/manifold-templates-rt/src/main/java/manifold/templates/rt/runtime/BaseTemplate.java +++ b/manifold-deps-parent/manifold-templates-rt/src/main/java/manifold/templates/rt/runtime/BaseTemplate.java @@ -29,7 +29,7 @@ /** * The base class for all generated template classes. You can derive your own base class from this one to - * provide application-specific functionality. See {@link manifold.templates.rt.sparkjava.SparkTemplate}. + * provide application-specific functionality. */ public abstract class BaseTemplate { diff --git a/manifold-deps-parent/manifold-templates-rt/src/main/java/manifold/templates/rt/sparkjava/SparkTemplate.java b/manifold-deps-parent/manifold-templates-rt/src/main/java/manifold/templates/rt/sparkjava/SparkTemplate.java deleted file mode 100644 index e7c56c253..000000000 --- a/manifold-deps-parent/manifold-templates-rt/src/main/java/manifold/templates/rt/sparkjava/SparkTemplate.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2018 - Manifold Systems LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package manifold.templates.rt.sparkjava; - -import manifold.templates.rt.runtime.BaseTemplate; -import manifold.rt.api.util.ManEscapeUtil; -import spark.Request; -import spark.Response; - -import static spark.Spark.afterAfter; -import static spark.Spark.before; - -/** - * The base class for a Spark-based template. Use the {@code extends} directive in your template to make this - * class your template's base class. Or, derive your own class from this one with features suitable for your - * application and use that for your templates' base class. - */ -public abstract class SparkTemplate extends BaseTemplate -{ - private static ThreadLocal REQUEST = new ThreadLocal<>(); - private static ThreadLocal RESPONSE = new ThreadLocal<>(); - - public static void init() - { - before( (request, response) -> { - REQUEST.set( request ); - RESPONSE.set( response ); - } ); - - afterAfter( (request, response) -> { - REQUEST.set( null ); - RESPONSE.set( null ); - } ); - } - - /** - * Access Spark's {@link Response} object. - */ - public Response getResponse() - { - return RESPONSE.get(); - } - - /** - * Access Spark's {@link Request} object; - */ - public Request getRequest() - { - return REQUEST.get(); - } - - @Override - public String toS( Object o ) - { - if( o instanceof RawObject ) - { - return super.toS( o.toString() ); - } - else if( o == null ) - { - return ""; - } - else - { - return escapeHTML( o.toString() ); - } - } - - private String escapeHTML( String str ) - { - return ManEscapeUtil.escapeForHTML( str, false ); - } - - /** - * Produce raw HTML - */ - public Object raw( Object o ) - { - return new RawObject( o ); - } - - private static class RawObject - { - private final Object in; - - RawObject( Object in ) - { - this.in = in; - } - - public String toString() - { - return in.toString(); - } - } -} diff --git a/manifold-deps-parent/manifold-templates/README.md b/manifold-deps-parent/manifold-templates/README.md index a84811cd0..13340a9b2 100644 --- a/manifold-deps-parent/manifold-templates/README.md +++ b/manifold-deps-parent/manifold-templates/README.md @@ -33,10 +33,9 @@ begin experimenting with ManTL templates using the Manifold IntelliJ plugin. * [`layout`](#layout) * [`content`](#layout) * [Whitespace](#whitespace) -* [**Spark** Java Support](#spark-java-support) +* [**Javalin** Support](#javalin-support) * [Hello World!](#hello-world) * [Tracing](#tracing) - * [Template Base Class](#sparktemplate-base-class) * [Sample Application](#sample-application) * [Provided Manifold Features](#provided-manifold-features) * [IDE Support](#ide-support) @@ -607,12 +606,12 @@ whitespace immediately preceding or following the language constructs are includ >Note the [`nest`](#nest) directive retains indentation to support use-cases such as *code generation* where whitespace >is significant. -# Spark Java Support +# Javalin Support -ManTL is designed with web frameworks like [Spark](http://sparkjava.com/) in mind. +ManTL is designed with web frameworks like [Javalin](http://javalin.io/) in mind. ## Hello World! -A simple "Hello World!" Spark application making use of ManTL: +A simple "Hello World!" Javalin application making use of ManTL: ```java package app; @@ -621,7 +620,7 @@ import manifold.templates.rt.ManifoldTemplates; import views.Index; import views.layout.DefaultLayout; -import static spark.Spark.*; +import io.javalin.Javalin; public class WebApp { public static void main(String[] args) { @@ -631,32 +630,24 @@ public class WebApp { // Enable tracing ManifoldTemplates.trace(); + // Basic Javalin config + Javalin app = Javalin.create(config -> { + config.staticFiles.add("/public", Location.CLASSPATH); + config.http.defaultContentType = "html"; + }); + // Render the Index template - get("/", (req, resp) -> Index.render("Hello World!")); + app.get("/", ctx -> ctx.result(Index.render("Hello World!"))); } } ``` There are two templates in the `resources` directory: `views/Index.html.mtl` and `views/layouts/DefaultLayout.html.mtl`. Here the code references the `Index` template directly as a Java class. This is a powerful aspect of ManTL -- the -compiler verifies your links are never broken and you can fully leverage the strength of IntelliJ for deterministic +compiler verifies your links are never broken, and you can fully leverage the strength of IntelliJ for deterministic code completion, usage searching, refactoring, navigation, incremental compilation, and hot swap. -> Note the code takes advantage of the _type-safe_ parameters available in ManTL and no Spark "TemplateEngine" is needed. - -### SparkTemplate Base Class - -Manifold provides base class `manifold.templates.rt.sparkjava.SparkTemplate` for use with the `extends` directive -in your templates (or, more commonly, you extend this class and add more of your own application functionality). This -class provides various convenience methods to get the HTTP `Request`, `Response`, etc. and it also automatically escapes -all string content for HTML, to help prevent malicious user input from causing a security issue in your application. - -If you wish, you can output raw HTML in a template that extends `manifold.templates.rt.sparkjava.SparkTemplate` using the -`raw()` function: - -```jsp - ${raw("

Some Raw HTML

")} -``` +> Note the code takes advantage of the _type-safe_ parameters available in ManTL and no Javalin "TemplateEngine" is needed. ### Tracing @@ -671,7 +662,7 @@ After invoking the `trace()` method, every following `render()` call prints the ### Sample Application -A sample Spark application is available here: +A sample Javalin application is available here: [https://github.com/manifold-systems/manifold-sample-web-app](https://github.com/manifold-systems/manifold-sample-web-app)