Skip to content

Commit

Permalink
Deployment works, nicer ui
Browse files Browse the repository at this point in the history
  • Loading branch information
simonharrer committed Oct 23, 2021
1 parent 41ae248 commit eb712e0
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 70 deletions.
1 change: 1 addition & 0 deletions build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./mvnw spring-boot:build-image && docker push simonharrer/mob-timer && ssh [email protected] 'bash -s' < deploy
3 changes: 3 additions & 0 deletions deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker stop mobtimer
docker rm mobtimer
docker run -d -e VIRTUAL_HOST=timer.mob.sh -e LETSENCRYPT_HOST=timer.mob.sh -e [email protected] -e PORT=80 --expose 80 --network=proxy --pull always --name mobtimer simonharrer/mob-timer:latest
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<name>timer</name>
<description>timer</description>
<properties>
<java.version>17</java.version>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -58,8 +58,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<configuration>
<source>17</source>
<target>17</target>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/sh/mob/timer/web/IndexController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sh.mob.timer.web;

import java.util.Objects;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -28,8 +29,40 @@ public String index(Model model) {

@PostMapping
public String post(@ModelAttribute Form form) {
return "redirect:/%s".formatted(form.room());
return String.format("redirect:/%s", form.room());
}

public record Form (String room) {}
public static final class Form {

private final String room;

public Form(String room) {
this.room = room;
}

public String room() {
return room;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (Form) obj;
return Objects.equals(this.room, that.room);
}

@Override
public int hashCode() {
return Objects.hash(room);
}

@Override
public String toString() {
return "Form[" +
"room=" + room + ']';
}
}
}
112 changes: 109 additions & 3 deletions src/main/java/sh/mob/timer/web/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

final class Room {

Expand Down Expand Up @@ -40,10 +42,66 @@ public TimeLeft timeLeft() {
return new TimeLeft(result, timer, lastTimerRequestedTimestamp, lastTimerRequest.user());
}

public record TimeLeft(Duration duration, Long timer, Instant requested, String name) {}
public static final class TimeLeft {

private final Duration duration;
private final Long timer;
private final Instant requested;
private final String name;

public TimeLeft(Duration duration, Long timer, Instant requested, String name) {
this.duration = duration;
this.timer = timer;
this.requested = requested;
this.name = name;
}

public Duration duration() {
return duration;
}

public Long timer() {
return timer;
}

public Instant requested() {
return requested;
}

public String name() {
return name;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (TimeLeft) obj;
return Objects.equals(this.duration, that.duration) &&
Objects.equals(this.timer, that.timer) &&
Objects.equals(this.requested, that.requested) &&
Objects.equals(this.name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(duration, timer, requested, name);
}

@Override
public String toString() {
return "TimeLeft[" +
"duration=" + duration + ", " +
"timer=" + timer + ", " +
"requested=" + requested + ", " +
"name=" + name + ']';
}
}

public List<String> team() {
return timerRequests().stream().map(TimerRequest::user).distinct().sorted().toList();
return timerRequests().stream().map(TimerRequest::user).distinct().sorted().collect(Collectors.toList());
}

public String name() {
Expand All @@ -54,5 +112,53 @@ public List<TimerRequest> timerRequests() {
return Collections.unmodifiableList(timerRequests);
}

record TimerRequest(Long timer, Instant requested, String user) {}
static final class TimerRequest {

private final Long timer;
private final Instant requested;
private final String user;

TimerRequest(Long timer, Instant requested, String user) {
this.timer = timer;
this.requested = requested;
this.user = user;
}

public Long timer() {
return timer;
}

public Instant requested() {
return requested;
}

public String user() {
return user;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (TimerRequest) obj;
return Objects.equals(this.timer, that.timer) &&
Objects.equals(this.requested, that.requested) &&
Objects.equals(this.user, that.user);
}

@Override
public int hashCode() {
return Objects.hash(timer, requested, user);
}

@Override
public String toString() {
return "TimerRequest[" +
"timer=" + timer + ", " +
"requested=" + requested + ", " +
"user=" + user + ']';
}
}
}
49 changes: 41 additions & 8 deletions src/main/java/sh/mob/timer/web/RoomApiController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sh.mob.timer.web;

import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -67,13 +68,10 @@ private static ServerSentEvent<String> timerUpdate(Long sequence, TimeLeft timeL
String data =
timeLeft.timer() == null
? "00:00"
: "%d:%d (%d min timer started by %s at %s)"
.formatted(
timeLeft.duration().toMinutesPart(),
timeLeft.duration().toSecondsPart(),
timeLeft.timer(),
timeLeft.name(),
timeLeft.requested().toString());
: String.format(
"%02d:%02d",
timeLeft.duration().toMinutesPart(),
timeLeft.duration().toSecondsPart());
return ServerSentEvent.<String>builder()
.id(String.valueOf(sequence))
.event("TIMER_UPDATE")
Expand All @@ -97,5 +95,40 @@ public void publishEvent(@PathVariable String roomId, @RequestBody TimerRequest
System.out.println("timerRequest = " + timerRequest);
}

record TimerRequest(Long timer, String user) {}
static final class TimerRequest {

private final Long timer;
private final String user;

TimerRequest(Long timer, String user) {
this.timer = timer;
this.user = user;
}

public Long timer() {
return timer;
}

public String user() {
return user;
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (TimerRequest) obj;
return Objects.equals(this.timer, that.timer) && Objects.equals(this.user, that.user);
}

@Override
public int hashCode() {
return Objects.hash(timer, user);
}

@Override
public String toString() {
return "TimerRequest[" + "timer=" + timer + ", " + "user=" + user + ']';
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.main.banner-mode=off
server.port=${PORT:8080}
41 changes: 31 additions & 10 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Mob Timer</title>
</head>
<body>
<h1>Mob Timer</h1>

<ul>
<li>Rooms: [[${numberOfRooms}]]</li>
<li>Users: [[${numberOfUsers}]]</li>
<li>Connections: [[${numberOfConnections}]]</li>
<div class="col-lg-8 mx-auto p-3 py-md-5">
<header class="d-flex align-items-center pb-3 mb-5 border-bottom">
<a href="/" class="d-flex align-items-center text-dark text-decoration-none">
<span class="fs-4">Mob Timer</span>
</a>
</header>

<main>
<h1>Mob Timer</h1>

<ul class="list-unstyled">
<li>Rooms <span class="badge rounded-pill bg-info text-dark">[[${numberOfRooms}]]</span></li>
<li>Users <span class="badge rounded-pill bg-info text-dark">[[${numberOfUsers}]]</span></li>
<li>Connections <span class="badge rounded-pill bg-info text-dark">[[${numberOfConnections}]]</span></li>
</ul>

<form method="post">
<label>Room <input type="text" id="room" name="room" placeholder="room"/></label>
<button type="submit">Join</button>
</form>
<form method="post">
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon3">https://timer.mob.sh/</span>
<input class="form-control" type="text" id="room" name="room" placeholder="room" required/>
<button type="submit" class="btn btn-primary">Join</button>
</div>
</form>
</main>
<footer class="pt-5 my-5 text-muted border-top">
Created by <a href="https://twitter.com/simonharrer">Dr. Simon Harrer</a> &amp; <a href="https://twitter.com/jochen_christ">Jochen Christ</a> &middot; &copy; 2021
</footer>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>
</html>
Loading

0 comments on commit eb712e0

Please sign in to comment.