diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 86e9c48..f7c59f6 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -3,14 +3,14 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent plugins { id("java") - id("application") - id("checkstyle") - id("jacoco") + application + checkstyle + jacoco id("com.github.johnrengelman.shadow") version "8.1.1" id("se.patrikerdes.use-latest-versions") version "0.2.18" - id("com.github.ben-manes.versions") version "0.51.0" + id("com.github.ben-manes.versions") version "0.52.0" id("org.sonarqube") version "6.2.0.5505" } @@ -28,11 +28,10 @@ repositories { dependencies { // Javalin - implementation("io.javalin:javalin:6.5.0") - implementation("io.javalin:javalin-rendering:6.5.0") - implementation("org.slf4j:slf4j-simple:2.0.16") - implementation("gg.jte:jte:3.1.16") - + implementation("io.javalin:javalin:6.7.0") + implementation("io.javalin:javalin-rendering:6.7.0") + implementation("org.slf4j:slf4j-simple:2.1.0-alpha1") + implementation("gg.jte:jte:3.2.1") // LOMBOK compileOnly("org.projectlombok:lombok:1.18.38") @@ -41,17 +40,23 @@ dependencies { testCompileOnly("org.projectlombok:lombok:1.18.38") testAnnotationProcessor("org.projectlombok:lombok:1.18.38") + // CheckStyle + implementation("com.puppycrawl.tools:checkstyle:10.26.0") + // database H2 & HikariCP + implementation("com.h2database:h2:2.3.232") + implementation("com.zaxxer:HikariCP:6.3.0") - // CheckStyle - implementation("com.puppycrawl.tools:checkstyle:10.23.1") + // Tests + testImplementation("org.junit.jupiter:junit-jupiter:5.13.2") + testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.13.2") - testImplementation(platform("org.junit:junit-bom:5.10.0")) - testImplementation("org.junit.jupiter:junit-jupiter") + //testImplementation(platform("org.junit:junit-bom:5.13.2")) + //testImplementation("org.junit.jupiter:junit-jupiter") } checkstyle { - toolVersion = "10.23.1" + toolVersion = "10.26.0" configFile = file("config/checkstyle/checkstyle.xml") } diff --git a/app/src/main/java/hexlet/code/App.java b/app/src/main/java/hexlet/code/App.java index a9f713b..5bb3852 100644 --- a/app/src/main/java/hexlet/code/App.java +++ b/app/src/main/java/hexlet/code/App.java @@ -1,14 +1,56 @@ package hexlet.code; +import hexlet.code.repository.BaseRepository; +// import io.hexlet.component.DataInitializer; + import io.javalin.Javalin; import io.javalin.rendering.template.JavalinJte; import lombok.extern.slf4j.Slf4j; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.sql.SQLException; +import java.util.stream.Collectors; + @Slf4j public class App { - public static Javalin getApp() { //throws SQLException + + + private static String getDatabaseUrl() { + // Получаю url базы данных из переменной окружения DATABASE_URL + // Если она не установлена, используем базу в памяти + return System.getenv().getOrDefault("JDBC_DATABASE_URL", "jdbc:h2:mem:project;DB_CLOSE_DELAY=-1;"); + } + + + private static String readResourceFile(String fileName) throws IOException { + var inputStream = App.class.getClassLoader().getResourceAsStream(fileName); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { + return reader.lines().collect(Collectors.joining("\n")); + } + } + + public static Javalin getApp() throws IOException, SQLException { // + HikariConfig hikariConfig = new HikariConfig(); + hikariConfig.setJdbcUrl(getDatabaseUrl()); + + HikariDataSource dataSource = new HikariDataSource(hikariConfig); + var sql = readResourceFile("schema.sql"); + + log.info(sql); + try (var connection = dataSource.getConnection(); + var statement = connection.createStatement()) { + statement.execute(sql); + } + BaseRepository.dataSource = dataSource; + var app = Javalin.create(config -> { config.bundledPlugins.enableDevLogging(); @@ -21,7 +63,8 @@ public static Javalin getApp() { //throws SQLException return app; } - public static void main(String[] args) { //throws SQLException + + public static void main(String[] args) throws IOException, SQLException { //throws SQLException Javalin app = getApp(); app.start(7070); } diff --git a/app/src/main/java/hexlet/code/model/Url.java b/app/src/main/java/hexlet/code/model/Url.java new file mode 100644 index 0000000..c32515c --- /dev/null +++ b/app/src/main/java/hexlet/code/model/Url.java @@ -0,0 +1,21 @@ +package hexlet.code.model; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import java.time.LocalDateTime; + +@Getter +@Setter +@ToString +public class Url { + private Long id; + private String name; + private LocalDateTime createdAt; + + public Url(String name, LocalDateTime createdAt) { + this.name = name; + this.createdAt = createdAt; + } +} diff --git a/app/src/main/java/hexlet/code/repository/BaseRepository.java b/app/src/main/java/hexlet/code/repository/BaseRepository.java new file mode 100644 index 0000000..9ecf5f2 --- /dev/null +++ b/app/src/main/java/hexlet/code/repository/BaseRepository.java @@ -0,0 +1,7 @@ +package hexlet.code.repository; + +import com.zaxxer.hikari.HikariDataSource; + +public class BaseRepository { + public static HikariDataSource dataSource; +} diff --git a/app/src/main/resources/schema.sql b/app/src/main/resources/schema.sql new file mode 100644 index 0000000..fbbefb5 --- /dev/null +++ b/app/src/main/resources/schema.sql @@ -0,0 +1,7 @@ +DROP TABLE IF EXISTS urls; + +CREATE TABLE urls ( + id INT PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + created_at TIMESTAMP +); \ No newline at end of file