Skip to content

Commit

Permalink
adding template engine and a couple of templates
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-iork committed Jun 19, 2024
1 parent a6849c7 commit 7d1fdcc
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .dokerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.gradle
build/
.idea
connection.txt
jte-classes
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.gradle
build/
.idea
connection.txt
jte-classes
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ dependencies {
testImplementation platform('org.junit:junit-bom:5.10.2')
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2'
implementation 'io.javalin:javalin:6.1.6'
implementation 'io.javalin:javalin-rendering:6.1.6'
implementation 'org.slf4j:slf4j-simple:2.0.13'
implementation 'com.zaxxer:HikariCP:5.1.0'
// implementation 'com.h2database:h2:2.2.224'
implementation 'org.postgresql:postgresql:42.7.3'
implementation 'gg.jte:jte:3.1.12'
}

test {
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import gg.jte.ContentType;
import gg.jte.TemplateEngine;
import gg.jte.resolve.ResourceCodeResolver;
import hexlet.code.model.Url;
import hexlet.code.repository.BaseRepository;
import hexlet.code.repository.UrlRepository;
import io.javalin.Javalin;
import io.javalin.rendering.template.JavalinJte;

import java.io.BufferedReader;
import java.io.InputStreamReader;
Expand All @@ -30,6 +34,13 @@ private static String getDBUrl() {
// return Integer.parseInt(port);
// }

private static TemplateEngine createTemplateEngine() {
ClassLoader classLoader = App.class.getClassLoader();
ResourceCodeResolver codeResolver = new ResourceCodeResolver("templates", classLoader);
TemplateEngine templateEngine = TemplateEngine.create(codeResolver, ContentType.Html);
return templateEngine;
}

public static Javalin getApp() throws SQLException {
var hikariConfig = new HikariConfig();
var dbUrl = getDBUrl();
Expand All @@ -54,9 +65,13 @@ public static Javalin getApp() throws SQLException {
throw new SQLException("DB structure was not provided!");
}

var app = Javalin.create(config -> config.bundledPlugins.enableDevLogging());
var app = Javalin.create(config -> {
config.bundledPlugins.enableDevLogging();
config.fileRenderer(new JavalinJte(createTemplateEngine()));
});

app.get("/", ctx -> ctx.result(UrlRepository.getUrls().stream()
app.get("/", ctx -> ctx.render("main.jte"));
app.get("/list", ctx -> ctx.result(UrlRepository.getUrls().stream()
.map(Object::toString)
.collect(Collectors.joining("\n"))));
app.get("/create", ctx -> {
Expand Down
Empty file.
42 changes: 42 additions & 0 deletions src/main/resources/templates/layout.jte
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@import gg.jte.Content

@param Content content


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous">
<title>Page analyzer 📊</title>
</head>
<body>
<header class="bg-black text-white p-2 pt-3">
<p>
<span class="px-3 fs-2">Page analyzer</span>
<a href="/" class="pe-3 text-white">Main page</a>
<a href="/list" class="text-white">Sites list</a>
</p>
</header>
<main>
${content}
</main>
<footer>
<div class="border-top position-fixed top-100 start-50 translate-middle pb-5 w-100 bg-light text-dark">
<p class="text-center pt-3">
This site was created 📝 by
<a href="https://github.com/roman-iork" class="text-black">Roman Iork</a>
during studying 📖 at
<a href="https://ru.hexlet.io" class="text-black">Hexlet</a>
</p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
</body>
</html>
14 changes: 14 additions & 0 deletions src/main/resources/templates/main.jte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

@template.layout(
content = @`
<div class="bg-success text-white p-5 text-center">
<p class="fs-1">Page analyzer</p>
<p class="fs-4">Check SEO information for free</p>
<form>
<input class="form-control-lg w-75" type="text" name="siteAddress" placeholder="Enter site address as in example below 👇">
<input class="form-control-lg bg-black text-white" type="submit" value="Check">
</form>
<p class="fs-5">Example: https://mail.ru</p>
</div>
`
)

0 comments on commit 7d1fdcc

Please sign in to comment.