Skip to content

Commit

Permalink
Add web controller to accept URL and call declutter service.
Browse files Browse the repository at this point in the history
Return parsable kdoc.
Refactor kdoc-tempalte
Init CICD process
  • Loading branch information
davedavis committed Mar 14, 2022
1 parent 8e4ec8b commit 29daad6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 12 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@
<version>20210307</version>
</dependency>

<!--&lt;!&ndash; For WebClient calling internal services&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>io.projectreactor.netty</groupId>-->
<!-- <artifactId>reactor-netty</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>




</dependencies>
Expand Down
55 changes: 49 additions & 6 deletions src/main/java/io/klutter/controllers/WebController.java
Original file line number Diff line number Diff line change
@@ -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.*/
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/klutter/services/DeclutterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public class DeclutterService {

@PostMapping(value = "/declutter", produces = {"application/json"})
// public String declutter(@ModelAttribute Kdoc kdoc, Model model) {
ResponseEntity<Kdoc> declutter(@ModelAttribute Kdoc kdoc, Model model) {
// ResponseEntity<Kdoc> declutter(@ModelAttribute Kdoc kdoc, Model model) {
ResponseEntity<Kdoc> declutter(@RequestBody Kdoc kdoc){
// model.addAttribute("kdoc", kdoc);
System.out.println(kdoc);
String url = kdoc.getUrl();
Expand All @@ -49,7 +50,7 @@ ResponseEntity<Kdoc> 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();
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ <h1>Welcome to Klutter</h1>
<p class="lead">Submit your URL below to get a decluttered, ad-free version optimized for e-ink devices like
remarkable or kindle.</p>

<form action="#" th:action="@{/declutter}" th:object="${kdoc}" method="post">
<!-- <form action="#" th:action="@{/declutter}" th:object="${kdoc}" method="post">-->
<form action="#" th:action="@{/}" th:object="${kdoc}" method="post">
<div class="mb-3">
<label for="url" class="form-label">URL to declutter</label>
<input name = "url" type="url" th:field="*{url}" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
<!DOCTYPE html>
<html>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://thymeleaf.org">

<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Dave Davis">
<meta name="generator" content="Hugo 0.84.0">
<title th:utext="${createdKdoc.title}">Klutter.io - Document Management for E-Ink Fans</title>

<link rel="canonical" href="https://klutter.io/">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" integrity="sha384-qlmct0AOBiA2VPZkMY3+2WqkHtIQ9lSdAsAn5RUJD/3vA5MKDgSGcdmIv4ycVxyn" crossorigin="anonymous"></script>


<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><!--Kdoc title Goes Here--></title>
<style>
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */

Expand Down Expand Up @@ -403,10 +419,17 @@
</style>
</head>
<body>

<div id="readability-page-1" class="page">

<!--Kdoc Goes Here-->

<H1 th:utext="${createdKdoc.title}" />
<div th:utext="${createdKdoc.content}" />




</div>
</body>
</html>

0 comments on commit 29daad6

Please sign in to comment.