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
39 changes: 39 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
services:
gateway:
build: gateway
image: shareit-gateway
container_name: shareit-gateway
ports:
- "8080:8080"
depends_on:
- server
environment:
- SHAREIT_SERVER_URL=http://server:9090

server:
build: server
image: shareit-server
container_name: shareit-server
ports:
- "9090:9090"
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit
- SPRING_DATASOURCE_USERNAME=shareit
- SPRING_DATASOURCE_PASSWORD=shareit

db:
image: postgres:16.1
container_name: postgres
ports:
- "6541:5432"
environment:
- POSTGRES_PASSWORD=shareit
- POSTGRES_USER=shareit
- POSTGRES_DB=shareit
healthcheck:
test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER
timeout: 5s
interval: 5s
retries: 10
5 changes: 5 additions & 0 deletions gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM amazoncorretto:21
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar"]
91 changes: 91 additions & 0 deletions gateway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.practicum</groupId>
<artifactId>shareit</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>ShareIt Gateway</name>

<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.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</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>

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

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin.version}</version>

<configuration>
<failOnViolation>true</failOnViolation>
<logViolationsToConsole>true</logViolationsToConsole>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<configLocation>checkstyle.xml</configLocation>
</configuration>

<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
4 changes: 4 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/OnCreate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.practicum.shareit;

public interface OnCreate {
}
4 changes: 4 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/OnUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.practicum.shareit;

public interface OnUpdate {
}
12 changes: 12 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/ShareItGateway.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.practicum.shareit;

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

@SpringBootApplication
public class ShareItGateway {

public static void main(String[] args) {
SpringApplication.run(ShareItGateway.class, args);
}
}
13 changes: 13 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.practicum.shareit;

public class Util {
public static final String USER_ID_HEADER = "X-Sharer-User-Id";

public static final String EMPTY_PATH = "";

public static final String USERS_PATH = "/users";
public static final String ITEMS_PATH = "/items";
public static final String BOOKING_PATH = "/bookings";
public static final String REQUEST_PATH = "/requests";

}
141 changes: 141 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/client/BaseClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package ru.practicum.shareit.client;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.lang.Nullable;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;

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

public class BaseClient {

@Value("${shareit-server.url}")
private String serverUrl;

private final String prefix;

protected RestTemplate rest;

@PostConstruct
public void init() {
RestTemplateBuilder builder = new RestTemplateBuilder();

this.rest = builder.uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + prefix))
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory())
.build();
}

public BaseClient(String prefix) {
this.prefix = prefix;
}

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

protected ResponseEntity<Object> get(String path, long userId) {
return get(path, userId, null);
}

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

protected <T> ResponseEntity<Object> post(String path, T body) {
return post(path, null, null, body);
}

protected <T> ResponseEntity<Object> post(String path, long userId, T body) {
return post(path, userId, null, body);
}

protected <T> ResponseEntity<Object> post(String path, Long userId, @Nullable Map<String, Object> parameters, T body) {
return makeAndSendRequest(HttpMethod.POST, path, userId, parameters, body);
}

protected <T> ResponseEntity<Object> put(String path, long userId, T body) {
return put(path, userId, null, body);
}

protected <T> ResponseEntity<Object> put(String path, long userId, @Nullable Map<String, Object> parameters, T body) {
return makeAndSendRequest(HttpMethod.PUT, path, userId, parameters, body);
}

protected <T> ResponseEntity<Object> patch(String path, T body) {
return patch(path, null, null, body);
}

protected <T> ResponseEntity<Object> patch(String path, long userId) {
return patch(path, userId, null, null);
}

protected <T> ResponseEntity<Object> patch(String path, long userId, T body) {
return patch(path, userId, null, body);
}

protected <T> ResponseEntity<Object> patch(String path, long userId, Map<String, Object> parameters) {
return patch(path, userId, parameters, null);
}

protected <T> ResponseEntity<Object> patch(String path, Long userId, @Nullable Map<String, Object> parameters, T body) {
return makeAndSendRequest(HttpMethod.PATCH, path, userId, parameters, body);
}

protected ResponseEntity<Object> delete(String path) {
return delete(path, null, null);
}

protected ResponseEntity<Object> delete(String path, long userId) {
return delete(path, userId, null);
}

protected ResponseEntity<Object> delete(String path, Long userId, @Nullable Map<String, Object> parameters) {
return makeAndSendRequest(HttpMethod.DELETE, path, userId, parameters, null);
}

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

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

private HttpHeaders defaultHeaders(Long userId) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
if (userId != null) {
headers.set("X-Sharer-User-Id", String.valueOf(userId));
}
return headers;
}

private static ResponseEntity<Object> prepareGatewayResponse(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,44 @@
package ru.practicum.shareit.client;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import ru.practicum.shareit.dto.BookingDto;
import ru.practicum.shareit.dto.BookingState;

import java.util.Collections;

import static ru.practicum.shareit.Util.BOOKING_PATH;
import static ru.practicum.shareit.Util.EMPTY_PATH;

@Component
public class BookingClient extends BaseClient {
public BookingClient() {
super(BOOKING_PATH);
}

public ResponseEntity<Object> createBooking(Long userId, BookingDto dto) {
if (!dto.getStart().isBefore(dto.getEnd())) {
return ResponseEntity.badRequest().build();
}

return post(EMPTY_PATH, userId, dto);
}

public ResponseEntity<Object> updateBookingStatus(Long userId, Long bookingId, Boolean approved) {
return patch("/" + bookingId + "?approved={approved}",
userId,
Collections.singletonMap("approved", approved));
}

public ResponseEntity<Object> getBooking(Long userId, Long bookingId) {
return get("/" + bookingId, userId);
}

public ResponseEntity<Object> findByBookerAndState(Long bookerId, BookingState state) {
return get("?state={state}", bookerId, Collections.singletonMap("state", state));
}

public ResponseEntity<Object> findByOwnerAndState(Long ownerId, BookingState state) {
return get("/owner?state={state}", ownerId, Collections.singletonMap("state", state));
}
}
Loading