Compare commits
12 Commits
1438b294ef
...
main
Author | SHA1 | Date | |
---|---|---|---|
d651676106
|
|||
ac0a1abe34
|
|||
|
7889bf48da | ||
|
ab230ef715 | ||
|
dfe260f412 | ||
|
577c97f1bb | ||
ee1ab34631
|
|||
2dae9c9e14
|
|||
8000a26aad
|
|||
3c2eea0eb5
|
|||
a184f4a038
|
|||
46dd36a846
|
@@ -4,8 +4,8 @@
|
||||
<envs>
|
||||
<env name="EUREKA" value="http://localhost:8761/eureka" />
|
||||
</envs>
|
||||
<module name="timezone-service" />
|
||||
<option name="SPRING_BOOT_MAIN_CLASS" value="dev.mars3142.fhq.timezone_service.Application" />
|
||||
<module name="website" />
|
||||
<option name="SPRING_BOOT_MAIN_CLASS" value="dev.mars3142.fhq.Application" />
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
|
@@ -3,7 +3,7 @@
|
||||
<module name="timezone-service" />
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="dev.mars3142.fhq.timezone_service.*" />
|
||||
<option name="PATTERN" value="dev.mars3142.fhq.timezone.*" />
|
||||
<option name="ENABLED" value="true" />
|
||||
</pattern>
|
||||
</extension>
|
||||
|
@@ -11,7 +11,7 @@ USER app
|
||||
|
||||
# Copy all needed project files to a folder
|
||||
COPY --chown=app ./.mvn/ .mvn
|
||||
COPY --chown=app ./mvnw ./pom.xml ./app.json ./
|
||||
COPY --chown=app ./mvnw ./pom.xml ./
|
||||
COPY --chown=app ./src ./src
|
||||
|
||||
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/opentelemetry-javaagent.jar /usr/app/opentelemetry-javaagent.jar
|
||||
|
||||
ENV JAVA_TOOL_OPTIONS="-javaagent:/usr/app/opentelemetry-javaagent.jar"
|
||||
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
|
||||
HEALTHCHECK CMD curl --fail http://localhost:8080/actuator/health || exit 1
|
||||
|
||||
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
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
16
pom.xml
16
pom.xml
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.3.5</version>
|
||||
<version>3.4.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>dev.mars3142.fhq</groupId>
|
||||
@@ -28,8 +28,8 @@
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<spring-modulith.version>1.2.5</spring-modulith.version>
|
||||
<spring-cloud.version>2023.0.3</spring-cloud.version>
|
||||
<spring-modulith.version>1.3.0</spring-modulith.version>
|
||||
<spring-cloud.version>2024.0.0</spring-cloud.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -54,6 +54,10 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.modulith</groupId>
|
||||
<artifactId>spring-modulith-starter-test</artifactId>
|
||||
@@ -82,10 +86,6 @@
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
@@ -94,7 +94,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.6.0</version>
|
||||
<version>2.7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<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.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.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.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.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.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_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 dev.mars3142.fhq.timezone.timezone.domain.model.response.TimezoneResponse;
|
||||
import dev.mars3142.fhq.timezone.timezone.service.TimezoneService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
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.RestController;
|
||||
|
||||
@@ -25,23 +26,28 @@ public class TimezoneController {
|
||||
@GetMapping
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public TimezoneResponse getTimeZone(
|
||||
@RequestHeader(value = "X-Forwarded-For", defaultValue = "127.0.0.1") String header) {
|
||||
val clientIp = header.split(",")[0];
|
||||
@RequestHeader(value = "X-Forwarded-For", defaultValue = "127.0.0.1") String header_ip) {
|
||||
val clientIp = header_ip.split(",")[0];
|
||||
val ip = timeZoneService.getExternalIp(clientIp);
|
||||
val timezoneInfo = timeZoneService.getTimeZoneInfoByIp(ip);
|
||||
val posix = timeZoneService.getPosixTimeZone(timezoneInfo.timezone());
|
||||
val response = new TimezoneResponse();
|
||||
response.setTimezone(timezoneInfo.timezone());
|
||||
response.setPosix_tz(posix);
|
||||
response.add(linkTo(TimezoneController.class).slash(ip).withSelfRel());
|
||||
return response;
|
||||
}
|
||||
|
||||
@GetMapping("{area}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public LocationResponse getLocations(@PathVariable String area) {
|
||||
val locations = timeZoneService.getLocations(area);
|
||||
return new LocationResponse(locations.size(), locations);
|
||||
public Page<String> getLocations(@PathVariable String area,
|
||||
@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
|
||||
@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}")
|
||||
@@ -52,7 +58,6 @@ public class TimezoneController {
|
||||
val response = new TimezoneResponse();
|
||||
response.setTimezone(timezone);
|
||||
response.setPosix_tz(posix);
|
||||
response.add(linkTo(TimezoneController.class).slash(area).slash(location).withSelfRel());
|
||||
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,
|
||||
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) {
|
||||
|
@@ -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_service.timezone.domain.entities.response.IPApiResponse;
|
||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.IpifyResponse;
|
||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.TimeApiTimezoneZoneResponse;
|
||||
import dev.mars3142.fhq.timezone_service.timezone.service.TimezoneService;
|
||||
import dev.mars3142.fhq.timezone.exceptions.NotFoundException;
|
||||
import dev.mars3142.fhq.timezone.timezone.domain.entities.response.IPApiResponse;
|
||||
import dev.mars3142.fhq.timezone.timezone.domain.entities.response.IpifyResponse;
|
||||
import dev.mars3142.fhq.timezone.timezone.domain.entities.response.TimeApiTimezoneZoneResponse;
|
||||
import dev.mars3142.fhq.timezone.timezone.service.TimezoneService;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -15,6 +15,9 @@ import java.util.stream.Stream;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import lombok.val;
|
||||
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.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
@@ -97,19 +100,41 @@ public class TimezoneServiceImpl implements TimezoneService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(value = "locations", key = "{#area}")
|
||||
public List<String> getLocations(String area) {
|
||||
@Cacheable(value = "locations", key = "{#area, #pageRequest}")
|
||||
public Page<String> getPagedLocations(String area, PageRequest pageRequest) {
|
||||
val directory = new File("/usr/share/zoneinfo/" + area);
|
||||
if (!directory.exists()) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return Stream.of(Objects.requireNonNull(directory.listFiles()))
|
||||
return toPage(Stream.of(Objects.requireNonNull(directory.listFiles()))
|
||||
.filter(file -> !file.isDirectory())
|
||||
.map(file -> {
|
||||
val path = file.getPath().split("/");
|
||||
return path[path.length - 2] + "/" + path[path.length - 1];
|
||||
})
|
||||
.sorted()
|
||||
.toList();
|
||||
.toList(), pageRequest);
|
||||
}
|
||||
|
||||
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:
|
||||
client:
|
||||
register-with-eureka: true
|
||||
fetch-registry: true
|
||||
register-with-eureka: true
|
||||
service-url:
|
||||
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.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 org.junit.jupiter.api.BeforeEach;
|
||||
@@ -59,6 +59,6 @@ class TimezoneServiceImplTest {
|
||||
}
|
||||
|
||||
@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.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