Skip to content

Commit

Permalink
Initial dashboard application
Browse files Browse the repository at this point in the history
  • Loading branch information
bclozel authored and snicoll committed May 24, 2019
0 parents commit 5e33a1c
Show file tree
Hide file tree
Showing 55 changed files with 2,985 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
3 changes: 3 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= Initializr Stats

See https://springoneplatform.io/2018/sessions/spring-boot-2-0-web-applications
25 changes: 25 additions & 0 deletions dashboard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
97 changes: 97 additions & 0 deletions dashboard/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.spring.sample</groupId>
<artifactId>dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dashboard</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>bulma</artifactId>
<version>0.7.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>flatpickr</artifactId>
<version>4.5.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>font-awesome</artifactId>
<version>4.7.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>highcharts</artifactId>
<version>6.1.1</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.spring.sample.dashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DashboardApplication {

public static void main(String[] args) {
SpringApplication.run(DashboardApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.spring.sample.dashboard;

import java.time.LocalDate;

import io.spring.sample.dashboard.stats.StatsService;

import org.springframework.format.annotation.DateTimeFormat;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class DashboardController {

private final StatsService statsService;

public DashboardController(StatsService statsService) {
this.statsService = statsService;
}

@GetMapping("/")
public String index() {
return redirect(LocalDate.now().minusWeeks(1).toString(), LocalDate.now().toString());
}

@PostMapping("/")
public String redirect(@RequestParam String fromDate, @RequestParam String toDate) {
return String.format("redirect:/stats/%s/%s", fromDate, toDate);
}

@GetMapping("/stats/{fromDate}/{toDate}")
public String showDashboard(@PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromDate,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @PathVariable LocalDate toDate,
Model model) {
model.addAttribute("fromDate", fromDate);
model.addAttribute("toDate", toDate);
model.addAttribute("stats", statsService.fetchStats(fromDate.toString(), toDate.toString()));
return "index";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.spring.sample.dashboard;

import io.spring.sample.dashboard.stats.ReverseLookupClient;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

@Component
class ReverseLookupHealthIndicator extends AbstractHealthIndicator {

private final ReverseLookupClient client;

public ReverseLookupHealthIndicator(ReverseLookupClient client) {
this.client = client;
}

@Override
protected void doHealthCheck(Health.Builder builder) {
client.freeReverseLookup("10.10.10.10");
builder.up();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.spring.sample.dashboard;

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.info.InfoEndpoint;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.requestMatchers(EndpointRequest.to(InfoEndpoint.class, HealthEndpoint.class)).permitAll()
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ADMIN")
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").permitAll()
.and().httpBasic();
}

@Bean
@SuppressWarnings("deprecation")
public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
return new InMemoryUserDetailsManager(
User.withDefaultPasswordEncoder().username("user").password("user").roles("USER").build(),
User.withDefaultPasswordEncoder().username("admin").password("admin").roles("USER", "ADMIN").build()
);
}

}
14 changes: 14 additions & 0 deletions dashboard/src/main/java/io/spring/sample/dashboard/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.spring.sample.dashboard;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.spring.sample.dashboard.stats;

public class Annotation {

private String text;

private String date;

public Annotation(String text, String date) {
this.text = text;
this.date = date;
}

public String getText() {
return text;
}

public String getDate() {
return date;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.spring.sample.dashboard.stats;

public class Client {

private String ip;
private String hostname;

public Client(String ip, String hostname) {
this.ip = ip;
this.hostname = hostname;
}

public String getIp() {
return ip;
}

public String getHostname() {
return hostname;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.spring.sample.dashboard.stats;

public class ProjectCreations {

public ProjectCreations(String date, int count) {
this.date = date;
this.count = count;
}

private String date;

private int count;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.spring.sample.dashboard.stats;

import io.spring.sample.dashboard.stats.support.ReverseLookupDescriptor;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class ReverseLookupClient {

private final RestTemplate client;

public ReverseLookupClient(RestTemplateBuilder builder) {
this.client = builder.build();
}

public ReverseLookupDescriptor freeReverseLookup(String ip) {
return this.client.getForObject("http://localhost:8081/reverse-lookup/free/{ip}", ReverseLookupDescriptor.class, ip);
}

public ReverseLookupDescriptor payingReverseLookup(String ip) {
return this.client.getForObject("http://localhost:8081/reverse-lookup/costly/{ip}", ReverseLookupDescriptor.class, ip);
}
}
Loading

0 comments on commit 5e33a1c

Please sign in to comment.