Compare commits
14 Commits
1438b294ef
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
34d56df527
|
|||
| 627cb833fe | |||
|
d651676106
|
|||
|
ac0a1abe34
|
|||
|
|
7889bf48da | ||
|
|
ab230ef715 | ||
|
|
dfe260f412 | ||
|
|
577c97f1bb | ||
|
ee1ab34631
|
|||
|
2dae9c9e14
|
|||
|
8000a26aad
|
|||
|
3c2eea0eb5
|
|||
|
a184f4a038
|
|||
|
46dd36a846
|
51
.gitea/workflows/docker.yml
Normal file
51
.gitea/workflows/docker.yml
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
name: Build and Push Multi-Arch Docker Image
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-and-push:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
|
- name: Set Registry Domain
|
||||||
|
run: |
|
||||||
|
REGISTRY_DOMAIN=$(echo "${{ github.server_url }}" | sed 's|https://||' | sed 's|http://||')
|
||||||
|
echo "REGISTRY_DOMAIN=$REGISTRY_DOMAIN" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Login to Gitea Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ${{ env.REGISTRY_DOMAIN }}
|
||||||
|
username: ${{ secrets.REGISTRY_USER }}
|
||||||
|
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Set up JDK
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
java-version: '17'
|
||||||
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: Extract Version
|
||||||
|
run: |
|
||||||
|
VERSION=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout)
|
||||||
|
echo "VERSION=$VERSION" >> $GITHUB_ENV
|
||||||
|
echo "MAJOR=$(echo $VERSION | cut -d. -f1)" >> $GITHUB_ENV
|
||||||
|
echo "MAJOR_MINOR=$(echo $VERSION | cut -d. -f1,2)" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Build and Push Multi-Arch Image
|
||||||
|
run: |
|
||||||
|
docker buildx build \
|
||||||
|
--platform linux/amd64,linux/arm64 \
|
||||||
|
-t ${{ env.REGISTRY_DOMAIN }}/${{ github.repository }}:latest \
|
||||||
|
-t ${{ env.REGISTRY_DOMAIN }}/${{ github.repository }}:${{ env.MAJOR }} \
|
||||||
|
-t ${{ env.REGISTRY_DOMAIN }}/${{ github.repository }}:${{ env.MAJOR_MINOR }} \
|
||||||
|
-t ${{ env.REGISTRY_DOMAIN }}/${{ github.repository }}:${{ env.VERSION }} \
|
||||||
|
--push .
|
||||||
11
.github/dependabot.yml
vendored
11
.github/dependabot.yml
vendored
@@ -1,11 +0,0 @@
|
|||||||
version: 2
|
|
||||||
updates:
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "weekly"
|
|
||||||
|
|
||||||
- package-ecosystem: "maven"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "weekly"
|
|
||||||
39
.github/workflows/deploy-hetzner.yaml
vendored
39
.github/workflows/deploy-hetzner.yaml
vendored
@@ -1,39 +0,0 @@
|
|||||||
---
|
|
||||||
name: 'deploy to hetzner'
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
deploy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Cloning repo
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Set up JDK 21
|
|
||||||
uses: actions/setup-java@v4
|
|
||||||
with:
|
|
||||||
java-version: '21'
|
|
||||||
distribution: 'temurin'
|
|
||||||
cache: maven
|
|
||||||
|
|
||||||
- name: Build and test with Maven
|
|
||||||
run: mvn --batch-mode --update-snapshots verify
|
|
||||||
|
|
||||||
- name: Push to dokku
|
|
||||||
timeout-minutes: 20
|
|
||||||
uses: dokku/github-action@v1
|
|
||||||
with:
|
|
||||||
branch: main
|
|
||||||
git_remote_url: ${{ secrets.HETZNER_GIT_URI }}
|
|
||||||
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
||||||
git_push_flags: --force
|
|
||||||
@@ -4,8 +4,8 @@
|
|||||||
<envs>
|
<envs>
|
||||||
<env name="EUREKA" value="http://localhost:8761/eureka" />
|
<env name="EUREKA" value="http://localhost:8761/eureka" />
|
||||||
</envs>
|
</envs>
|
||||||
<module name="timezone-service" />
|
<module name="website" />
|
||||||
<option name="SPRING_BOOT_MAIN_CLASS" value="dev.mars3142.fhq.timezone_service.Application" />
|
<option name="SPRING_BOOT_MAIN_CLASS" value="dev.mars3142.fhq.Application" />
|
||||||
<method v="2">
|
<method v="2">
|
||||||
<option name="Make" enabled="true" />
|
<option name="Make" enabled="true" />
|
||||||
</method>
|
</method>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<module name="timezone-service" />
|
<module name="timezone-service" />
|
||||||
<extension name="coverage">
|
<extension name="coverage">
|
||||||
<pattern>
|
<pattern>
|
||||||
<option name="PATTERN" value="dev.mars3142.fhq.timezone_service.*" />
|
<option name="PATTERN" value="dev.mars3142.fhq.timezone.*" />
|
||||||
<option name="ENABLED" value="true" />
|
<option name="ENABLED" value="true" />
|
||||||
</pattern>
|
</pattern>
|
||||||
</extension>
|
</extension>
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ USER app
|
|||||||
|
|
||||||
# Copy all needed project files to a folder
|
# Copy all needed project files to a folder
|
||||||
COPY --chown=app ./.mvn/ .mvn
|
COPY --chown=app ./.mvn/ .mvn
|
||||||
COPY --chown=app ./mvnw ./pom.xml ./app.json ./
|
COPY --chown=app ./mvnw ./pom.xml ./
|
||||||
COPY --chown=app ./src ./src
|
COPY --chown=app ./src ./src
|
||||||
|
|
||||||
RUN curl -OL https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
|
RUN curl -OL https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
|
||||||
@@ -28,10 +28,6 @@ USER app
|
|||||||
COPY --chown=app --from=build /usr/src/app/target/*.jar /usr/app/timezone.jar
|
COPY --chown=app --from=build /usr/src/app/target/*.jar /usr/app/timezone.jar
|
||||||
COPY --chown=app --from=build /usr/src/app/opentelemetry-javaagent.jar /usr/app/opentelemetry-javaagent.jar
|
COPY --chown=app --from=build /usr/src/app/opentelemetry-javaagent.jar /usr/app/opentelemetry-javaagent.jar
|
||||||
|
|
||||||
ENV JAVA_TOOL_OPTIONS="-javaagent:/usr/app/opentelemetry-javaagent.jar"
|
HEALTHCHECK CMD curl --fail http://localhost:8080/actuator/health || exit 1
|
||||||
ENV OTEL_SERVICE_NAME="timezone-service"
|
|
||||||
ENV OTEL_EXPORTER_OTLP_ENDPOINT="http://opentelemetry-collector.web:4318"
|
|
||||||
|
|
||||||
HEALTHCHECK CMD curl --fail http://localhost:5000/actuator/health/liveness || exit 1
|
|
||||||
|
|
||||||
CMD ["java", "-jar", "/usr/app/timezone.jar"]
|
CMD ["java", "-jar", "/usr/app/timezone.jar"]
|
||||||
|
|||||||
20
Makefile
Normal file
20
Makefile
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
REMOTE_HOST = mars3142@172.16.20.1
|
||||||
|
PROJECT_NAME = firmware-hq
|
||||||
|
SERVICE_NAME = timezone-service
|
||||||
|
RASPI_PATH = /mnt/data/${PROJECT_NAME}
|
||||||
|
|
||||||
|
install: build deploy import cleanup
|
||||||
|
|
||||||
|
build:
|
||||||
|
docker buildx build --platform linux/arm64 --no-cache -t ${PROJECT_NAME}/${SERVICE_NAME}:latest -o type=docker,dest=./${SERVICE_NAME}-arm64.tar .
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
scp ./${SERVICE_NAME}-arm64.tar ${REMOTE_HOST}:${RASPI_PATH}/
|
||||||
|
|
||||||
|
import:
|
||||||
|
ssh ${REMOTE_HOST} "sudo docker load -i ${RASPI_PATH}/${SERVICE_NAME}-arm64.tar && rm ${RASPI_PATH}/${SERVICE_NAME}-arm64.tar"
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
rm -f ${SERVICE_NAME}-arm64.tar
|
||||||
|
|
||||||
|
.PHONY: build deploy import cleanup
|
||||||
14
app.json
14
app.json
@@ -1,14 +0,0 @@
|
|||||||
{
|
|
||||||
"healthchecks": {
|
|
||||||
"web": [
|
|
||||||
{
|
|
||||||
"type": "readiness",
|
|
||||||
"name": "web check",
|
|
||||||
"description": "Checking if the app responds to the /actuator/health/readiness endpoint",
|
|
||||||
"path": "/actuator/health/readiness",
|
|
||||||
"port": 5000,
|
|
||||||
"attempts": 3
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
18
pom.xml
18
pom.xml
@@ -5,12 +5,12 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>3.3.5</version>
|
<version>3.4.0</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>dev.mars3142.fhq</groupId>
|
<groupId>dev.mars3142.fhq</groupId>
|
||||||
<artifactId>timezone-service</artifactId>
|
<artifactId>timezone-service</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<name>timezone-service</name>
|
<name>timezone-service</name>
|
||||||
<description>TimeZone Service</description>
|
<description>TimeZone Service</description>
|
||||||
<url/>
|
<url/>
|
||||||
@@ -28,8 +28,8 @@
|
|||||||
</scm>
|
</scm>
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
<spring-modulith.version>1.2.5</spring-modulith.version>
|
<spring-modulith.version>1.3.0</spring-modulith.version>
|
||||||
<spring-cloud.version>2023.0.3</spring-cloud.version>
|
<spring-cloud.version>2024.0.0</spring-cloud.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -54,6 +54,10 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-commons</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.modulith</groupId>
|
<groupId>org.springframework.modulith</groupId>
|
||||||
<artifactId>spring-modulith-starter-test</artifactId>
|
<artifactId>spring-modulith-starter-test</artifactId>
|
||||||
@@ -82,10 +86,6 @@
|
|||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-hateoas</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||||
<artifactId>caffeine</artifactId>
|
<artifactId>caffeine</artifactId>
|
||||||
@@ -94,7 +94,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springdoc</groupId>
|
<groupId>org.springdoc</groupId>
|
||||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||||
<version>2.6.0</version>
|
<version>2.7.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service;
|
package dev.mars3142.fhq.timezone;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.config;
|
package dev.mars3142.fhq.timezone.config;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.config;
|
package dev.mars3142.fhq.timezone.config;
|
||||||
|
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
import io.swagger.v3.oas.models.info.Info;
|
import io.swagger.v3.oas.models.info.Info;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.config;
|
package dev.mars3142.fhq.timezone.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.exceptions;
|
package dev.mars3142.fhq.timezone.exceptions;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
@@ -1,17 +1,18 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.controllers;
|
package dev.mars3142.fhq.timezone.timezone.controllers;
|
||||||
|
|
||||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
|
import dev.mars3142.fhq.timezone.timezone.domain.model.response.TimezoneResponse;
|
||||||
|
import dev.mars3142.fhq.timezone.timezone.service.TimezoneService;
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.model.response.LocationResponse;
|
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.model.response.TimezoneResponse;
|
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.service.TimezoneService;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Sort.Direction;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@@ -25,23 +26,28 @@ public class TimezoneController {
|
|||||||
@GetMapping
|
@GetMapping
|
||||||
@ResponseStatus(HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public TimezoneResponse getTimeZone(
|
public TimezoneResponse getTimeZone(
|
||||||
@RequestHeader(value = "X-Forwarded-For", defaultValue = "127.0.0.1") String header) {
|
@RequestHeader(value = "X-Forwarded-For", defaultValue = "127.0.0.1") String header_ip) {
|
||||||
val clientIp = header.split(",")[0];
|
val clientIp = header_ip.split(",")[0];
|
||||||
val ip = timeZoneService.getExternalIp(clientIp);
|
val ip = timeZoneService.getExternalIp(clientIp);
|
||||||
val timezoneInfo = timeZoneService.getTimeZoneInfoByIp(ip);
|
val timezoneInfo = timeZoneService.getTimeZoneInfoByIp(ip);
|
||||||
val posix = timeZoneService.getPosixTimeZone(timezoneInfo.timezone());
|
val posix = timeZoneService.getPosixTimeZone(timezoneInfo.timezone());
|
||||||
val response = new TimezoneResponse();
|
val response = new TimezoneResponse();
|
||||||
response.setTimezone(timezoneInfo.timezone());
|
response.setTimezone(timezoneInfo.timezone());
|
||||||
response.setPosix_tz(posix);
|
response.setPosix_tz(posix);
|
||||||
response.add(linkTo(TimezoneController.class).slash(ip).withSelfRel());
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("{area}")
|
@GetMapping("{area}")
|
||||||
@ResponseStatus(HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public LocationResponse getLocations(@PathVariable String area) {
|
public Page<String> getLocations(@PathVariable String area,
|
||||||
val locations = timeZoneService.getLocations(area);
|
@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
|
||||||
return new LocationResponse(locations.size(), locations);
|
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
|
||||||
|
@RequestParam(value = "direction", required = false) String direction) {
|
||||||
|
var pageRequest = PageRequest.of(page, pageSize);
|
||||||
|
if (direction != null && !direction.isEmpty()) {
|
||||||
|
pageRequest = PageRequest.of(page, pageSize, Direction.fromString(direction), "location");
|
||||||
|
}
|
||||||
|
return timeZoneService.getPagedLocations(area, pageRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("{area}/{location}")
|
@GetMapping("{area}/{location}")
|
||||||
@@ -52,7 +58,6 @@ public class TimezoneController {
|
|||||||
val response = new TimezoneResponse();
|
val response = new TimezoneResponse();
|
||||||
response.setTimezone(timezone);
|
response.setTimezone(timezone);
|
||||||
response.setPosix_tz(posix);
|
response.setPosix_tz(posix);
|
||||||
response.add(linkTo(TimezoneController.class).slash(area).slash(location).withSelfRel());
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.domain.entities.response;
|
package dev.mars3142.fhq.timezone.timezone.domain.entities.response;
|
||||||
|
|
||||||
public record IPApiResponse(String status, String country, String countryCode, String region, String regionName,
|
public record IPApiResponse(String status, String country, String countryCode, String region, String regionName,
|
||||||
String city, String zip, String lat, String lon, String timezone, String isp, String or,
|
String city, String zip, String lat, String lon, String timezone, String isp, String or,
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package dev.mars3142.fhq.timezone.timezone.domain.entities.response;
|
||||||
|
|
||||||
|
public record IpifyResponse(String ip) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.domain.entities.response;
|
package dev.mars3142.fhq.timezone.timezone.domain.entities.response;
|
||||||
|
|
||||||
public record TimeApiTimezoneZoneResponse(Interval dstInterval) {
|
public record TimeApiTimezoneZoneResponse(Interval dstInterval) {
|
||||||
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package dev.mars3142.fhq.timezone.timezone.domain.model.response;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class TimezoneResponse {
|
||||||
|
|
||||||
|
private String timezone;
|
||||||
|
private String posix_tz;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package dev.mars3142.fhq.timezone.timezone.service;
|
||||||
|
|
||||||
|
import dev.mars3142.fhq.timezone.timezone.domain.entities.response.IPApiResponse;
|
||||||
|
import dev.mars3142.fhq.timezone.timezone.domain.entities.response.TimeApiTimezoneZoneResponse;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
|
||||||
|
public interface TimezoneService {
|
||||||
|
|
||||||
|
String getExternalIp(String ip);
|
||||||
|
|
||||||
|
IPApiResponse getTimeZoneInfoByIp(String ip);
|
||||||
|
|
||||||
|
TimeApiTimezoneZoneResponse getTimeZoneInfo(String timezone);
|
||||||
|
|
||||||
|
String getPosixTimeZone(String timezone);
|
||||||
|
|
||||||
|
Page<String> getPagedLocations(String area, PageRequest pageRequest);
|
||||||
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.service.impl;
|
package dev.mars3142.fhq.timezone.timezone.service.impl;
|
||||||
|
|
||||||
import dev.mars3142.fhq.timezone_service.exceptions.NotFoundException;
|
import dev.mars3142.fhq.timezone.exceptions.NotFoundException;
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.IPApiResponse;
|
import dev.mars3142.fhq.timezone.timezone.domain.entities.response.IPApiResponse;
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.IpifyResponse;
|
import dev.mars3142.fhq.timezone.timezone.domain.entities.response.IpifyResponse;
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.TimeApiTimezoneZoneResponse;
|
import dev.mars3142.fhq.timezone.timezone.domain.entities.response.TimeApiTimezoneZoneResponse;
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.service.TimezoneService;
|
import dev.mars3142.fhq.timezone.timezone.service.TimezoneService;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -15,6 +15,9 @@ import java.util.stream.Stream;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageImpl;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.http.HttpStatusCode;
|
import org.springframework.http.HttpStatusCode;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
@@ -48,18 +51,18 @@ public class TimezoneServiceImpl implements TimezoneService {
|
|||||||
@Override
|
@Override
|
||||||
@Cacheable(value = "TZInfoByIp", key = "{#ip}")
|
@Cacheable(value = "TZInfoByIp", key = "{#ip}")
|
||||||
public IPApiResponse getTimeZoneInfoByIp(String ip) {
|
public IPApiResponse getTimeZoneInfoByIp(String ip) {
|
||||||
return restClient
|
return restClient
|
||||||
.get()
|
.get()
|
||||||
.uri(builder -> builder
|
.uri(builder -> builder
|
||||||
.scheme("http")
|
.scheme("http")
|
||||||
.host("ip-api.com")
|
.host("ip-api.com")
|
||||||
.path("json/" + ip)
|
.path("json/" + ip)
|
||||||
.build())
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
|
.onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
|
||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
})
|
})
|
||||||
.body(IPApiResponse.class);
|
.body(IPApiResponse.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -97,19 +100,41 @@ public class TimezoneServiceImpl implements TimezoneService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(value = "locations", key = "{#area}")
|
@Cacheable(value = "locations", key = "{#area, #pageRequest}")
|
||||||
public List<String> getLocations(String area) {
|
public Page<String> getPagedLocations(String area, PageRequest pageRequest) {
|
||||||
val directory = new File("/usr/share/zoneinfo/" + area);
|
val directory = new File("/usr/share/zoneinfo/" + area);
|
||||||
if (!directory.exists()) {
|
if (!directory.exists()) {
|
||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
}
|
}
|
||||||
return Stream.of(Objects.requireNonNull(directory.listFiles()))
|
return toPage(Stream.of(Objects.requireNonNull(directory.listFiles()))
|
||||||
.filter(file -> !file.isDirectory())
|
.filter(file -> !file.isDirectory())
|
||||||
.map(file -> {
|
.map(file -> {
|
||||||
val path = file.getPath().split("/");
|
val path = file.getPath().split("/");
|
||||||
return path[path.length - 2] + "/" + path[path.length - 1];
|
return path[path.length - 2] + "/" + path[path.length - 1];
|
||||||
})
|
})
|
||||||
.sorted()
|
.toList(), pageRequest);
|
||||||
.toList();
|
}
|
||||||
|
|
||||||
|
Page<String> toPage(List<String> list, PageRequest pageRequest) {
|
||||||
|
val pageSize = pageRequest.getPageSize();
|
||||||
|
val pageNumber = pageRequest.getPageNumber();
|
||||||
|
val totalPages = list.size() / pageSize;
|
||||||
|
|
||||||
|
int max = pageNumber >= totalPages ? list.size() : pageSize * (pageNumber + 1);
|
||||||
|
int min = pageNumber > totalPages ? max : pageSize * pageNumber;
|
||||||
|
|
||||||
|
var content = list.stream().sorted((left, right) -> {
|
||||||
|
if (!pageRequest.getSort().isSorted()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Objects.requireNonNull(pageRequest.getSort().getOrderFor("location")).isAscending()) {
|
||||||
|
return left.compareTo(right);
|
||||||
|
} else {
|
||||||
|
return right.compareTo(left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return new PageImpl<>(content.toList().subList(min, max), pageRequest, list.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.domain.entities.response;
|
|
||||||
|
|
||||||
public record IpifyResponse(String ip) {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.domain.model.response;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public record LocationResponse(int count, List<String> locations) {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.domain.model.response;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.springframework.hateoas.RepresentationModel;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class TimezoneResponse extends RepresentationModel<TimezoneResponse> {
|
|
||||||
|
|
||||||
private String timezone;
|
|
||||||
private String posix_tz;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.service;
|
|
||||||
|
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.IPApiResponse;
|
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.TimeApiTimezoneZoneResponse;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface TimezoneService {
|
|
||||||
|
|
||||||
String getExternalIp(String ip);
|
|
||||||
|
|
||||||
IPApiResponse getTimeZoneInfoByIp(String ip);
|
|
||||||
|
|
||||||
TimeApiTimezoneZoneResponse getTimeZoneInfo(String timezone);
|
|
||||||
|
|
||||||
String getPosixTimeZone(String timezone);
|
|
||||||
|
|
||||||
List<String> getLocations(String area);
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
eureka:
|
eureka:
|
||||||
client:
|
client:
|
||||||
register-with-eureka: true
|
|
||||||
fetch-registry: true
|
fetch-registry: true
|
||||||
|
register-with-eureka: true
|
||||||
service-url:
|
service-url:
|
||||||
defaultZone: ${EUREKA}
|
defaultZone: ${EUREKA}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service;
|
package dev.mars3142.fhq.timezone;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.service.impl;
|
package dev.mars3142.fhq.timezone.timezone.service.impl;
|
||||||
|
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@@ -59,6 +59,6 @@ class TimezoneServiceImplTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getLocations() {
|
void getPagedLocations() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.mars3142.fhq.timezone_service.timezone.web.controllers;
|
package dev.mars3142.fhq.timezone.timezone.web.controllers;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
10
src/test/resources/bootstrap.yml
Normal file
10
src/test/resources/bootstrap.yml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
eureka:
|
||||||
|
client:
|
||||||
|
enabled: false
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: timezone-service
|
||||||
|
cloud:
|
||||||
|
config:
|
||||||
|
enabled: false
|
||||||
Reference in New Issue
Block a user