From 4c4fcc884d5ee9429d819bd776f67f7c56384489 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 1 Jun 2024 18:16:54 +0100 Subject: [PATCH] move to backend as the frontend (easier to manage and deploy) --- pom.xml | 5 ++ .../config/ThymeleafConfiguration.java | 41 +++++++++++++++ .../backend/controller/IndexController.java | 49 ++++++++++++++++++ .../backend/controller/PasteController.java | 11 +--- src/main/resources/templates/index.html | 50 +++++++++++++++++++ src/main/resources/templates/paste-raw.html | 13 +++++ src/main/resources/templates/paste.html | 34 +++++++++++++ 7 files changed, 194 insertions(+), 9 deletions(-) create mode 100644 src/main/java/cc/fascinated/backend/config/ThymeleafConfiguration.java create mode 100644 src/main/java/cc/fascinated/backend/controller/IndexController.java create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/paste-raw.html create mode 100644 src/main/resources/templates/paste.html diff --git a/pom.xml b/pom.xml index 923cdf2..173dd98 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,11 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-thymeleaf + 3.3.0 + diff --git a/src/main/java/cc/fascinated/backend/config/ThymeleafConfiguration.java b/src/main/java/cc/fascinated/backend/config/ThymeleafConfiguration.java new file mode 100644 index 0000000..7565333 --- /dev/null +++ b/src/main/java/cc/fascinated/backend/config/ThymeleafConfiguration.java @@ -0,0 +1,41 @@ +package cc.fascinated.backend.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.thymeleaf.spring6.SpringTemplateEngine; +import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver; +import org.thymeleaf.spring6.view.ThymeleafViewResolver; + +/** + * @author Fascinated (fascinated7) + */ +@Configuration +@EnableWebMvc +public class ThymeleafConfiguration { + + @Bean + public SpringTemplateEngine templateEngine() { + SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(thymeleafTemplateResolver()); + return templateEngine; + } + + @Bean + public SpringResourceTemplateResolver thymeleafTemplateResolver() { + SpringResourceTemplateResolver templateResolver + = new SpringResourceTemplateResolver(); + templateResolver.setPrefix("classpath:/templates/"); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode("HTML"); + templateResolver.setCacheable(false); + return templateResolver; + } + + @Bean + public ThymeleafViewResolver thymeleafViewResolver() { + ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + return viewResolver; + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/backend/controller/IndexController.java b/src/main/java/cc/fascinated/backend/controller/IndexController.java new file mode 100644 index 0000000..817297e --- /dev/null +++ b/src/main/java/cc/fascinated/backend/controller/IndexController.java @@ -0,0 +1,49 @@ +package cc.fascinated.backend.controller; + +import cc.fascinated.backend.model.Paste; +import cc.fascinated.backend.service.PasteService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author Fascinated (fascinated7) + */ +@Controller +@RequestMapping(value = "/") +public class IndexController { + + private final PasteService pasteService; + + @Autowired + public IndexController(PasteService pasteService) { + this.pasteService = pasteService; + } + + @GetMapping(value = "/") + public String home() { + return "index"; + } + + @GetMapping(value = "/{id}") + public String paste(@PathVariable String id, Model model) { + Paste paste = pasteService.getPaste(id); + model.addAttribute("paste", paste); + model.addAttribute("title", "Paste - " + paste.getId()); + model.addAttribute("rawUrl", "/raw/" + paste.getId()); + return "paste"; + } + + @GetMapping(value = "/raw/{id}") + public String pasteRaw(@PathVariable String id, Model model) { + Paste paste = pasteService.getPaste(id); + model.addAttribute("paste", paste); + model.addAttribute("title", "Paste - " + paste.getId() + " (Raw)"); + return "paste-raw"; + } +} diff --git a/src/main/java/cc/fascinated/backend/controller/PasteController.java b/src/main/java/cc/fascinated/backend/controller/PasteController.java index f60cbb8..c8ed17c 100644 --- a/src/main/java/cc/fascinated/backend/controller/PasteController.java +++ b/src/main/java/cc/fascinated/backend/controller/PasteController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*; import java.util.Map; @RestController -@RequestMapping(value = "/") +@RequestMapping(value = "/api") public class PasteController { private final PasteService pasteService; @@ -20,20 +20,13 @@ public class PasteController { this.pasteService = pasteService; } - @GetMapping(value = "/") - public ResponseEntity home() { - return ResponseEntity.ok(Map.of( - "status", "OK" - )); - } - @PostMapping(value = "/upload", consumes = MediaType.TEXT_PLAIN_VALUE) public ResponseEntity uploadPaste(@RequestBody String content) { String id = pasteService.createPaste(content); return ResponseEntity.ok(Map.of("id", id)); } - @GetMapping(value = "/{id}") + @GetMapping(value = "/paste/{id}") public ResponseEntity getPaste(@PathVariable String id) { Paste paste = pasteService.getPaste(id); return ResponseEntity.ok(paste); diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..5f913cb --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,50 @@ + + + + + + + Paste + + + + + +
+
+ + +
+ +
+ +
+
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/paste-raw.html b/src/main/resources/templates/paste-raw.html new file mode 100644 index 0000000..a3a60d5 --- /dev/null +++ b/src/main/resources/templates/paste-raw.html @@ -0,0 +1,13 @@ + + + + + + + + + +
+ + diff --git a/src/main/resources/templates/paste.html b/src/main/resources/templates/paste.html new file mode 100644 index 0000000..eda99cd --- /dev/null +++ b/src/main/resources/templates/paste.html @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + +
+ +
+ + +
+ + +