Skip to content

Commit

Permalink
ДП. Часть 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nnemich committed Dec 1, 2023
1 parent 41c6ea8 commit 5ceb59a
Show file tree
Hide file tree
Showing 31 changed files with 775 additions and 10 deletions.
47 changes: 41 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
version: '3.1'
services:
stats-server:
server:
build: stat-service/server
image: server
container_name: server
ports:
- "9090:9090"
- "9091:9091"
depends_on:
- statdb
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://statdb:5432/statdb
- SPRING_DATASOURCE_USERNAME=ewm
- SPRING_DATASOURCE_PASSWORD=ewm

stats-db:
image: postgres:14-alpine
statdb:
image: postgres:13.7-alpine
container_name: statdb
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=ewm
- POSTGRES_USER=ewm
- POSTGRES_DB=statdb

ewm-service:
main-service:
build: main-service
image: main-service
container_name: main-service
ports:
- "8080:8080"
depends_on:
- main-db
- server
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://main-db:5432/main-db
- SPRING_DATASOURCE_USERNAME=ewm
- SPRING_DATASOURCE_PASSWORD=ewm
- STATS_SERVER_URL=http://server:9090

ewm-db:
image: postgres:14-alpine
main-db:
image: postgres:13.7-alpine
container_name: main-db
ports:
- "6542:5432"
environment:
- POSTGRES_PASSWORD=ewm
- POSTGRES_USER=ewm
- POSTGRES_DB=main-db
2 changes: 1 addition & 1 deletion ewm-main-service-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2833,4 +2833,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion ewm-stats-service-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@
}
}
}
}
}
3 changes: 3 additions & 0 deletions main-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM amazoncorretto:11-alpine-jdk
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
57 changes: 57 additions & 0 deletions main-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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>main-service</artifactId>

<dependencies>
<dependency>
<groupId>ru.practicum</groupId>
<artifactId>client</artifactId>
<version>${project.version}</version>
</dependency>

<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>

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

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
11 changes: 11 additions & 0 deletions main-service/src/main/java/ru/practicum/ewm/EWMMainServiceApp.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 EWMMainServiceApp {
public static void main(String[] args) {
SpringApplication.run(EWMMainServiceApp.class, args);
}
}
24 changes: 24 additions & 0 deletions main-service/src/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
server.port=8080
STAT_SERVER_URL=http://localhost:9090

spring.application.name=ewm-main-service
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL10Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.sql.init.mode=always
#---
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:6542/main-db
spring.datasource.username=ewm
spring.datasource.password=ewm
#---
spring.config.activate.on-profile=local
spring.datasource.url=jdbc:postgresql://localhost:6542/postgres
stats-server.url=http://localhost:9090
#---
spring.config.activate.on-profile=ci,test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:main-db
spring.datasource.username=ewm
spring.datasource.password=ewm
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -11,6 +11,10 @@
</parent>

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

<groupId>ru.practicum</groupId>
<artifactId>explore-with-me</artifactId>
Expand Down Expand Up @@ -183,5 +187,14 @@
</build>
</profile>
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>

</project>
</dependencyManagement>
</project>
34 changes: 34 additions & 0 deletions stat-service/client/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>stat-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>client</artifactId>

<dependencies>
<dependency>
<groupId>ru.practicum</groupId>
<artifactId>dto</artifactId>
<version>${project.version}</version>
</dependency>

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

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

</project>
59 changes: 59 additions & 0 deletions stat-service/client/src/main/java/ru/practicum/ewm/BaseClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ru.practicum.ewm;

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

import java.util.List;
import java.util.Map;

public class BaseClient {
protected final RestTemplate restTemplate;

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

protected <T> ResponseEntity<Object> post(T body) {
return makeAndSendRequest(HttpMethod.POST, "/hit", null, body);
}

protected <T> ResponseEntity<Object> get(String path, @Nullable Map<String, Object> parameters) {
return makeAndSendRequest(HttpMethod.GET, path, parameters, null);
}

private <T> ResponseEntity<Object> makeAndSendRequest(HttpMethod method, String path, @Nullable Map<String, Object> parameters, @Nullable T body) {
HttpEntity<T> requestEntity = new HttpEntity<>(body, defaultHeaders());

ResponseEntity<Object> exploreWithMeServerResponse;
try {
if (parameters != null) {
exploreWithMeServerResponse = restTemplate.exchange(path, method, requestEntity, Object.class, parameters);
} else {
exploreWithMeServerResponse = restTemplate.exchange(path, method, requestEntity, Object.class);
}
} catch (HttpStatusCodeException e) {
return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsByteArray());
}
return prepareResponse(exploreWithMeServerResponse);
}

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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ru.practicum.ewm;

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.web.util.DefaultUriBuilderFactory;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

public class StatsClient extends BaseClient {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Constants.DATE_FORMAT);

@Value("${server.application.name:ewm-main-service}")
private String applicationName;

public StatsClient(@Value("${server.url}") String serverUrl, RestTemplateBuilder builder) {
super(
builder
.uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl))
.requestFactory(HttpComponentsClientHttpRequestFactory::new)
.build()
);
}

public ResponseEntity<Object> saveHit(HttpServletRequest request) {
final EndpointHit hit = EndpointHit.builder()
.app(applicationName)
.uri(request.getRequestURI())
.ip(request.getRemoteAddr())
.timestamp(Timestamp.from(Instant.now()).toLocalDateTime())
.build();
return post(hit);
}

public ResponseEntity<Object> getHit(LocalDateTime start, LocalDateTime end, List<String> uris, Boolean unique) {
StringBuilder uriBuilder = new StringBuilder("/stats?start={start}&end={end}");
Map<String, Object> parameters = Map.of(
"start", start.format(formatter),
"end", end.format(formatter)
);

if (uris != null) {
parameters.put("uris", String.join(",", uris));
}
if (unique) {
parameters.put("unique", true);
}
return get(uriBuilder.toString(), parameters);
}
}
27 changes: 27 additions & 0 deletions stat-service/dto/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>stat-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dto</artifactId>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

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

0 comments on commit 5ceb59a

Please sign in to comment.