Commit 7cc24f50 authored by Phil Winder's avatar Phil Winder
Browse files

Initial commit

parent b831cdde
# Logs
logs
*.log
npm-debug.log*
# Dependency directories
node_modules
jspm_packages
# Optional npm cache directory
.npm
### Go template
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof
### Java template
/target/
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### OSX template
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea
## File-based project format:
*.iws
*.iml
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Created by .ignore support plugin (hsz.mobi)
# Maven builds
*/target
*/*/target
# AWS ECS install scripts generates an SSH key file
weave-ecs-demo-key.pem
# Load test generates pyc files
*.pyc
# Ignore Vagrant cache files
*.vagrant/
language: java
sudo: required
services:
- docker
jdk:
- oraclejdk8
install: true
env:
- GROUP=weaveworksdemos COMMIT=$TRAVIS_COMMIT TAG=$TRAVIS_TAG;
script:
- set -e
- ./scripts/build.sh;
- ./test/test.sh unit.py
- ./test/test.sh component.py
# - ./test/test.sh container.py --tag $TAG
after_success:
- set -e;
- ./test/test.sh coveralls.py
- if [ -z "$DOCKER_PASS" ] ; then
echo "This is a build triggered by an external PR. Skipping docker push.";
exit 0;
fi;
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS;
- ./scripts/push.sh
FROM java:openjdk-8-alpine
WORKDIR /usr/src/app
COPY ./target/*.jar ./app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/urandom","-jar","./app.jar", "--port=80"]
# cart # cart
Carts service for microservices-demo application A microservices-demo service that provides user account information.
This build is built, tested and released by travis.
# Test
`./test/test.sh < python testing file >`. For example: `./test/test.sh unit.py`
# Build
`GROUP=weaveworksdemos COMMIT=test ./scripts/build.sh`
# Push
`GROUP=weaveworksdemos COMMIT=test ./scripts/push.sh`
FROM java:openjdk-8-alpine
WORKDIR /usr/src/app
COPY *.jar ./app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/urandom","-jar","./app.jar", "--port=80"]
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>works.weave.microservices-demo</groupId>
<artifactId>carts</artifactId>
<packaging>jar</packaging>
<name>carts</name>
<description>Carts service for microservices-demo application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>1.50.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.openpojo</groupId>
<artifactId>openpojo</artifactId>
<version>0.8.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>carts</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>**/Unit*.java</include>
</includes>
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/IT*.java</include>
</includes>
<excludes>
<exclude>**/Unit*.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.2.0</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
#!/usr/bin/env bash
set -ev
SCRIPT_DIR=$(dirname "$0")
if [[ -z "$GROUP" ]] ; then
echo "Cannot find GROUP env var"
exit 1
fi
if [[ -z "$COMMIT" ]] ; then
echo "Cannot find COMMIT env var"
exit 1
fi
if [[ "$(uname)" == "Darwin" ]]; then
DOCKER_CMD=docker
else
DOCKER_CMD="sudo docker"
fi
CODE_DIR=$(cd $SCRIPT_DIR/..; pwd)
echo $CODE_DIR
$DOCKER_CMD run --rm -v $HOME/.m2:/root/.m2 -v $CODE_DIR:/usr/src/mymaven -w /usr/src/mymaven maven:3.2-jdk-8 mvn -DskipTests package
cp $CODE_DIR/target/*.jar $CODE_DIR/docker/cart
for m in ./docker/*/; do
REPO=${GROUP}/$(basename $m)
$DOCKER_CMD build -t ${REPO}:${COMMIT} $CODE_DIR/$m;
done;
#!/usr/bin/env bash
set -ev
if [[ -z "$GROUP" ]] ; then
echo "Cannot find GROUP env var"
exit 1
fi
if [[ -z "$COMMIT" ]] ; then
echo "Cannot find COMMIT env var"
exit 1
fi
push() {
DOCKER_PUSH=1;
while [ $DOCKER_PUSH -gt 0 ] ; do
echo "Pushing $1";
docker push $1;
DOCKER_PUSH=$(echo $?);
if [[ "$DOCKER_PUSH" -gt 0 ]] ; then
echo "Docker push failed with exit code $DOCKER_PUSH";
fi;
done;
}
tag_and_push_all() {
if [[ -z "$1" ]] ; then
echo "Please pass the tag"
exit 1
else
TAG=$1
fi
for m in ./docker/*/; do
REPO=${GROUP}/$(basename $m)
if [[ "$COMMIT" != "$TAG" ]]; then
docker tag ${REPO}:${COMMIT} ${REPO}:${TAG}
fi
push "$REPO:$TAG";
done;
}
# Always push commit
tag_and_push_all $COMMIT
# Push snapshot when in master
if [ "$TRAVIS_BRANCH" == "master" ]; then
tag_and_push_all snapshot
fi;
# Push tag and latest when tagged
if [ -n "$TRAVIS_TAG" ]; then
tag_and_push_all ${TRAVIS_TAG}
tag_and_push_all latest
fi;
package works.weave.socks.cart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CartApplication {
public static void main(String[] args) {
SpringApplication.run(CartApplication.class, args);
}
}
package works.weave.socks.cart.action;
import java.util.Collection;
import java.util.function.Supplier;
public class FirstResultOrDefault<T> implements Supplier<T> {
private final Collection<T> collection;
private final Supplier<T> nonePresent;
public FirstResultOrDefault(final Collection<T> collection, final Supplier<T> nonePresent) {
this.collection = collection;
this.nonePresent = nonePresent;
}
@Override
public T get() {
return collection.stream().findFirst().orElseGet(nonePresent);
}
}
package works.weave.socks.cart.cart;
import org.slf4j.Logger;
import works.weave.socks.cart.entities.Cart;
import works.weave.socks.cart.entities.Item;
import java.util.List;
import java.util.function.Supplier;
import static org.slf4j.LoggerFactory.getLogger;
public class CartContentsResource implements Contents<Item> {
private final Logger LOG = getLogger(getClass());
private final CartDAO cartRepository;
private final Supplier<Resource<Cart>> parent;
public CartContentsResource(CartDAO cartRepository, Supplier<Resource<Cart>> parent) {
this.cartRepository = cartRepository;
this.parent = parent;
}
@Override
public Supplier<List<Item>> contents() {
return () -> parentCart().contents();
}
@Override
public Runnable add(Supplier<Item> item) {
return () -> {
LOG.debug("Adding for user: " + parent.get().value().get().toString() + ", " + item.get());
cartRepository.save(parentCart().add(item.get()));
};
}
@Override
public Runnable delete(Supplier<Item> item) {
return () -> {
LOG.debug("Deleting for user: " + parent.get().value().get().toString() + ", " + item.get());
cartRepository.save(parentCart().remove(item.get()));
};
}
private Cart parentCart() {
return parent.get().value().get();
}
}
package works.weave.socks.cart.cart;
import works.weave.socks.cart.entities.Cart;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface CartDAO {
void delete(Cart cart);
Cart save(Cart cart);
List<Cart> findByCustomerId(String customerId);
class Fake implements CartDAO {
private Map<String, Cart> cartStore = new HashMap<>();
@Override
public void delete(Cart cart) {
cartStore.remove(cart.customerId);
}
@Override
public Cart save(Cart cart) {
return cartStore.put(cart.customerId, cart);
}
@Override
public List<Cart> findByCustomerId(String customerId) {
Cart cart = cartStore.get(customerId);
if (cart != null) {
return Collections.singletonList(cart);
} else {
return Collections.emptyList();
}
}
}
}
package works.weave.socks.cart.cart;
import works.weave.socks.cart.action.FirstResultOrDefault;
import works.weave.socks.cart.entities.Cart;
import java.util.function.Supplier;
public class CartResource implements Resource<Cart>, HasContents<CartContentsResource> {
private final CartDAO cartRepository;
private final String customerId;
public CartResource(CartDAO cartRepository, String customerId) {
this.cartRepository = cartRepository;
this.customerId = customerId;
}
@Override
public Runnable destroy() {
return () -> cartRepository.delete(value().get());
}
@Override
public Supplier<Cart> create() {
return () -> cartRepository.save(new Cart(customerId));
}
@Override
public Supplier<Cart> value() {
return new FirstResultOrDefault<>(
cartRepository.findByCustomerId(customerId),
() -> {
create().get();
return value().get();
});
}
@Override
public Runnable merge(Cart toMerge) {
return () -> toMerge.contents().forEach(item -> contents().get().add(() -> item).run());
}
@Override
public Supplier<CartContentsResource> contents() {
return () -> new CartContentsResource(cartRepository, () -> this);
}
}
package works.weave.socks.cart.cart;
import works.weave.socks.cart.entities.Item;
import java.util.List;
import java.util.function.Supplier;
public interface Contents<T> {
Supplier<List<T>> contents();
Runnable add(Supplier<Item> item);
Runnable delete(Supplier<Item> item);
}
package works.weave.socks.cart.cart;
import java.util.function.Supplier;
public interface HasContents<T extends Contents> {
Supplier<T> contents();
}
package works.weave.socks.cart.cart;
import works.weave.socks.cart.entities.Cart;
import java.util.function.Supplier;
public interface Resource<T> {
Runnable destroy();
Supplier<T> create();
Supplier<T> value();
Runnable merge(T toMerge);
class CartFake implements Resource<Cart> {
private final String customerId;
private Cart cart = null;
public CartFake(String customerId) {
this.customerId = customerId;
}
@Override
public Runnable destroy() {
return () -> cart = null;
}
@Override
public Supplier<Cart> create() {
return () -> cart = new Cart(customerId);
}
@Override
public Supplier<Cart> value() {
if (cart == null) {
create().get();
}
return () -> cart;
}