Use jte template engine

This commit is contained in:
2023-08-19 18:20:06 +03:00
parent 1734bcdea3
commit ef619715e4
8 changed files with 326 additions and 273 deletions

381
pom.xml
View File

@@ -1,179 +1,218 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.github.russp</groupId>
<artifactId>bpe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bpe</name>
<description>Bad-practice editor</description>
<properties>
<java.version>17</java.version>
<repackage.classifier/>
<spring-native.version>0.12.1</spring-native.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>${spring-native.version}</version>
</dependency>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.github.russp</groupId>
<artifactId>bpe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bpe</name>
<description>Bad-practice editor</description>
<properties>
<java.version>17</java.version>
<jte.version>3.0.3</jte.version>
<repackage.classifier/>
<spring-native.version>0.12.1</spring-native.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>${spring-native.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>gg.jte</groupId>
<artifactId>jte</artifactId>
<version>${jte.version}</version>
</dependency>
<dependency>
<groupId>gg.jte</groupId>
<artifactId>jte-spring-boot-starter-2</artifactId>
<version>${jte.version}</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<classifier>${repackage.classifier}</classifier>
<image>
<builder>paketobuildpacks/builder:tiny</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>${spring-native.version}</version>
<executions>
<execution>
<id>test-generate</id>
<goals>
<goal>test-generate</goal>
</goals>
</execution>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>native</id>
<properties>
<repackage.classifier>exec</repackage.classifier>
<native-buildtools.version>0.9.13</native-buildtools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native-buildtools.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>test-native</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
<execution>
<id>build-native</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<classifier>${repackage.classifier}</classifier>
<image>
<builder>paketobuildpacks/builder:tiny</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>${spring-native.version}</version>
<executions>
<execution>
<id>test-generate</id>
<goals>
<goal>test-generate</goal>
</goals>
</execution>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>gg.jte</groupId>
<artifactId>jte-maven-plugin</artifactId>
<version>${jte.version}</version>
<configuration>
<sourceDirectory>${basedir}/src/main/jte</sourceDirectory>
<contentType>Html</contentType>
<targetResourceDirectory>${project.build.directory}/classes</targetResourceDirectory>
<extensions>
<extension>
<className>gg.jte.nativeimage.NativeResourcesExtension</className>
</extension>
</extensions>
</configuration>
<executions>
<execution>
<id>jte-gen</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>gg.jte</groupId>
<artifactId>jte-native-resources</artifactId>
<version>${jte.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<profiles>
<profile>
<id>native</id>
<properties>
<repackage.classifier>exec</repackage.classifier>
<native-buildtools.version>0.9.13</native-buildtools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native-buildtools.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>test-native</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
<execution>
<id>build-native</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,29 @@
package com.github.russp.bpe.config;
import gg.jte.springframework.boot.autoconfigure.ReactiveJteAutoConfiguration;
import gg.jte.springframework.boot.autoconfigure.ReactiveJteViewResolver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.Locale;
@Slf4j
@Configuration
@ImportAutoConfiguration(ReactiveJteAutoConfiguration.class)
@RequiredArgsConstructor
public class JteConfiguration {
private final ReactiveJteViewResolver jteViewResolver;
@PostConstruct
public void vr() {
log.info("ReactiveJteViewResolver created: {}", jteViewResolver);
jteViewResolver.resolveViewName("login", Locale.ENGLISH)
.log()
.block();
}
}

View File

@@ -72,7 +72,7 @@ public class BrowserController {
model.addAttribute("breadcrumbs", breadcrumbs);
model.addAttribute("selectedFile", path);
model.addAttribute("selectedFileName", Paths.get(path).getFileName());
model.addAttribute("selectedFileName", Paths.get(path).getFileName().toString());
return "edit";
}

View File

@@ -1,3 +1,9 @@
@import java.util.List
@import com.github.russp.bpe.http.FileDto
@param List<FileDto> files
@param List<FileDto> breadcrumbs
<!DOCTYPE html>
<html lang="ru">
<head>
@@ -26,9 +32,9 @@
<nav>
<ul class="breadcrumbs">
<li><a href="/"><i class="fa fa-home"></i></a></li>
{{#breadcrumbs}}
<li><a href="/?path={{fullName}}">{{name}}</a></li>
{{/breadcrumbs}}
@for(FileDto breadcrumb : breadcrumbs)
<li><a href="/?path=${breadcrumb.getFullName()}">${breadcrumb.getName()}</a></li>
@endfor
</ul>
</nav>
</header>
@@ -47,26 +53,25 @@
</tr>
</thead>
<tbody>
{{#files}}
<tr>
<th scope="row">
{{#dir}}
<a href="/?path={{fullName}}">
<i class="fa fa-folder"></i>
{{name}}
</a>
{{/dir}}
{{^dir}}
<a href="/edit?path={{fullName}}">
@for(FileDto file : files)
<tr>
<th scope="row">
@if(file.isDir())
<a href="/?path=${file.getFullName()}">
<i class="fa fa-folder"></i>
${file.getName()}
</a>
@else
<a href="/edit?path=${file.getFullName()}">
<i class="fa fa-file"></i>
{{name}}
</a>
{{/dir}}
</th>
<td>{{modifiedAt}}</td>
<td>{{size}}</td>
</tr>
{{/files}}
${file.getName()}
</a>
@endif
</th>
<td>${file.getModifiedAt()}</td>
<td>${file.getSize()}</td>
</tr>
@endfor
</tbody>
</table>
</figure>
@@ -87,7 +92,7 @@
</details>
<small>Built with <a href="https://picocss.com">Pico</a> • <a
href="https://github.com/picocss/examples/blob/master/basic-template/">Source code</a></small>
href="https://github.com/picocss/examples/blob/master/basic-template/">Source code</a></small>
</footer><!-- ./ Footer -->

View File

@@ -1,3 +1,14 @@
@import java.util.List
@import org.springframework.security.web.server.csrf.CsrfToken
@import com.github.russp.bpe.http.FileDto
@param String selectedFile
@param String selectedFileName
@param List<FileDto> breadcrumbs
@param CsrfToken _csrf
<!DOCTYPE html>
<html lang="ru">
<head>
@@ -5,10 +16,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Browser">
<meta name="_csrf" content="{{_csrf.token}}"/>
<meta name="_csrf_header" content="{{_csrf.headerName}}"/>
<meta name="_csrf" content="${_csrf.getToken()}"/>
<meta name="_csrf_header" content="${_csrf.getHeaderName()}"/>
<title>Edit {{selectedFileName}}</title>
<title>Edit ${selectedFileName}</title>
<link rel="shortcut icon" href="https://picocss.com/favicon.ico">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css"
@@ -23,14 +34,14 @@
<body>
<header class="container">
<h5>Edit {{selectedFileName}}</h5>
<h5>Edit ${selectedFileName}</h5>
<nav>
<ul class="breadcrumbs">
<li><a href="/"><i class="fa fa-home"></i></a></li>
{{#breadcrumbs}}
<li><a href="/?path={{fullName}}">{{name}}</a></li>
{{/breadcrumbs}}
<li>{{selectedFileName}}</li>
@for(FileDto breadcrumb : breadcrumbs)
<li><a href="/?path=${breadcrumb.getFullName()}">${breadcrumb.getName()}</a></li>
@endfor
<li>${selectedFileName}</li>
</ul>
<ul>
<li><a href="javascript:save()" role="button">Save</a></li>
@@ -51,7 +62,7 @@
<!-- Footer -->
<footer class="container">
<small>Built with <a href="https://picocss.com">Pico</a> • <a
href="https://github.com/picocss/examples/blob/master/basic-template/">Source code</a></small>
href="https://github.com/picocss/examples/blob/master/basic-template/">Source code</a></small>
</footer><!-- ./ Footer -->
@@ -71,51 +82,51 @@
<script>
var modelist = require("ace/ext/modelist");
var mode = modelist.getModeForPath("{{selectedFileName}}");
var mode = modelist.getModeForPath("${selectedFileName}");
var editor = ace.edit("editor",
{
mode: mode.mode,
selectionStyle: "text",
theme: "ace/theme/dracula"
});
{
mode: mode.mode,
selectionStyle: "text",
theme: "ace/theme/dracula"
});
document.getElementById('editor').style.fontSize = '1.2em';
fetch('/content?path={{selectedFile}}')
.then(function (response) {
return response.text()
})
.then(function (body) {
editor.setValue(body);
editor.clearSelection();
editor.gotoLine(1);
});
fetch('/content?path=${selectedFile}')
.then(function (response) {
return response.text()
})
.then(function (body) {
editor.setValue(body);
editor.clearSelection();
editor.gotoLine(1);
});
function save() {
var blob = new Blob([editor.getValue()]);
var formData = new FormData();
formData.set("file", blob);
fetch('/content?path={{selectedFile}}', {
fetch('/content?path=${selectedFile}', {
method: 'POST',
credentials: 'include',
headers: {
"{{_csrf.headerName}}": "{{_csrf.token}}"
"${_csrf.getHeaderName()}": "${_csrf.getToken()}"
},
body: formData,
})
.then(function(ok) {
Toastify({
text: "Saved",
duration: 3000,
close: true,
gravity: "top",
position: "right",
stopOnFocus: true,
style: {
background: "green",
},
onClick: function(){} // Callback after click
}).showToast();
}).catch(function(ex) {
.then(function(ok) {
Toastify({
text: "Saved",
duration: 3000,
close: true,
gravity: "top",
position: "right",
stopOnFocus: true,
style: {
background: "green",
},
onClick: function(){} // Callback after click
}).showToast();
}).catch(function(ex) {
Toastify({
text: "Error",
duration: 3000,

View File

@@ -1,3 +1,7 @@
@import org.springframework.security.web.server.csrf.CsrfToken
@param CsrfToken _csrf
<!DOCTYPE html>
<html lang="ru">
<head>
@@ -14,7 +18,7 @@
<!-- Pico.css -->
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css">
<!-- Custom template-->
<!-- <link rel="stylesheet" href="/stylesheet.css">-->
<!-- <link rel="stylesheet" href="/stylesheet.css">-->
<style>
/* Grid */
@@ -107,7 +111,7 @@
<form method="post">
<input type="text" name="username" placeholder="Username" aria-label="Login" autocomplete="nickname" required>
<input type="password" name="password" placeholder="Password" aria-label="Password" autocomplete="current-password" required>
<input type="hidden" name="{{_csrf.parameterName}}" value="{{_csrf.token}}"/>
<input type="hidden" name="${_csrf.getParameterName()}" value="${_csrf.getToken()}"/>
<button type="submit" class="contrast">Login</button>
</form>
</div>
@@ -119,7 +123,7 @@
<footer class="container">
<small>Built with <a href="https://picocss.com">Pico</a> • <a
href="https://github.com/picocss/examples/blob/master/basic-template/">Source code</a></small>
href="https://github.com/picocss/examples/blob/master/basic-template/">Source code</a></small>
</footer><!-- ./ Footer -->
</body>

View File

@@ -1,35 +0,0 @@
<header class="container">
<hgroup>
<h1>Basic template</h1>
<h2>A basic custom template for Pico using only CSS custom properties (variables).</h2>
</hgroup>
<nav>
<ul>
<li>
<details role="list">
<summary aria-haspopup="listbox" role="button" class="secondary">Theme</summary>
<ul role="listbox">
<li><a href="#" data-theme-switcher="auto">Auto</a></li>
<li><a href="#" data-theme-switcher="light">Light</a></li>
<li><a href="#" data-theme-switcher="dark">Dark</a></li>
</ul>
</details>
</li>
<li>
<details role="list">
<summary aria-haspopup="listbox">Examples</summary>
<ul role="listbox">
<li><a href="../preview/">Preview</a></li>
<li><a href="../preview-rtl/">Right-to-left</a></li>
<li><a href="../classless/">Class-less</a></li>
<li><a href="../basic-template/">Basic template</a></li>
<li><a href="../company/">Company</a></li>
<li><a href="../google-amp/">Google Amp</a></li>
<li><a href="../sign-in/">Sign in</a></li>
<li><a href="../bootstrap-grid/">Bootstrap grid</a></li>
</ul>
</details>
</li>
</ul>
</nav>
</header>