Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Demo Dropwizard Application

A boilerplate dropwizard application that includes:

* dagger2 dependency injection
* JDBI dao's
* flyway database migration

## Getting Started

git clone [email protected]:mainstreethub/dropwizard-demo.git
cd dropwizard-demo
mvn clean package
mysql -uroot -e 'create database dwdemo'
mysql -uroot -e "grant all on dwdemo.* to 'dwdemo'@'localhost' identified by 'foobar'"
java -jar application/target/application*.jar server config.yaml
8 changes: 8 additions & 0 deletions application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jdbi</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-flyway</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.mainstreethub.project;

import com.google.common.base.Throwables;
import com.mainstreethub.project.dao.JDBIModule;

import io.dropwizard.Application;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.db.PooledDataSourceFactory;
import io.dropwizard.flyway.FlywayBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.bundles.version.VersionBundle;
import io.dropwizard.bundles.version.VersionSupplier;
import io.dropwizard.bundles.version.suppliers.MavenVersionSupplier;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.FlywayException;


public class ProjectApplication extends Application<ProjectConfiguration> {
public static void main(String[] args) throws Exception {
Expand All @@ -18,11 +25,24 @@ public static void main(String[] args) throws Exception {

@Override
public void initialize(Bootstrap<ProjectConfiguration> bootstrap) {
initVersion(bootstrap);
initVersion(bootstrap);
initFlyway(bootstrap);
}

@Override
public void run(ProjectConfiguration configuration, Environment environment) throws Exception {
//Flyway will migrate or init the scheme in config.yaml
final Flyway flyway = new Flyway();
DataSourceFactory database = configuration.getDatabase();
flyway.setDataSource(database.getUrl(), database.getUser(), database.getPassword());
try {
flyway.migrate();
} catch (FlywayException e) {
flyway.repair();
//don't start app after failed migration attempt
Throwables.propagate(e);
}

ProjectComponent component = DaggerProjectComponent.builder()
.jDBIModule(new JDBIModule(configuration.getDatabase(), environment))
.projectModule(new ProjectModule())
Expand All @@ -31,6 +51,15 @@ public void run(ProjectConfiguration configuration, Environment environment) thr
environment.jersey().register(component.getUsersResource());
}

private void initFlyway(Bootstrap<ProjectConfiguration> bootstrap) {
bootstrap.addBundle(new FlywayBundle<ProjectConfiguration>() {
@Override
public PooledDataSourceFactory getDataSourceFactory(ProjectConfiguration configuration) {
return configuration.getDatabase();
}
});
}

private void initVersion(Bootstrap<ProjectConfiguration> bootstrap) {
VersionSupplier supplier = new MavenVersionSupplier("com.mainstreethub.project", "application");
bootstrap.addBundle(new VersionBundle(supplier));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
first VARCHAR(255),
last VARCHAR(255),
salt VARCHAR(255),
hash VARCHAR(255),
PRIMARY KEY (id)
);
5 changes: 5 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
database:
driverClass: com.mysql.jdbc.Driver
user: dwdemo
password: foobar
url: jdbc:mysql://localhost:3306/dwdemo
18 changes: 15 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@

<!-- Dependency versions. -->
<dagger.version>2.0</dagger.version>
<dropwizard.version>0.8.1</dropwizard.version>
<dropwizard.version>0.9.2</dropwizard.version>
<flyway.bundle.version>0.8.0-2</flyway.bundle.version>
<guava.version>18.0</guava.version>
<jackson.api.version>2.5.0</jackson.api.version>
<jackson.version>2.5.1</jackson.version>
<jackson.api.version>2.6.3</jackson.api.version>
<jackson.version>2.6.3</jackson.version>
<junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<mysql.version>5.1.36</mysql.version>
<dropwizard.version.bundle.version>0.8.1-2</dropwizard.version.bundle.version>
</properties>

Expand Down Expand Up @@ -102,11 +104,21 @@
<artifactId>dropwizard-testing</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-flyway</artifactId>
<version>${flyway.bundle.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down