Skip to content

Commit

Permalink
Merge pull request #20 from 12urenloop/web-application
Browse files Browse the repository at this point in the history
Web application
  • Loading branch information
mcbloch authored Nov 6, 2019
2 parents d5f67d9 + 29dfb05 commit 4a1c44d
Show file tree
Hide file tree
Showing 34 changed files with 635 additions and 526 deletions.
6 changes: 6 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 36 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ version '1.0-SNAPSHOT'

sourceCompatibility = 11

// Set our project variables
project.ext {
dropwizardVersion = '1.3.9'
}

repositories {
mavenCentral()
jcenter()
}
application {
mainClassName = 'telraam.App'
Expand All @@ -25,13 +31,15 @@ task runDev {

finalizedBy {
run.environment("CONFIG_KEY", "DEVELOPMENT")
run.args('server', "$rootProject.projectDir/src/main/resources/telraam/devConfig.yml")
run
}
}
task runProd {

finalizedBy {
run.environment("CONFIG_KEY", "PRODUCTION")
run.args('server', "$rootProject.projectDir/src/main/resources/telraam/prodConfig.yml")
run
}
}
Expand All @@ -42,10 +50,29 @@ idea {
}

dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.5.2')
// Web framework stuff
implementation(
'io.dropwizard:dropwizard-core:' + dropwizardVersion,
'io.dropwizard:dropwizard-hibernate:' + dropwizardVersion,
'io.dropwizard:dropwizard-auth:' + dropwizardVersion,
'io.dropwizard:dropwizard-jdbi3:' + dropwizardVersion,

)
// Database
implementation('com.h2database:h2:1.4.199')
implementation('org.postgresql:postgresql:42.2.8')

// Testing
testImplementation('org.junit.jupiter:junit-jupiter:5.5.2')
testImplementation('org.flywaydb:flyway-core:6.0.8')
testImplementation("org.mockito:mockito-core:1.+")
testImplementation("io.dropwizard:dropwizard-testing:" + dropwizardVersion)

// JAX-B dependencies for JDK 9+ -> https://stackoverflow.com/a/43574427
implementation "javax.xml.bind:jaxb-api:2.2.11"
implementation "com.sun.xml.bind:jaxb-core:2.2.11"
implementation "com.sun.xml.bind:jaxb-impl:2.2.11"
implementation "javax.activation:activation:1.1.1"
}

test {
Expand Down Expand Up @@ -86,24 +113,28 @@ jacocoTestCoverageVerification {
}
}
def prodProps = new Properties()
file("src/main/resources/telraam/prodConfig.properties").withInputStream {
file("$rootProject.projectDir/src/main/resources/telraam/prodConfig.properties").withInputStream {
prodProps.load(it)
}
task migrateProductionDatabase(type: FlywayMigrateTask) {
url = prodProps.getProperty("DB_URL")
}

def devProps = new Properties()
file("src/main/resources/telraam/devConfig.properties").withInputStream {
file("$rootProject.projectDir/src/main/resources/telraam/devConfig.properties").withInputStream {
devProps.load(it)
}
task migrateDevelopmentDatabase(type: FlywayMigrateTask) {
url = devProps.getProperty("DB_URL")
user = devProps.getProperty("DB_USER")
password = devProps.getProperty("DB_PASSWORD")
}

def testProps = new Properties()
file("src/main/resources/telraam/testConfig.properties").withInputStream {
file("$rootProject.projectDir/src/main/resources/telraam/testConfig.properties").withInputStream {
testProps.load(it)
}
task migrateTestingDatabase(type: FlywayMigrateTask) {
url = testProps.getProperty("DB_URL")
baselineOnMigrate = true
}
}
68 changes: 57 additions & 11 deletions src/main/java/telraam/App.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,70 @@
package telraam;

import telraam.database.Database;
import io.dropwizard.Application;
import io.dropwizard.jdbi3.JdbiFactory;
import io.dropwizard.jdbi3.bundles.JdbiExceptionsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.jdbi.v3.core.Jdbi;
import telraam.api.BatonResource;
import telraam.api.HelloworldResource;
import telraam.database.daos.BatonDAO;
import telraam.database.models.Baton;
import telraam.database.models.Id;
import telraam.healthchecks.TemplateHealthCheck;

import java.sql.Connection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class App {

public class App extends Application<AppConfiguration> {
private static Logger logger = Logger.getLogger(App.class.getName());

public static void main(String[] args) {
logger.log(Level.INFO, "Main method");
Connection conn = Database.getInstance().getDataAccessContext().getConnection();
public static void main(String[] args) throws Exception {
new App().run(args);
}

@Override
public String getName() {
return "hello-world";
}

@Override
public void initialize(Bootstrap<AppConfiguration> bootstrap) {
// nothing to do yet
bootstrap.addBundle(new JdbiExceptionsBundle());
}

/**
* Temporary test method
*/
public String greeting() {
@Override
public void run(AppConfiguration configuration, Environment environment) throws Exception {
// Add database
final JdbiFactory factory = new JdbiFactory();
final Jdbi database = factory.build(environment, configuration.getDataSourceFactory(), "postgresql");

final BatonDAO dao = database.onDemand(BatonDAO.class);
Id id = dao.insert(new Baton("Heto"));

// TODO By default everything should be logged to stdout (see dropwizard logging docs) but it isn't
List<Baton> batons = dao.listBatons();
if(logger.isLoggable(Level.INFO)) {
logger.info("Baton testing information");
logger.info(batons.stream().map(Baton::getName).collect(Collectors.joining(" : ")));
logger.info(String.valueOf(dao.findBatonById(id.getId())));
}

// Add api resources
final HelloworldResource resource = new HelloworldResource(
configuration.getTemplate(),
configuration.getDefaultName()
);
environment.jersey().register(resource);
environment.jersey().register(new BatonResource(database.onDemand(BatonDAO.class)));

return "test";
// Register healthcheck
// environment.healthChecks().register("database", new DatabaseHealthCheck(database));
environment.healthChecks().register("template", new TemplateHealthCheck(configuration.getTemplate()));
logger.warning("TEST LOG");
}
}
55 changes: 55 additions & 0 deletions src/main/java/telraam/AppConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package telraam;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import io.dropwizard.db.DataSourceFactory;
import telraam.api.responses.Template;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

public class AppConfiguration extends Configuration {
@NotNull
private String template;

@NotNull
private String defaultName = "Stranger";

@JsonProperty
public String getTemplate() {
return template;
}

@JsonProperty
public void setTemplate(String template) {
this.template = template;
}

public Template buildTemplate() {
return new Template(template, defaultName);
}

@JsonProperty
public String getDefaultName() {
return defaultName;
}

@JsonProperty
public void setDefaultName(String name) {
this.defaultName = name;
}

@Valid
@NotNull
private DataSourceFactory database = new DataSourceFactory();

@JsonProperty("database")
public void setDataSourceFactory(DataSourceFactory factory) {
this.database = factory;
}

@JsonProperty("database")
public DataSourceFactory getDataSourceFactory() {
return database;
}
}
101 changes: 0 additions & 101 deletions src/main/java/telraam/Config.java

This file was deleted.

Loading

0 comments on commit 4a1c44d

Please sign in to comment.