aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www
diff options
context:
space:
mode:
Diffstat (limited to 'juick-www/src/main/java/com/juick/www')
-rw-r--r--juick-www/src/main/java/com/juick/www/WebApp.java65
-rw-r--r--juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java22
-rw-r--r--juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java6
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java9
4 files changed, 71 insertions, 31 deletions
diff --git a/juick-www/src/main/java/com/juick/www/WebApp.java b/juick-www/src/main/java/com/juick/www/WebApp.java
index 72eb3fbc..6d809245 100644
--- a/juick-www/src/main/java/com/juick/www/WebApp.java
+++ b/juick-www/src/main/java/com/juick/www/WebApp.java
@@ -20,14 +20,10 @@ package com.juick.www;
import com.juick.Message;
import com.juick.Tag;
import com.juick.service.TagService;
-import com.juick.service.UserService;
-import com.juick.www.controllers.PageTemplates;
-import org.apache.commons.lang3.BooleanUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.core.env.Environment;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.annotation.Value;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.XmppException;
import rocks.xmpp.core.session.Extension;
@@ -35,6 +31,8 @@ import rocks.xmpp.core.session.XmppSessionConfiguration;
import rocks.xmpp.core.session.debug.LogbackDebugger;
import rocks.xmpp.extensions.component.accept.ExternalComponent;
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -44,35 +42,42 @@ import java.util.stream.Stream;
*
* @author Ugnich Anton
*/
-public class WebApp implements AutoCloseable {
+public class WebApp implements DisposableBean {
private static Logger logger = LoggerFactory.getLogger(WebApp.class);
private ExternalComponent xmpp;
- Environment env;
- PageTemplates templates;
- UserService userService;
+ @Inject
TagService tagService;
+ @Value("${img_path:/var/www/juick.com/i/}")
private String imgDir;
+ @Value("${upload_tmp_dir:/var/www/juick.com/i/tmp/}")
private String tmpDir;
+ @Value("${xmpp_host:localhost}")
+ private String xmppHost;
+ @Value("${xmpp_password:secret}")
+ private String xmppPassword;
+ @Value("${www_xmpp_jid:www.juick.local}")
+ private String xmppJid;
+ @Value("${xmpp_port:5347}")
+ private int xmppPort;
+ @Value("${xmpp_disabled:false}")
+ private boolean isXmppDisabled;
- public WebApp(Environment env, PageTemplates templates, UserService userService, TagService tagService) {
- this.env = env;
- this.templates = templates;
- this.userService = userService;
- this.tagService = tagService;
- tmpDir = env.getProperty("upload_tmp_dir", "/var/www/juick.com/i/tmp/");
- imgDir = env.getProperty("img_path", "/var/www/juick.com/i/");
- boolean isXmppDisabled = BooleanUtils.toBoolean(env.getProperty("xmpp_disabled"));
+ private String scriptsUrl;
+ private String styleUrl;
+
+ @PostConstruct
+ public void init() {
if (!isXmppDisabled) {
- setupXmppComponent(Jid.of(env.getProperty("www_xmpp_jid", "www.juick.local")),
- env.getProperty("xmpp_password"), NumberUtils.toInt(env.getProperty("xmpp_port", StringUtils.EMPTY), 5347));
+ setupXmppComponent(Jid.of(xmppJid),
+ xmppPassword, xmppPort);
}
}
@Override
- public void close() {
+ public void destroy() {
try {
if (xmpp != null)
xmpp.close();
@@ -87,7 +92,7 @@ public class WebApp implements AutoCloseable {
.extensions(Extension.of(Message.class))
.debugger(LogbackDebugger.class)
.build();
- xmpp = ExternalComponent.create(componentJid.toString(), password, configuration, "localhost", port);
+ xmpp = ExternalComponent.create(componentJid.toString(), password, configuration, xmppHost, port);
xmpp.addConnectionListener(e -> logger.error(e.toString(), e.getCause()));
try {
xmpp.connect();
@@ -128,4 +133,20 @@ public class WebApp implements AutoCloseable {
}
return tags;
}
+
+ public String getStyleUrl() {
+ return styleUrl;
+ }
+
+ public String getScriptsUrl() {
+ return scriptsUrl;
+ }
+
+ public void setScriptsUrl(String scriptsUrl) {
+ this.scriptsUrl = scriptsUrl;
+ }
+
+ public void setStyleUrl(String styleUrl) {
+ this.styleUrl = styleUrl;
+ }
}
diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java
index 2cf1faed..a5bc3f73 100644
--- a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java
+++ b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java
@@ -1,13 +1,16 @@
package com.juick.www.configuration;
import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.www.HelpService;
-import com.juick.www.controllers.PageTemplates;
import com.juick.www.WebApp;
+import com.juick.www.controllers.PageTemplates;
+import org.apache.commons.io.IOUtils;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
@@ -17,6 +20,9 @@ import org.springframework.core.env.Environment;
import javax.annotation.Resource;
import javax.inject.Inject;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
/**
* Created by aalexeev on 11/22/16.
@@ -31,10 +37,20 @@ public class WwwAppConfiguration {
private UserService userService;
@Inject
private TagService tagService;
+ @Inject
+ private ObjectMapper jsonMapper;
+ @Value("#{servletContext.contextPath}")
+ private String servletContextPath;
@Bean
- public WebApp webApp() {
- return new WebApp(env, templates(), userService, tagService);
+ public WebApp webApp() throws IOException {
+ WebApp app = new WebApp();
+ String manifestString = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("manifest.json"), StandardCharsets.UTF_8);
+ HashMap<String, String> manifest = jsonMapper.readValue(manifestString,
+ new TypeReference<HashMap<String, String>>() {});
+ app.setScriptsUrl(manifest.get("scripts.js"));
+ app.setStyleUrl(manifest.get("style.css"));
+ return app;
}
@Bean
public PageTemplates templates() {
diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java
index 43ca37db..ae51f1cd 100644
--- a/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java
+++ b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java
@@ -38,11 +38,11 @@ public class WwwServletConfiguration extends WebMvcConfigurationSupport {
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.setOrder(0);
registry.addResourceHandler(
- "/scripts.js*",
- "/style.css*",
+ "/scripts*.js*",
+ "/style*.css*",
"/*.png",
"/favicon.ico")
- .addResourceLocations("/")
+ .addResourceLocations("classpath:/")
.setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS).mustRevalidate().cachePublic())
.resourceChain(true)
.addResolver(new PathResourceResolver());
diff --git a/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java b/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java
index 925d1013..5f758608 100644
--- a/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java
+++ b/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java
@@ -23,6 +23,7 @@ import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.util.MessageUtils;
+import com.juick.www.WebApp;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
@@ -64,19 +65,21 @@ public class PageTemplates {
MessagesService messagesService;
@Inject
UserService userService;
+ @Inject
+ WebApp webApp;
public void pageHead(PrintWriter out, com.juick.User visitor, String title, String headers) {
out.println("<!DOCTYPE html>");
out.print("<html>");
out.print("<head>");
out.println("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">");
- out.print("<link rel=\"stylesheet\" href=\"/style.css?v=2\"/>");
- out.print("<script type=\"text/javascript\" src=\"/scripts.js\"></script>");
+ out.print("<link rel=\"stylesheet\" href=\"/" + webApp.getStyleUrl() + "\"/>");
+ out.print("<script type=\"text/javascript\" src=\"/" + webApp.getScriptsUrl() + "\"></script>");
if (headers != null) {
out.print(headers);
}
out.print("<title>" + title + "</title>");
- out.println("<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\"/>");
+ out.println("<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"/>");
out.println("<link rel=\"icon\" href=\"//i.juick.com/favicon.png\"/>");
out.println("<!--[if lt IE 9 & (!IEMobile 7)]>");
out.println("<script src=\"//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js\"></script>");