diff options
Diffstat (limited to 'src/main/java/com/juick/config')
-rw-r--r-- | src/main/java/com/juick/config/AppConfig.java | 40 | ||||
-rw-r--r-- | src/main/java/com/juick/config/WebConfig.java | 71 |
2 files changed, 65 insertions, 46 deletions
diff --git a/src/main/java/com/juick/config/AppConfig.java b/src/main/java/com/juick/config/AppConfig.java index 276221958..25ba2b861 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 136faea9e..43857bb54 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(); } |