Skip to content

Commit

Permalink
adding starwizard-services module
Browse files Browse the repository at this point in the history
  • Loading branch information
eonwhite committed Sep 26, 2018
1 parent e1d1771 commit 25256b8
Show file tree
Hide file tree
Showing 20 changed files with 1,187 additions and 158 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<groupId>io.stardog.starwizard</groupId>
<artifactId>starwizard</artifactId>
<packaging>pom</packaging>
<version>0.1.4-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<modules>
<module>starwizard-mongodb</module>
<module>starwizard-core</module>
<module>starwizard-services</module>
</modules>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down Expand Up @@ -40,7 +41,7 @@
</scm>

<properties>
<dropwizard.version>1.1.0</dropwizard.version>
<dropwizard.version>1.3.5</dropwizard.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Expand All @@ -61,7 +62,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<version>0.8.1</version>
<executions>
<execution>
<goals>
Expand Down
11 changes: 8 additions & 3 deletions starwizard-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>starwizard</artifactId>
<groupId>io.stardog.starwizard</groupId>
<version>0.1.4-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -27,10 +27,15 @@
<artifactId>dropwizard-swagger</artifactId>
<version>1.0.6-1</version>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.3</version>
<version>1.6.2</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -42,7 +47,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.21</version>
<version>2.22.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.stardog.starwizard.util;

import javax.servlet.http.HttpServletRequest;

public class RequestUtil {
/**
* Return a remote IP address, optionally picking up the X-Forwarded-For header used by AWS ELB
* @param request request
* @return the ip address, from either X-Forwarded-For or the direct connection
*/
public static String getRemoteIp(HttpServletRequest request) {
if (request == null) {
return "127.0.0.1";
}

String ip = request.getHeader("X-Forwarded-For") != null ? request.getHeader("X-Forwarded-For") : request.getRemoteAddr();
if (ip.contains(",")) {
ip = ip.substring(0, ip.indexOf(","));
}
return ip;
}
}
153 changes: 79 additions & 74 deletions starwizard-core/starwizard-core.iml

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions starwizard-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>starwizard</artifactId>
<groupId>io.stardog.starwizard</groupId>
<version>0.1.4-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
<version>3.6.4</version>
</dependency>
<dependency>
<groupId>com.smoketurner</groupId>
Expand All @@ -36,13 +36,13 @@
<dependency>
<groupId>com.github.fakemongo</groupId>
<artifactId>fongo</artifactId>
<version>2.0.12</version>
<version>2.2.0-RC2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.21</version>
<version>2.22.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
152 changes: 78 additions & 74 deletions starwizard-mongodb/starwizard-mongodb.iml

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions starwizard-services/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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">
<parent>
<artifactId>starwizard</artifactId>
<groupId>io.stardog.starwizard</groupId>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>starwizard-services</artifactId>

<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ssm</artifactId>
<version>1.11.415</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.6.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.stardog.starwizard.services.common;

import io.dropwizard.lifecycle.Managed;

import javax.inject.Singleton;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
* A shared service for executing lightweight asynchronous processes and managing their thread pool using Dropwizard
* lifecycle.
*/
@Singleton
public class AsyncService implements Managed {
private final ExecutorService executorService = Executors.newFixedThreadPool(10);

@Override
public void start() throws Exception {

}

@Override
public void stop() throws Exception {
executorService.shutdown();
executorService.awaitTermination(60, TimeUnit.SECONDS);
executorService.shutdownNow();
}

public ExecutorService getExecutorService() {
return executorService;
}

public Future<?> submit(Runnable runnable) {
return executorService.submit(runnable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.stardog.starwizard.services.common;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

/**
* Injectable service for getting the current time and date - can override the concept of "now" for test purposes.
*/
@Singleton
public class TimeService {
private final ZoneId timezone;
private Instant now = null;

@Inject
public TimeService(@Named("timezone") ZoneId timezone) {
this.timezone = timezone;
}

public void setToday(LocalDate today) {
setNow(today.atStartOfDay(timezone).toInstant());
}

public void setNow(Instant now) {
this.now = now;
}

public Instant getNow() {
return now != null ? now : Instant.now();
}

public LocalDate getToday() {
return toDate(getNow());
}

public LocalDate toDate(Instant at) {
return at.atZone(timezone).toLocalDate();
}

public Instant toInstant(LocalDate date) { return date.atStartOfDay(timezone).toInstant(); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.stardog.starwizard.services.geocode;

import io.stardog.starwizard.services.geocode.data.Geopoint;

import java.util.Optional;

public interface GeocodeService {
Optional<Geopoint> geocode(String string);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.stardog.starwizard.services.geocode.data;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* Geopoint is a data object that represents a particular point in space. It Jackson-serializes to/from the GeoJSON format.
*/
@AutoValue
@JsonPropertyOrder({"type", "coordinates"})
public abstract class Geopoint {
public String getType() {
return "Point";
}

public abstract List<Double> getCoordinates();

@JsonIgnore
public double getLng() {
return getCoordinates().get(0);
}

@JsonIgnore
public double getLat() {
return getCoordinates().get(1);
}

/**
* Return the distance, in meters, from this point to another Geopoint.
* From: http://stackoverflow.com/questions/3694380/calculating-distance-between-two-points-using-latitude-longitude-what-am-i-doi
* @param other another geopoint
* @return distance in meters
*/
@JsonIgnore
public double getDistanceTo(Geopoint other) {
double lat1 = getLat();
double lon1 = getLng();
double lat2 = other.getLat();
double lon2 = other.getLng();
double el1 = 0;
double el2 = 0;

final int R = 6371; // Radius of the earth

Double latDistance = Math.toRadians(lat2 - lat1);
Double lonDistance = Math.toRadians(lon2 - lon1);
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c * 1000; // convert to meters

double height = el1 - el2;

distance = Math.pow(distance, 2) + Math.pow(height, 2);

return Math.sqrt(distance);
}

public static Geopoint of(double lng, double lat) {
if (Math.abs(lng) > 180) {
throw new IllegalArgumentException("invalid longitude: " + lng);
}
if (Math.abs(lat) > 90) {
throw new IllegalArgumentException("invalid latitude: " + lat);
}
return new AutoValue_Geopoint(ImmutableList.of(lng, lat));
}

@JsonCreator
public static Geopoint of(@JsonProperty("type") String type, @JsonProperty("coordinates") List<Double> coordinates) {
if (!"Point".equals(type)) {
throw new IllegalArgumentException("Must be type 'Point'");
}
if (coordinates.size() != 2) {
throw new IllegalArgumentException("Must have two coordinates");
}
return Geopoint.of(coordinates.get(0), coordinates.get(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.stardog.starwizard.services.geocode.exceptions;

public class GeocodeException extends RuntimeException {
public GeocodeException(Throwable cause) {
super(cause);
}
}
Loading

0 comments on commit 25256b8

Please sign in to comment.