Skip to content

Commit 669c4e8

Browse files
Merge pull request #2 from VictorGotsenko/Step03
Step03 - done
2 parents 378b562 + f482171 commit 669c4e8

5 files changed

Lines changed: 99 additions & 16 deletions

File tree

app/build.gradle.kts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent
33

44
plugins {
55
id("java")
6-
id("application")
7-
id("checkstyle")
8-
id("jacoco")
6+
application
7+
checkstyle
8+
jacoco
99

1010
id("com.github.johnrengelman.shadow") version "8.1.1"
1111

1212
id("se.patrikerdes.use-latest-versions") version "0.2.18"
13-
id("com.github.ben-manes.versions") version "0.51.0"
13+
id("com.github.ben-manes.versions") version "0.52.0"
1414

1515
id("org.sonarqube") version "6.2.0.5505"
1616
}
@@ -28,11 +28,10 @@ repositories {
2828

2929
dependencies {
3030
// Javalin
31-
implementation("io.javalin:javalin:6.5.0")
32-
implementation("io.javalin:javalin-rendering:6.5.0")
33-
implementation("org.slf4j:slf4j-simple:2.0.16")
34-
implementation("gg.jte:jte:3.1.16")
35-
31+
implementation("io.javalin:javalin:6.7.0")
32+
implementation("io.javalin:javalin-rendering:6.7.0")
33+
implementation("org.slf4j:slf4j-simple:2.1.0-alpha1")
34+
implementation("gg.jte:jte:3.2.1")
3635

3736
// LOMBOK
3837
compileOnly("org.projectlombok:lombok:1.18.38")
@@ -41,17 +40,23 @@ dependencies {
4140
testCompileOnly("org.projectlombok:lombok:1.18.38")
4241
testAnnotationProcessor("org.projectlombok:lombok:1.18.38")
4342

43+
// CheckStyle
44+
implementation("com.puppycrawl.tools:checkstyle:10.26.0")
4445

46+
// database H2 & HikariCP
47+
implementation("com.h2database:h2:2.3.232")
48+
implementation("com.zaxxer:HikariCP:6.3.0")
4549

46-
// CheckStyle
47-
implementation("com.puppycrawl.tools:checkstyle:10.23.1")
50+
// Tests
51+
testImplementation("org.junit.jupiter:junit-jupiter:5.13.2")
52+
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.13.2")
4853

49-
testImplementation(platform("org.junit:junit-bom:5.10.0"))
50-
testImplementation("org.junit.jupiter:junit-jupiter")
54+
//testImplementation(platform("org.junit:junit-bom:5.13.2"))
55+
//testImplementation("org.junit.jupiter:junit-jupiter")
5156
}
5257

5358
checkstyle {
54-
toolVersion = "10.23.1"
59+
toolVersion = "10.26.0"
5560
configFile = file("config/checkstyle/checkstyle.xml")
5661
}
5762

app/src/main/java/hexlet/code/App.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
11
package hexlet.code;
22

3+
import hexlet.code.repository.BaseRepository;
4+
// import io.hexlet.component.DataInitializer;
5+
36
import io.javalin.Javalin;
47
import io.javalin.rendering.template.JavalinJte;
58

69
import lombok.extern.slf4j.Slf4j;
710

11+
import com.zaxxer.hikari.HikariConfig;
12+
import com.zaxxer.hikari.HikariDataSource;
13+
14+
import java.io.BufferedReader;
15+
import java.io.IOException;
16+
import java.io.InputStreamReader;
17+
import java.nio.charset.StandardCharsets;
18+
import java.sql.SQLException;
19+
import java.util.stream.Collectors;
20+
821

922
@Slf4j
1023
public class App {
11-
public static Javalin getApp() { //throws SQLException
24+
25+
26+
private static String getDatabaseUrl() {
27+
// Получаю url базы данных из переменной окружения DATABASE_URL
28+
// Если она не установлена, используем базу в памяти
29+
return System.getenv().getOrDefault("JDBC_DATABASE_URL", "jdbc:h2:mem:project;DB_CLOSE_DELAY=-1;");
30+
}
31+
32+
33+
private static String readResourceFile(String fileName) throws IOException {
34+
var inputStream = App.class.getClassLoader().getResourceAsStream(fileName);
35+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
36+
return reader.lines().collect(Collectors.joining("\n"));
37+
}
38+
}
39+
40+
public static Javalin getApp() throws IOException, SQLException { //
41+
HikariConfig hikariConfig = new HikariConfig();
42+
hikariConfig.setJdbcUrl(getDatabaseUrl());
43+
44+
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
45+
var sql = readResourceFile("schema.sql");
46+
47+
log.info(sql);
48+
try (var connection = dataSource.getConnection();
49+
var statement = connection.createStatement()) {
50+
statement.execute(sql);
51+
}
52+
BaseRepository.dataSource = dataSource;
53+
1254

1355
var app = Javalin.create(config -> {
1456
config.bundledPlugins.enableDevLogging();
@@ -21,7 +63,8 @@ public static Javalin getApp() { //throws SQLException
2163
return app;
2264
}
2365

24-
public static void main(String[] args) { //throws SQLException
66+
67+
public static void main(String[] args) throws IOException, SQLException { //throws SQLException
2568
Javalin app = getApp();
2669
app.start(7070);
2770
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package hexlet.code.model;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
import java.time.LocalDateTime;
8+
9+
@Getter
10+
@Setter
11+
@ToString
12+
public class Url {
13+
private Long id;
14+
private String name;
15+
private LocalDateTime createdAt;
16+
17+
public Url(String name, LocalDateTime createdAt) {
18+
this.name = name;
19+
this.createdAt = createdAt;
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package hexlet.code.repository;
2+
3+
import com.zaxxer.hikari.HikariDataSource;
4+
5+
public class BaseRepository {
6+
public static HikariDataSource dataSource;
7+
}

app/src/main/resources/schema.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DROP TABLE IF EXISTS urls;
2+
3+
CREATE TABLE urls (
4+
id INT PRIMARY KEY AUTO_INCREMENT,
5+
name VARCHAR(255) NOT NULL,
6+
created_at TIMESTAMP
7+
);

0 commit comments

Comments
 (0)