resolve public url dynamically, remove publicUrl config element

This commit is contained in:
Athou
2024-08-14 21:07:45 +02:00
parent e170dfe60b
commit d612d83874
7 changed files with 21 additions and 32 deletions

View File

@@ -10,7 +10,6 @@ import io.quarkus.runtime.configuration.MemorySize;
import io.smallrye.config.ConfigMapping; import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault; import io.smallrye.config.WithDefault;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive; import jakarta.validation.constraints.Positive;
/** /**
@@ -20,15 +19,6 @@ import jakarta.validation.constraints.Positive;
*/ */
@ConfigMapping(prefix = "commafeed") @ConfigMapping(prefix = "commafeed")
public interface CommaFeedConfiguration { public interface CommaFeedConfiguration {
/**
* URL used to access commafeed, used for various redirects.
*
*/
@NotBlank
@WithDefault("http://localhost:8082")
String publicUrl();
/** /**
* Whether to expose a robots.txt file that disallows web crawlers and search engine indexers. * Whether to expose a robots.txt file that disallows web crawlers and search engine indexers.
*/ */

View File

@@ -5,8 +5,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import com.commafeed.CommaFeedConfiguration; import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.dao.FeedDAO; import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO; import com.commafeed.backend.dao.FeedEntryStatusDAO;
@@ -56,15 +54,6 @@ public class FeedSubscriptionService {
} }
public long subscribe(User user, String url, String title, FeedCategory category, int position) { public long subscribe(User user, String url, String title, FeedCategory category, int position) {
final String pubUrl = config.publicUrl();
if (StringUtils.isBlank(pubUrl)) {
throw new FeedSubscriptionException("Public URL of this CommaFeed instance is not set");
}
if (url.startsWith(pubUrl)) {
throw new FeedSubscriptionException("Could not subscribe to a feed from this CommaFeed instance");
}
Integer maxFeedsPerUser = config.database().cleanup().maxFeedsPerUser(); Integer maxFeedsPerUser = config.database().cleanup().maxFeedsPerUser();
if (maxFeedsPerUser > 0 && feedSubscriptionDAO.count(user) >= maxFeedsPerUser) { if (maxFeedsPerUser > 0 && feedSubscriptionDAO.count(user) >= maxFeedsPerUser) {
String message = String.format("You cannot subscribe to more feeds on this CommaFeed instance (max %s feeds per user)", String message = String.format("You cannot subscribe to more feeds on this CommaFeed instance (max %s feeds per user)",

View File

@@ -66,6 +66,7 @@ import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status; import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriInfo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -89,6 +90,7 @@ public class CategoryREST {
private final FeedEntryService feedEntryService; private final FeedEntryService feedEntryService;
private final FeedSubscriptionService feedSubscriptionService; private final FeedSubscriptionService feedSubscriptionService;
private final CommaFeedConfiguration config; private final CommaFeedConfiguration config;
private final UriInfo uri;
@Path("/entries") @Path("/entries")
@GET @GET
@@ -215,7 +217,7 @@ public class CategoryREST {
feed.setFeedType("rss_2.0"); feed.setFeedType("rss_2.0");
feed.setTitle("CommaFeed - " + entries.getName()); feed.setTitle("CommaFeed - " + entries.getName());
feed.setDescription("CommaFeed - " + entries.getName()); feed.setDescription("CommaFeed - " + entries.getName());
feed.setLink(config.publicUrl()); feed.setLink(uri.getBaseUri().toString());
feed.setEntries(entries.getEntries().stream().map(Entry::asRss).toList()); feed.setEntries(entries.getEntries().stream().map(Entry::asRss).toList());
SyndFeedOutput output = new SyndFeedOutput(); SyndFeedOutput output = new SyndFeedOutput();

View File

@@ -1,7 +1,6 @@
package com.commafeed.frontend.resource; package com.commafeed.frontend.resource;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Instant; import java.time.Instant;
import java.util.Calendar; import java.util.Calendar;
@@ -86,6 +85,7 @@ import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.ResponseBuilder; import jakarta.ws.rs.core.Response.ResponseBuilder;
import jakarta.ws.rs.core.Response.Status; import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriInfo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -114,6 +114,7 @@ public class FeedREST {
private final OPMLImporter opmlImporter; private final OPMLImporter opmlImporter;
private final OPMLExporter opmlExporter; private final OPMLExporter opmlExporter;
private final CommaFeedConfiguration config; private final CommaFeedConfiguration config;
private final UriInfo uri;
private static FeedEntry initTestEntry() { private static FeedEntry initTestEntry() {
FeedEntry entry = new FeedEntry(); FeedEntry entry = new FeedEntry();
@@ -217,7 +218,7 @@ public class FeedREST {
feed.setFeedType("rss_2.0"); feed.setFeedType("rss_2.0");
feed.setTitle("CommaFeed - " + entries.getName()); feed.setTitle("CommaFeed - " + entries.getName());
feed.setDescription("CommaFeed - " + entries.getName()); feed.setDescription("CommaFeed - " + entries.getName());
feed.setLink(config.publicUrl()); feed.setLink(uri.getBaseUri().toString());
feed.setEntries(entries.getEntries().stream().map(Entry::asRss).toList()); feed.setEntries(entries.getEntries().stream().map(Entry::asRss).toList());
SyndFeedOutput output = new SyndFeedOutput(); SyndFeedOutput output = new SyndFeedOutput();
@@ -413,7 +414,7 @@ public class FeedREST {
} catch (Exception e) { } catch (Exception e) {
log.info("Could not subscribe to url {} : {}", url, e.getMessage()); log.info("Could not subscribe to url {} : {}", url, e.getMessage());
} }
return Response.temporaryRedirect(URI.create(config.publicUrl())).build(); return Response.temporaryRedirect(uri.getBaseUri()).build();
} }
private String prependHttp(String url) { private String prependHttp(String url) {

View File

@@ -58,6 +58,7 @@ import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status; import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriInfo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -79,6 +80,7 @@ public class UserREST {
private final PasswordEncryptionService encryptionService; private final PasswordEncryptionService encryptionService;
private final MailService mailService; private final MailService mailService;
private final CommaFeedConfiguration config; private final CommaFeedConfiguration config;
private final UriInfo uri;
@Path("/settings") @Path("/settings")
@GET @GET
@@ -289,7 +291,7 @@ public class UserREST {
} }
private String buildEmailContent(User user) throws Exception { private String buildEmailContent(User user) throws Exception {
String publicUrl = FeedUtils.removeTrailingSlash(config.publicUrl()); String publicUrl = FeedUtils.removeTrailingSlash(uri.getBaseUri().toString());
publicUrl += "/rest/user/passwordResetCallback"; publicUrl += "/rest/user/passwordResetCallback";
return String.format( return String.format(
"You asked for password recovery for account '%s', <a href='%s'>follow this link</a> to change your password. Ignore this if you didn't request a password recovery.", "You asked for password recovery for account '%s', <a href='%s'>follow this link</a> to change your password. Ignore this if you didn't request a password recovery.",
@@ -337,7 +339,7 @@ public class UserREST {
String message = "Your new password is: " + passwd; String message = "Your new password is: " + passwd;
message += "<br />"; message += "<br />";
message += String.format("<a href=\"%s\">Back to Homepage</a>", config.publicUrl()); message += String.format("<a href=\"%s\">Back to Homepage</a>", uri.getBaseUri());
return Response.ok(message).build(); return Response.ok(message).build();
} }

View File

@@ -1,6 +1,5 @@
package com.commafeed.frontend.servlet; package com.commafeed.frontend.servlet;
import java.net.URI;
import java.time.Instant; import java.time.Instant;
import java.util.Date; import java.util.Date;
@@ -14,6 +13,7 @@ import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path; import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.NewCookie; import jakarta.ws.rs.core.NewCookie;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
@Path("/logout") @Path("/logout")
@PermitAll @PermitAll
@@ -21,16 +21,19 @@ import jakarta.ws.rs.core.Response;
public class LogoutServlet { public class LogoutServlet {
private final CommaFeedConfiguration config; private final CommaFeedConfiguration config;
private final UriInfo uri;
private final String cookieName; private final String cookieName;
public LogoutServlet(CommaFeedConfiguration config, @ConfigProperty(name = "quarkus.http.auth.form.cookie-name") String cookieName) { public LogoutServlet(CommaFeedConfiguration config, UriInfo uri,
@ConfigProperty(name = "quarkus.http.auth.form.cookie-name") String cookieName) {
this.config = config; this.config = config;
this.uri = uri;
this.cookieName = cookieName; this.cookieName = cookieName;
} }
@GET @GET
public Response get() { public Response get() {
NewCookie removeCookie = new NewCookie.Builder(cookieName).maxAge(0).expiry(Date.from(Instant.EPOCH)).path("/").build(); NewCookie removeCookie = new NewCookie.Builder(cookieName).maxAge(0).expiry(Date.from(Instant.EPOCH)).path("/").build();
return Response.temporaryRedirect(URI.create(config.publicUrl())).cookie(removeCookie).build(); return Response.temporaryRedirect(uri.getBaseUri()).cookie(removeCookie).build();
} }
} }

View File

@@ -26,6 +26,7 @@ import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path; import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@Path("/next") @Path("/next")
@@ -40,12 +41,13 @@ public class NextUnreadServlet {
private final FeedEntryService feedEntryService; private final FeedEntryService feedEntryService;
private final CommaFeedConfiguration config; private final CommaFeedConfiguration config;
private final AuthenticationContext authenticationContext; private final AuthenticationContext authenticationContext;
private final UriInfo uri;
@GET @GET
public Response get(@QueryParam("category") String categoryId, @QueryParam("order") @DefaultValue("desc") ReadingOrder order) { public Response get(@QueryParam("category") String categoryId, @QueryParam("order") @DefaultValue("desc") ReadingOrder order) {
User user = authenticationContext.getCurrentUser(); User user = authenticationContext.getCurrentUser();
if (user == null) { if (user == null) {
return Response.temporaryRedirect(URI.create(config.publicUrl())).build(); return Response.temporaryRedirect(uri.getBaseUri()).build();
} }
FeedEntryStatus status = unitOfWork.call(() -> { FeedEntryStatus status = unitOfWork.call(() -> {
@@ -71,7 +73,7 @@ public class NextUnreadServlet {
return s; return s;
}); });
String url = status == null ? config.publicUrl() : status.getEntry().getUrl(); String url = status == null ? uri.getBaseUri().toString() : status.getEntry().getUrl();
return Response.temporaryRedirect(URI.create(url)).build(); return Response.temporaryRedirect(URI.create(url)).build();
} }
} }