Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Deploy to Heroku #33

Merged
merged 12 commits into from
Jun 25, 2018
Merged
31 changes: 27 additions & 4 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- Icons http://aalmiray.github.io/ikonli/cheat-sheet-fontawesome5.html -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- Icons http://aalmiray.github.io/ikonli/cheat-sheet-fontawesome5.html -->
<dependency>
<!-- Icons -->
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>2.1.1</version>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-fontawesome5-pack</artifactId>
<version>2.1.1</version>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
Expand All @@ -58,6 +57,30 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.github.coldab.client.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
Expand Down
15 changes: 9 additions & 6 deletions client/src/main/java/com/github/coldab/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
public class Main {

private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
private static String webSocketEndpoint = "ws://localhost:8080/ws/";
private static String restEndpoint = "http://localhost:8080";
private static String webSocketEndpoint;
private static String restEndpoint;

public static void main(String[] args) {
// Set log level
Expand All @@ -25,17 +25,20 @@ public static void main(String[] args) {
// Parse args
Options options = new Options();
options.addOption("debugwebsockets", "Set log level");
options.addOption("k", "insecure", false, "Use TLS (HTTP and WSS)");
options.addOption("h", "host", true, "Hosts ip or domain name");
try {
CommandLine commandLine = new DefaultParser().parse(options, args);
if (commandLine.hasOption("debugwebsockets")) {
MessageEncoder.enableDebugging();
}
String host = commandLine.getOptionValue("h");
if (host != null) {
webSocketEndpoint = "ws://" + host + ":8080/ws/";
restEndpoint = "http://" + host + ":8080";
String host = commandLine.getOptionValue("host");
if (host == null) {
host = "coldab.herokuapp.com";
}
boolean secure = !commandLine.hasOption("insecure");
webSocketEndpoint = (secure ? "wss" : "ws") + "://" + host + "/ws/";
restEndpoint = (secure ? "https" : "http") + "://" + host;
} catch (Exception exception) {
LOGGER.severe("Error while parsing CLI arguments:" + exception.getMessage());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ private class ProjectRow extends ListCell<Project> {
private final HBox box;
private final Label label;
private final Button open;
private final Button remove;

ProjectRow(ListView<Project> listView) {
label = new Label();
Pane pane = new Pane();
HBox.setHgrow(pane, Priority.ALWAYS);
open = new Button("Open");
Button remove = new Button(null, new FontIcon(FontAwesomeRegular.TRASH_ALT));
remove = new Button(null, new FontIcon(FontAwesomeRegular.TRASH_ALT));
box = new HBox(label, pane, open, remove);
}

Expand All @@ -101,6 +102,7 @@ protected void updateItem(Project project, boolean empty) {
setText(null);
label.setText(project.getName());
open.setOnAction(event -> openProject(project));
remove.setOnAction(event -> removeProject(project));
setGraphic(box);
}
}
Expand All @@ -112,5 +114,10 @@ private void openProject(Project project) {
resultCallback.accept(project);
}

private void removeProject(Project project) {
if (accountServer.removeProject(project)) {
projectsListView.getItems().remove(project);
}
}
}
}
11 changes: 11 additions & 0 deletions client/src/main/java/com/github/coldab/client/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.logging.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;

Expand Down Expand Up @@ -87,4 +88,14 @@ public void logout(String sessionId) {
restTemplate
.postForEntity("/account/logout", sessionId, Boolean.TYPE);
}

@Override
public boolean removeProject(Project project) {
try {
restTemplate.delete("/account/project/{id}", project.getId());
return true;
} catch (HttpServerErrorException e) {
return false;
}
}
}
20 changes: 7 additions & 13 deletions client/src/main/resources/fxml/projectChooser.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.131" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.github.coldab.client.gui.ProjectChooserController">
<ListView fx:id="projectsListView" prefHeight="364.0" prefWidth="248.0" GridPane.rowIndex="1" />
<Button alignment="BASELINE_RIGHT" onAction="#addNewProject">Create New Project</Button>
<Button alignment="CENTER_RIGHT" onAction="#refreshProjects">Refresh</Button>
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
<Button onAction="#addNewProject" GridPane.columnIndex="0">
Create New Project
</Button>
<Button onAction="#refreshProjects" GridPane.columnIndex="1">
Refresh
</Button>
<ListView fx:id="projectsListView" GridPane.rowIndex="1" GridPane.columnSpan="2"/>
</GridPane>
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>2.0.2.RELEASE</spring.version>
</properties>

<build>
Expand Down Expand Up @@ -77,7 +78,7 @@
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.2.RELEASE</version>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
28 changes: 25 additions & 3 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@
<version>8.0.9-rc</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.16.1</version>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>

Expand All @@ -67,6 +66,29 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.heroku.sdk</groupId>
<artifactId>heroku-maven-plugin</artifactId>
<version>2.0.4</version>
<configuration>
<appName>coldab</appName>
<processTypes>
<web>java -Dserver.port=$PORT -jar target/server-0.1.jar --spring.profiles.active=heroku</web>
</processTypes>
<includeTarget>false</includeTarget>
<includes>
<include>target/server-0.1.jar</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
25 changes: 0 additions & 25 deletions server/src/main/java/com/github/coldab/server/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package com.github.coldab.server;

import com.github.coldab.server.dal.AccountStore;
import com.github.coldab.server.dal.FileStore;
import com.github.coldab.server.dal.ProjectStore;
import com.github.coldab.shared.account.Account;
import com.github.coldab.shared.edit.Addition;
import com.github.coldab.shared.project.Project;
import com.github.coldab.shared.project.TextFile;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EntityScan("com.github.coldab")
Expand All @@ -20,20 +11,4 @@ public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}

@Bean
public CommandLineRunner demo(ProjectStore projectStore, AccountStore accountStore,
FileStore fileStore) {
return args -> {
Project project = new Project("TestProject");
TextFile textFile = new TextFile(0, "index.html");
Account piet = new Account("Piet Hein", "[email protected]", "1234");
piet = accountStore.save(piet);
textFile.addEdit(new Addition(0, piet, null, "Hello World from database"));
fileStore.save(textFile);
project.getAdmins().add(piet);
project.getFiles().add(textFile);
projectStore.save(project);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import com.github.coldab.shared.account.Account;
import com.github.coldab.shared.project.Project;
import java.util.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
Expand All @@ -28,6 +31,7 @@ public ProjectController(ProjectStore projectStore,

/**
* Create a new project.
*
* @param input contains the project name, and current user as admin
* @return a empty project with the current user as admin
*/
Expand All @@ -52,4 +56,18 @@ Iterable<Project> getProjects(@RequestHeader("Session") String sessionId) {
.map(projectStore::findProjectsByUser)
.orElse(null);
}

/**
* Remove project.
*/
@DeleteMapping("/{id}")
ResponseEntity removeProject(
@PathVariable int id,
@RequestHeader("Session") String sessionId) {
Account account = sessionManager.validateSessionId(sessionId);
Optional<Project> project = projectStore.findById(id)
.filter(account::isMemberOf);
project.ifPresent(projectStore::delete);
return new ResponseEntity(project.isPresent() ? HttpStatus.OK : HttpStatus.UNAUTHORIZED);
}
}
3 changes: 3 additions & 0 deletions server/src/main/resources/application-heroku.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
5 changes: 0 additions & 5 deletions server/src/main/resources/application-sqlite.properties

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.coldab.server.services;

import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class Project {
@Expose
private LocalDateTime creationDate = TimeProvider.getInstance().now();

@OneToMany(fetch = FetchType.EAGER)
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JsonIgnore
private Set<File> files = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public interface AccountServer {
* logout from the application.
*/
void logout(String sessionId);

boolean removeProject(Project project);
}