Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
services:
stats-server:
build:
context: ./stat-svc/stat-server
image: stats-server
container_name: stat-server
ports:
- "9090:9090"
depends_on:
- statdb
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://statdb:5432/statdb
- SPRING_DATASOURCE_USERNAME=admin
- SPRING_DATASOURCE_PASSWORD=admin

stats-db:
statdb:
image: postgres:16.1
container_name: statdb
ports:
- "6541:5432"
environment:
- POSTGRES_PASSWORD=admin
- POSTGRES_USER=admin
- POSTGRES_DB=statdb
healthcheck:
test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER
timeout: 5s
interval: 5s
retries: 10

ewm-service:
build:
context: ./ewm-main-svc
image: ewm-service
container_name: ewm-service
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://ewm-db:5432/ewm-db
SPRING_DATASOURCE_USERNAME: admin
SPRING_DATASOURCE_PASSWORD: admin
ports:
- "8080:8080"
depends_on:
- stats-server
- ewm-db

ewm-db:
image: postgres:16.1
container_name: ewm-db
ports:
- "6542:5432"
environment:
- POSTGRES_DB=ewm-db
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=admin
healthcheck:
test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER
timeout: 5s
interval: 5s
retries: 10
5 changes: 5 additions & 0 deletions ewm-main-svc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM eclipse-temurin:21-jre-jammy
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar"]
50 changes: 50 additions & 0 deletions ewm-main-svc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.practicum</groupId>
<artifactId>explore-with-me</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>ewm-main-svc</artifactId>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

</dependencies>
</project>
11 changes: 11 additions & 0 deletions ewm-main-svc/src/main/java/ru/practicum/ewm/MainApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.practicum.ewm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
7 changes: 7 additions & 0 deletions ewm-main-svc/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
logging.level.org.springframework.web.client.RestTemplate=DEBUG
#logging.level.org.apache.http=DEBUG
#logging.level.httpclient.wire=DEBUG

server.port=8080

stat-server.url=${STAT_SERVER_URL:http://localhost:9090}
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
</parent>

<name>Explore With Me</name>
<modules>
<module>ewm-main-svc</module>
<module>stat-svc</module>
</modules>

<groupId>ru.practicum</groupId>
<artifactId>explore-with-me</artifactId>
Expand Down
34 changes: 34 additions & 0 deletions stat-svc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.practicum</groupId>
<artifactId>explore-with-me</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>stat-svc</artifactId>
<packaging>pom</packaging>

<modules>
<module>stat-client</module>
<module>stat-dto</module>
<module>stat-server</module>
</modules>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

</project>
41 changes: 41 additions & 0 deletions stat-svc/stat-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.practicum</groupId>
<artifactId>explore-with-me</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>stat-client</artifactId>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>ru.practicum</groupId>
<artifactId>stat-dto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.practicum.stat;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriComponentsBuilder;
import ru.practicum.stat.base.BaseClient;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class StatisticsClient extends BaseClient {

private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

private final String appName;

@Autowired
public StatisticsClient(@Value("${stat-server.url}") String serverUrl,
@Value("${app.name}") String appName,
RestTemplateBuilder builder) {
super(
builder
.uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl))
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory())
.build()
);
this.appName = appName;
}

public ResponseEntity<Object> create(HttpServletRequest request) {
EndpointHitCreateDto endpointHitCreateDto = EndpointHitCreateDto.builder()
.app(appName)
.uri(request.getRequestURI())
.ip(request.getRemoteAddr())
.timestamp(LocalDateTime.now())
.build();
return post(endpointHitCreateDto);
}

public ResponseEntity<Object> getStats(LocalDateTime start, LocalDateTime end, List<String> uris, Boolean unique) {
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/stats")
.queryParam("start", start.format(formatter))
.queryParam("end", end.format(formatter))
.queryParam("unique", unique);

if (uris != null && !uris.isEmpty()) {
builder.queryParam("uris", String.join(",", uris));
}
String url = builder.toUriString();
return get(url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.practicum.stat.base;

import org.springframework.http.*;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;

import java.util.List;

public class BaseClient {

protected final RestTemplate rest;

public BaseClient(RestTemplate rest) {
this.rest = rest;
}

protected ResponseEntity<Object> get(String path) {
return makeAndSendRequest(path);
}

protected ResponseEntity<Object> post(Object body) {
HttpEntity<Object> requestEntity = new HttpEntity<>(body);
return rest.postForEntity("/hit", requestEntity, Object.class);
}

private <T> ResponseEntity<Object> makeAndSendRequest(String path) {
HttpEntity<T> requestEntity = new HttpEntity<>(null, defaultHeaders());

ResponseEntity<Object> responseEntity;
try {
responseEntity = rest.exchange(path, HttpMethod.GET, requestEntity, Object.class);
} catch (HttpStatusCodeException e) {
return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsByteArray());
}
return prepareResponse(responseEntity);
}

private HttpHeaders defaultHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
return headers;
}

private static ResponseEntity<Object> prepareResponse(ResponseEntity<Object> response) {
if (response.getStatusCode().is2xxSuccessful()) {
return response;
}
ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.status(response.getStatusCode());
if (response.hasBody()) {
return responseBuilder.body(response.getBody());
}
return responseBuilder.build();
}
}
43 changes: 43 additions & 0 deletions stat-svc/stat-dto/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.practicum</groupId>
<artifactId>explore-with-me</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>stat-dto</artifactId>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Loading