Skip to content

Instantly share code, notes, and snippets.

@rowanl
Created January 17, 2018 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rowanl/29a2a11554a9a4991db7031abf707053 to your computer and use it in GitHub Desktop.
Save rowanl/29a2a11554a9a4991db7031abf707053 to your computer and use it in GitHub Desktop.
playframework-kubernetes - Serving an HTML page
public class CharityController {
private final CharityRepository charityRepository;
private final HttpExecutionContext ec;
private final LocalDateTime endDateTime;
private final Clock clock;
@Inject
public CharityController(CharityRepository charityRepository,
HttpExecutionContext ec,
Clock clock,
Config config) {
this.charityRepository = charityRepository;
this.ec = ec;
this.formFactory = formFactory;
this.clock = clock;
long endTimeAsEpochMilli = config.getLong("charity.end-time");
String zonedIdAsString = config.getString("time.zone-id");
this.endDateTime = ofInstant(ofEpochMilli(endTimeAsEpochMilli), ZoneId.of(zonedIdAsString));
}
@AddCSRFToken
public CompletionStage<Result> index() {
return charityRepository.findList().thenApplyAsync(items ->
timeSensitiveResult(() -> voteIndex.render(items)), ec.current());
}
private Result timeSensitiveResult(Supplier<Html> pageToRender) {
if (isAfterEndDateTime()) {
return movedPermanently(routes.ClosedController.index());
} else {
return ok(pageToRender.get());
}
}
private boolean isAfterEndDateTime() {
return endDateTime.isBefore(now(clock));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment