Deniss M.
Deniss M.

Reputation: 4050

Display current service gateway url by using Thymeleaf

I'm having trouble with finding a solution to display my current service url (registered with eureka and called up through a zuul gateway), like: http://localhost:8080/services/someservice with Thymeleaf in a client html page.

I've tried using #httpServletRequest?.requestURL, but that returns how the service is registered with Eureka, like: user.somepackage.com:someservice:8011, which isn't exactly what I want.

One possible solution I have for this is to assign a variable in Thymeleaf once with the url hardcoded, or do it through a Spring controller, but I don't think this is a neat.

Maybe somebody had a similar problem and found a better solution.

Upvotes: 1

Views: 1243

Answers (2)

tmarwen
tmarwen

Reputation: 16364

Having the Reverse proxy enabled, this should be straightforward using the configured routes mapping.

Consider the following route mapping example for the ZUUL API Gateway:

zuul:
  routes:
    someservice:
      path: /services/someservice/**
      url: http://localhost:8080/services/someservice

You should be able to access the context-relative URL of your service within the client Thymeleaf template:

<a th:href="@{/services/someservice/}">
<!-- OR -->
<form th:action="@{/services/someservice}">
<!-- ... -->

According to your need, you should use the appropriate Thymeleaf URL syntax. Here down the URL sample:

<!-- Server-relative -->
<a th:href="@{~/services/someservice/}">

<!-- Context-relative -->
<a th:href="@{/services/someservice/}">
<!-- ... -->

Upvotes: 1

Deniss M.
Deniss M.

Reputation: 4050

Actually, the solution was quite simple:

<a th:href="@{~/services/someservice/logout}">

The most interesting part is the ~ sign. This defines the actual server url, like: http://localhost:8080. Just what I needed.

Upvotes: 2

Related Questions