aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2024-03-29 21:50:19 +0300
committerGravatar Vitaly Takmazov2024-03-29 22:08:34 +0300
commitc172469d601220c3a3b3c35e0de3409b7e23e78c (patch)
tree3a2142fc1916ff467d3466acef9c45d2ff4566c5
parentc1ce093dacb09f164ae31c2bc01a04ce5cac3184 (diff)
Disable caching in development mode
-rw-r--r--src/main/java/com/juick/EmailManager.java6
-rw-r--r--src/main/java/com/juick/config/AppConfig.java40
-rw-r--r--src/main/java/com/juick/config/WebConfig.java71
-rw-r--r--src/main/java/com/juick/service/MessagesService.java6
-rw-r--r--src/main/java/com/juick/service/MessagesServiceImpl.java39
-rw-r--r--src/main/java/com/juick/www/WebApp.java58
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java6
7 files changed, 121 insertions, 105 deletions
diff --git a/src/main/java/com/juick/EmailManager.java b/src/main/java/com/juick/EmailManager.java
index 6180668e..6958d970 100644
--- a/src/main/java/com/juick/EmailManager.java
+++ b/src/main/java/com/juick/EmailManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2023, Juick
+ * Copyright (C) 2008-2024, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -148,9 +148,9 @@ public class EmailManager implements NotificationListener {
headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid()));
}
}
- String plainText = webApp.renderPlaintext(formatPost(msg, webDomain), formatUrl(msg)).orElseThrow(IllegalStateException::new);
+ String plainText = messagesService.renderPlaintext(formatPost(msg, webDomain), formatUrl(msg)).orElseThrow(IllegalStateException::new);
String hash = userService.getHashByUID(userService.getUserByEmail(email).getUid());
- String htmlText = webApp.renderHtml(MessageUtils.formatHtml(msg), formatUrl(msg), msg, hash).orElseThrow(IllegalStateException::new);
+ String htmlText = messagesService.renderHtml(MessageUtils.formatHtml(msg), formatUrl(msg), msg, hash).orElseThrow(IllegalStateException::new);
sendEmail(StringUtils.EMPTY, email, subject, plainText, htmlText, headers);
}
public boolean sendEmail(String from, String to, String subject, String textPart, String htmlPart, Map<String, String> messageHeaders) {
diff --git a/src/main/java/com/juick/config/AppConfig.java b/src/main/java/com/juick/config/AppConfig.java
index 27622195..25ba2b86 100644
--- a/src/main/java/com/juick/config/AppConfig.java
+++ b/src/main/java/com/juick/config/AppConfig.java
@@ -97,35 +97,6 @@ public class AppConfig {
}
@Bean
- Loader<?> templateLoader() {
- return new ClasspathLoader();
- }
-
- @Bean
- SpringExtension springExtension() {
- return new SpringExtension(null);
- }
-
- @Bean
- PebbleEngine pebbleEngine() {
- boolean devToolsArePresent = false;
- try {
- Class.forName("org.springframework.boot.devtools.livereload.Connection");
- devToolsArePresent = true;
- } catch (ClassNotFoundException e) {
- // release mode
- }
- return new PebbleEngine.Builder()
- .loader(this.templateLoader())
- .cacheActive(!devToolsArePresent)
- .extension(springExtension())
- .extension(new FormatterExtension())
- .newLineTrimming(false)
- .strictVariables(true)
- .build();
- }
-
- @Bean
ResourceUrlProvider resourceUrlProvider() {
return new ResourceUrlProvider();
}
@@ -162,17 +133,6 @@ public class AppConfig {
return new TopManager();
}
-
- @Bean
- ViewResolver viewResolver() {
- PebbleViewResolver viewResolver = new PebbleViewResolver(pebbleEngine());
- viewResolver.setPrefix("templates");
- viewResolver.setSuffix(".html");
- viewResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
- viewResolver.setExposeRequestAttributes(true);
- return viewResolver;
- }
-
@Inject
private UserService userService;
@Value("${service_user:juick}")
diff --git a/src/main/java/com/juick/config/WebConfig.java b/src/main/java/com/juick/config/WebConfig.java
index 136faea9..43857bb5 100644
--- a/src/main/java/com/juick/config/WebConfig.java
+++ b/src/main/java/com/juick/config/WebConfig.java
@@ -18,6 +18,7 @@
package com.juick.config;
import java.net.MalformedURLException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.TimeUnit;
@@ -26,6 +27,13 @@ import javax.inject.Inject;
import com.juick.service.UserService;
import com.juick.www.VisitorArgumentResolver;
+import com.mitchellbosecke.pebble.extension.FormatterExtension;
+import io.pebbletemplates.pebble.PebbleEngine;
+import io.pebbletemplates.pebble.loader.ClasspathLoader;
+import io.pebbletemplates.pebble.loader.Loader;
+import io.pebbletemplates.spring.extension.SpringExtension;
+import io.pebbletemplates.spring.servlet.PebbleViewResolver;
+import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
@@ -36,6 +44,7 @@ import org.springframework.http.CacheControl;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
+import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.VersionResourceResolver;
@@ -52,6 +61,17 @@ public class WebConfig implements WebMvcConfigurer {
@Inject
private UserService userService;
+ private boolean devToolsArePresent = false;
+ @PostConstruct
+ public void initConfiguration() {
+ try {
+ Class.forName("org.springframework.boot.devtools.livereload.Connection");
+ devToolsArePresent = true;
+ } catch (ClassNotFoundException e) {
+ // release mode
+ }
+ }
+
@Override
public void addFormatters(FormatterRegistry registry) {
for (Converter<?, ?> converter : converters) {
@@ -66,15 +86,54 @@ public class WebConfig implements WebMvcConfigurer {
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(visitorArgumentResolver());
}
+
+ @Bean
+ Loader<?> templateLoader() {
+ return new ClasspathLoader();
+ }
+
+ @Bean
+ SpringExtension springExtension() {
+ return new SpringExtension(null);
+ }
+
+ @Bean
+ PebbleEngine pebbleEngine() {
+
+ return new PebbleEngine.Builder()
+ .loader(this.templateLoader())
+ .cacheActive(!devToolsArePresent)
+ .extension(springExtension())
+ .extension(new FormatterExtension())
+ .newLineTrimming(false)
+ .strictVariables(true)
+ .build();
+ }
+ @Bean
+ ViewResolver viewResolver() {
+ PebbleViewResolver viewResolver = new PebbleViewResolver(pebbleEngine());
+ viewResolver.setPrefix("templates");
+ viewResolver.setSuffix(".html");
+ viewResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
+ viewResolver.setExposeRequestAttributes(true);
+ return viewResolver;
+ }
+
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
try {
- registry
- .addResourceHandler("/**", "/i/a/**")
- .addResourceLocations("classpath:/static/", Paths.get(baseDir, "/i/a/").toUri().toURL().toString())
- .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
- .resourceChain(false)
- .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**", "/i/a/**"));
+ if (devToolsArePresent) {
+ registry
+ .addResourceHandler("/**", "/i/a/**")
+ .addResourceLocations("classpath:/static/", Paths.get(baseDir, "/i/a/").toUri().toURL().toString());
+ } else {
+ registry
+ .addResourceHandler("/**", "/i/a/**")
+ .addResourceLocations("classpath:/static/", Paths.get(baseDir, "/i/a/").toUri().toURL().toString())
+ .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
+ .resourceChain(false)
+ .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**", "/i/a/**"));
+ }
} catch (MalformedURLException e) {
e.printStackTrace();
}
diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java
index 6f262dd7..f2321b2a 100644
--- a/src/main/java/com/juick/service/MessagesService.java
+++ b/src/main/java/com/juick/service/MessagesService.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2023, Juick
+ * Copyright (C) 2008-2024, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -165,4 +165,8 @@ public interface MessagesService {
@CacheEvict(value = { "discover", "discussions", "messages", "replies" }, allEntries = true)
void setFriendsOnly(Integer mid, Boolean friendsOnly);
+
+ Optional<String> renderPlaintext(String body, String messageUrl);
+
+ Optional<String> renderHtml(String body, String messageUrl, Message msg, String hash);
}
diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java
index 91a257ef..940cdaba 100644
--- a/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2021, Juick
+ * Copyright (C) 2008-2024, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -23,6 +23,8 @@ import com.juick.www.WebApp;
import com.juick.util.MessageUtils;
import com.juick.util.TagUtils;
+import io.pebbletemplates.pebble.PebbleEngine;
+import io.pebbletemplates.pebble.template.PebbleTemplate;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
@@ -43,6 +45,9 @@ import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.inject.Inject;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
import java.net.URI;
import java.sql.*;
import java.time.Instant;
@@ -71,6 +76,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
private String baseImagesUrl;
@Inject
private User archiveUser;
+ @Inject
+ private PebbleEngine pebbleEngine;
private class MessageMapper implements RowMapper<Message> {
@Override
@@ -1208,4 +1215,34 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
int privacy = friendsOnly ? -1 : 1;
jdbcTemplate.update("UPDATE messages SET privacy=? WHERE message_id=?", privacy, mid);
}
+
+ public Optional<String> renderPlaintext(String body, String messageUrl) {
+ PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/plaintext");
+ Map<String, Object> context = new HashMap<>();
+ context.put("messageBody", body);
+ context.put("messageUrl", messageUrl);
+ try {
+ Writer writer = new StringWriter();
+ noteTemplate.evaluate(writer, context);
+ return Optional.of(writer.toString());
+ } catch (IOException e) {
+ return Optional.empty();
+ }
+ }
+
+ public Optional<String> renderHtml(String body, String messageUrl, Message msg, String hash) {
+ PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/html");
+ Map<String, Object> context = new HashMap<>();
+ context.put("messageBody", body);
+ context.put("messageUrl", messageUrl);
+ context.put("msg", msg);
+ context.put("hash", hash);
+ try {
+ Writer writer = new StringWriter();
+ noteTemplate.evaluate(writer, context);
+ return Optional.of(writer.toString());
+ } catch (IOException e) {
+ return Optional.empty();
+ }
+ }
}
diff --git a/src/main/java/com/juick/www/WebApp.java b/src/main/java/com/juick/www/WebApp.java
index a7c5eb8b..cf9570aa 100644
--- a/src/main/java/com/juick/www/WebApp.java
+++ b/src/main/java/com/juick/www/WebApp.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2020, Juick
+ * Copyright (C) 2008-2024, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -16,34 +16,22 @@
*/
package com.juick.www;
-import java.io.IOException;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Stream;
-
-import javax.inject.Inject;
-
-import com.juick.model.Message;
import com.juick.model.Tag;
import com.juick.model.User;
import com.juick.service.TagService;
-import io.pebbletemplates.pebble.PebbleEngine;
-import io.pebbletemplates.pebble.template.PebbleTemplate;
-
import jakarta.annotation.PostConstruct;
-
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.resource.ResourceUrlProvider;
import org.springframework.web.util.UriComponentsBuilder;
+import javax.inject.Inject;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Stream;
+
/**
*
* @author Ugnich Anton
@@ -54,8 +42,6 @@ public class WebApp {
private TagService tagService;
@Inject
private ResourceUrlProvider resourceUrlProvider;
- @Inject
- private PebbleEngine pebbleEngine;
@Value("${img_url:http://localhost:8080/i/}")
private String baseImagesUri;
UriComponentsBuilder avatarBuilder;
@@ -105,34 +91,4 @@ public class WebApp {
avatarBuilder.replacePath(getAvatarWebPath(user));
return avatarBuilder.build().toUriString();
}
-
- public Optional<String> renderPlaintext(String body, String messageUrl) {
- PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/plaintext");
- Map<String, Object> context = new HashMap<>();
- context.put("messageBody", body);
- context.put("messageUrl", messageUrl);
- try {
- Writer writer = new StringWriter();
- noteTemplate.evaluate(writer, context);
- return Optional.of(writer.toString());
- } catch (IOException e) {
- return Optional.empty();
- }
- }
-
- public Optional<String> renderHtml(String body, String messageUrl, Message msg, String hash) {
- PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/html");
- Map<String, Object> context = new HashMap<>();
- context.put("messageBody", body);
- context.put("messageUrl", messageUrl);
- context.put("msg", msg);
- context.put("hash", hash);
- try {
- Writer writer = new StringWriter();
- noteTemplate.evaluate(writer, context);
- return Optional.of(writer.toString());
- } catch (IOException e) {
- return Optional.empty();
- }
- }
}
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index 7046fdf8..a728ac2b 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -2416,17 +2416,17 @@ public class ServerTests {
@Test
public void emailTemplatesTest() throws IOException {
- String plainText = webApp.renderPlaintext("yo", "https://localhost/m/1").orElseThrow();
+ String plainText = messagesService.renderPlaintext("yo", "https://localhost/m/1").orElseThrow();
assertThat(plainText, is(getSnapshot(testSubscriptionTextEmail)));
User demo = MockUtils.mockUser(45, ugnichName, ugnichPassword);
Message html = MockUtils.mockMessage(56, demo, "yo");
- String htmlText = webApp
+ String htmlText = messagesService
.renderHtml(MessageUtils.formatHtml(html), PlainTextFormatter.formatUrl(html), html,
"12345")
.orElseThrow();
assertThat(htmlText, is(getSnapshot(testSubscriptionHtmlEmail)));
html.setMid(0);
- String htmlPM = webApp
+ String htmlPM = messagesService
.renderHtml(MessageUtils.formatHtml(html), PlainTextFormatter.formatUrl(html), html,
"12345")
.orElseThrow();