From 29daad6d3c55303431688a30a20957e60fd1afb8 Mon Sep 17 00:00:00 2001 From: Dave Davis Date: Mon, 14 Mar 2022 22:09:55 +0000 Subject: [PATCH] Add web controller to accept URL and call declutter service. Return parsable kdoc. Refactor kdoc-tempalte Init CICD process --- pom.xml | 12 ++++ .../io/klutter/controllers/WebController.java | 55 +++++++++++++++++-- .../io/klutter/services/DeclutterService.java | 5 +- src/main/resources/templates/index.html | 3 +- .../{kdoc.html => kdoc-template.html} | 29 +++++++++- 5 files changed, 92 insertions(+), 12 deletions(-) rename src/main/resources/templates/{kdoc.html => kdoc-template.html} (88%) diff --git a/pom.xml b/pom.xml index ee4c36d..e445ab6 100644 --- a/pom.xml +++ b/pom.xml @@ -154,6 +154,18 @@ 20210307 + + + + + + + + org.springframework.boot + spring-boot-starter-webflux + + + diff --git a/src/main/java/io/klutter/controllers/WebController.java b/src/main/java/io/klutter/controllers/WebController.java index 4714a8b..adf1804 100644 --- a/src/main/java/io/klutter/controllers/WebController.java +++ b/src/main/java/io/klutter/controllers/WebController.java @@ -1,19 +1,21 @@ package io.klutter.controllers; import io.klutter.models.Kdoc; -import io.klutter.services.KdocService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; - +import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.reactive.function.client.WebClient; @Controller @RequestMapping("/") public class WebController { + // ToDo: Proper form: https://www.wimdeblauwe.com/blog/2021/05/23/form-handling-with-thymeleaf/ + + WebClient webClient = WebClient.create("http://localhost:8080/declutter"); + + /* Homepage. Added ModelAttribute, so I could use ThymeLeaf and have the user submit a URL as a Kdoc object right from the homepage form.*/ @@ -23,6 +25,47 @@ public String index(@ModelAttribute Kdoc kdoc, Model model) { return "index"; } + + private final String URI_DECLUTTER = "http://localhost:8080/declutter"; + + +// @PostMapping() +// public ModelAndView homeDeclutter(@ModelAttribute ("Kdoc") Kdoc kdoc, Model model) { +// System.out.println(kdoc); +// RestTemplate restTemplate = new RestTemplate(); +// Kdoc createdKdoc = restTemplate.postForObject(URI_DECLUTTER, kdoc, Kdoc.class); +// System.out.println(createdKdoc); +// ModelAndView mav = new ModelAndView("kdoc"); +// +// model.addAttribute("dave", createdKdoc); +// mav.addObject("kdoc", createdKdoc); +// +// return mav; +// +// } + + + @PostMapping() + public String homeDeclutter(@ModelAttribute ("Kdoc") Kdoc kdoc, Model model) { + System.out.println(kdoc); + RestTemplate restTemplate = new RestTemplate(); + Kdoc createdKdoc = restTemplate.postForObject(URI_DECLUTTER, kdoc, Kdoc.class); + System.out.println(createdKdoc); + model.addAttribute("createdKdoc", createdKdoc); + + return "kdoc-template"; + + } + + + + + + + + + + // private final KdocService kdocService; // // public WebController(KdocService kdocService) { diff --git a/src/main/java/io/klutter/services/DeclutterService.java b/src/main/java/io/klutter/services/DeclutterService.java index 025a518..51ba061 100644 --- a/src/main/java/io/klutter/services/DeclutterService.java +++ b/src/main/java/io/klutter/services/DeclutterService.java @@ -36,7 +36,8 @@ public class DeclutterService { @PostMapping(value = "/declutter", produces = {"application/json"}) // public String declutter(@ModelAttribute Kdoc kdoc, Model model) { - ResponseEntity declutter(@ModelAttribute Kdoc kdoc, Model model) { +// ResponseEntity declutter(@ModelAttribute Kdoc kdoc, Model model) { + ResponseEntity declutter(@RequestBody Kdoc kdoc){ // model.addAttribute("kdoc", kdoc); System.out.println(kdoc); String url = kdoc.getUrl(); @@ -49,7 +50,7 @@ ResponseEntity declutter(@ModelAttribute Kdoc kdoc, Model model) { else { System.out.println("This is a new URL"); - ChromeOptions options = new ChromeOptions();options.addArguments("--headless"); + ChromeOptions options = new ChromeOptions().addArguments("--headless"); // Using Webdriver WebDriverManager.chromedriver().setup(); diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index ce7ffb5..7e51f0d 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -55,7 +55,8 @@

Welcome to Klutter

Submit your URL below to get a decluttered, ad-free version optimized for e-ink devices like remarkable or kindle.

-
+ +
diff --git a/src/main/resources/templates/kdoc.html b/src/main/resources/templates/kdoc-template.html similarity index 88% rename from src/main/resources/templates/kdoc.html rename to src/main/resources/templates/kdoc-template.html index c0cc2a7..7cd5b6b 100644 --- a/src/main/resources/templates/kdoc.html +++ b/src/main/resources/templates/kdoc-template.html @@ -1,9 +1,25 @@ - - + + + + + + + + + + + Klutter.io - Document Management for E-Ink Fans + + + + + + + - <!--Kdoc title Goes Here--> +
+

+
+ + + +
\ No newline at end of file