Browse Source

Now we can get data from user and server

main
Jesus 2 years ago
parent
commit
ec2552b256
3 changed files with 66 additions and 6 deletions
  1. +42
    -5
      src/main/java/com/jesuspinar/webserver/Manager.java
  2. +1
    -1
      src/main/resources/templates/files/time.html
  3. +23
    -0
      src/main/resources/templates/files/whoami.html

+ 42
- 5
src/main/java/com/jesuspinar/webserver/Manager.java View File

@ -3,23 +3,60 @@ package com.jesuspinar.webserver;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.*;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@Controller @Controller
public class Manager { public class Manager {
@GetMapping({"/", "", "/index"}) @GetMapping({"/", "", "/index"})
public String welcome(Model model){
public String welcome(Model model) {
loadTheme(model); loadTheme(model);
model.addAttribute("mesage", "Hello"); model.addAttribute("mesage", "Hello");
return "index"; return "index";
} }
@GetMapping("/hour")
public String hour(Model model){
@GetMapping("/time")
public String hour(Model model) {
loadTheme(model); loadTheme(model);
return "time";
return "files/time";
}
@GetMapping("/whoami")
public String test(Model model) throws MalformedURLException {
loadTheme(model);
InetAddress inetAddress = null;
String hostnameCanonical = null;
String hostname = null;
byte[] address = null;
String currentURL = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString();
URL requestURL = new URL(currentURL);
try {
inetAddress = InetAddress.getByName(InetAddress.getLocalHost().getHostName());
hostnameCanonical = inetAddress.getCanonicalHostName();
address = inetAddress.getAddress();
hostname = inetAddress.getHostName();
} catch (UnknownHostException e) {
model.addAttribute("error", "UnknownHostException");
}
//Data from the user
model.addAttribute("inetAddress", inetAddress);
model.addAttribute("hostname", hostname);
model.addAttribute("hostnameCanonical", hostnameCanonical);
model.addAttribute("address", address);
//Data from the server
model.addAttribute("url", currentURL);
model.addAttribute("protocol", requestURL.getProtocol());
//ToDo: split host info in a class
model.addAttribute("host", requestURL.getHost());
model.addAttribute("port", requestURL.getPort());
return "files/whoami";
} }
private void loadTheme(Model model) { private void loadTheme(Model model) {
@ -28,7 +65,7 @@ public class Manager {
DateTimeFormatter hour = DateTimeFormatter.ofPattern("HH"); DateTimeFormatter hour = DateTimeFormatter.ofPattern("HH");
int h = Integer.parseInt(hour.format(LocalDateTime.now())); int h = Integer.parseInt(hour.format(LocalDateTime.now()));
String bg = "light"; String bg = "light";
if (h >= 20 || h <= 7){
if (h >= 20 || h <= 7) {
bg = "dark"; bg = "dark";
} }
model.addAttribute("background", bg); model.addAttribute("background", bg);


src/main/resources/templates/time.html → src/main/resources/templates/files/time.html View File

@ -10,6 +10,6 @@
</head> </head>
<body> <body>
<h1>The actual date is :</h1> <h1>The actual date is :</h1>
<p th:text="${dateTime}"></p>
<h3 th:text="${dateTime}"></h3>
</body> </body>
</html> </html>

+ 23
- 0
src/main/resources/templates/files/whoami.html View File

@ -0,0 +1,23 @@
<!DOCTYspanE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta httspan-equiv="X-UA-Comspanatible" content="IE=edge">
<meta name="viewspanort" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" tyspane="text/css" media="all" th:href="'../css/'+${background}+'.css'">
<title th:text="${hostname}"></title>
</head>
<body>
<h1>User data</h1>
<p>Internet Address: <span th:text="${inetAddress}"></span></p>
<p>Hostname: <span th:text="${hostname}"></span></p>
<p>Canonical Hostname: <span th:text="${hostnameCanonical}"></span></p>
<p>Address in bytes: <span th:text="${address}"></span></p>
<h1>Server data</h1>
<p>URL: <span th:text="${url}"></span></p>
<p>Protocol: <span th:text="${protocol}"></span></p>
<p>Host: <span th:text="${host}"></span></p>
<p>Port: <span th:text="${port}"></span></p>
</body>
</html>

Loading…
Cancel
Save