-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
775 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2833,4 +2833,4 @@ | |
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,4 +167,4 @@ | |
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
main-service/src/main/java/ru/practicum/ewm/EWMMainServiceApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
stat-service/client/src/main/java/ru/practicum/ewm/BaseClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
stat-service/client/src/main/java/ru/practicum/ewm/StatsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.