Skip to content

Commit 3ab9f28

Browse files
Create new ads service in Java (#27)
* split py and java for ads * add gradle app * remove build files * cleanup * comments * attempt to serve static resource * attempt to serve static image * add mvp java ad service * add dd-trace for java * update so frontend can request ad * change service name * update workflows
1 parent 9e3621f commit 3ab9f28

File tree

25 files changed

+227
-5
lines changed

25 files changed

+227
-5
lines changed

.github/workflows/ads-java.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Ads
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
paths:
7+
- services/ads/java/**
8+
workflow_dispatch:
9+
branches: [ main ]
10+
11+
defaults:
12+
run:
13+
working-directory: ads
14+
15+
jobs:
16+
17+
build:
18+
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v2
24+
25+
- name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v1
27+
28+
- name: Configure AWS credentials
29+
uses: aws-actions/configure-aws-credentials@v1
30+
with:
31+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
32+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33+
aws-region: us-east-1
34+
35+
- name: Login to ECR
36+
id: login-ecr
37+
uses: docker/login-action@v1
38+
with:
39+
registry: public.ecr.aws
40+
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
41+
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
42+
43+
- name: Build and push
44+
uses: docker/build-push-action@v2
45+
with:
46+
context: ./services/ads/java
47+
platforms: linux/amd64
48+
push: true
49+
tags: ${{ secrets.PUBLIC_ECR_REGISTRY }}/storedog/ads-java:latest
50+

.github/workflows/ads.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches: [ main ]
66
paths:
7-
- services/ads/**
7+
- services/ads/python/**
88
workflow_dispatch:
99
branches: [ main ]
1010

@@ -43,7 +43,7 @@ jobs:
4343
- name: Build and push
4444
uses: docker/build-push-action@v2
4545
with:
46-
context: ./services/ads
46+
context: ./services/ads/python
4747
platforms: linux/amd64
4848
push: true
4949
tags: ${{ secrets.PUBLIC_ECR_REGISTRY }}/storedog/ads:latest

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
${{ secrets.PUBLIC_ECR_REGISTRY }}/storedog/backend
4040
${{ secrets.PUBLIC_ECR_REGISTRY }}/storedog/discounts
4141
${{ secrets.PUBLIC_ECR_REGISTRY }}/storedog/ads
42+
${{ secrets.PUBLIC_ECR_REGISTRY }}/storedog/ads-java
4243
${{ secrets.PUBLIC_ECR_REGISTRY }}/storedog/attackbox
4344
${{ secrets.PUBLIC_ECR_REGISTRY }}/storedog/auth
4445
)

docker-compose.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ services:
7878
- DD_PROFILING_ENABLED=true
7979
- DD_APPSEC_ENABLED=true
8080
build:
81-
context: ./services/ads
81+
context: ./services/ads/python
8282
command: flask run --port=${ADS_PORT} --host=0.0.0.0 # If using any other port besides the default 9292, overriding the CMD is required
8383
volumes:
84-
- "./services/ads:/app"
84+
- ./services/ads/python:/app
8585
ports:
8686
- "${ADS_PORT}:${ADS_PORT}"
8787
networks:
@@ -104,7 +104,7 @@ services:
104104
build:
105105
context: ./services/discounts
106106
volumes:
107-
- "./services/discounts:/app"
107+
- ./services/discounts:/app
108108
ports:
109109
- "${DISCOUNTS_PORT}:${DISCOUNTS_PORT}"
110110
- "22:22"
@@ -153,6 +153,19 @@ services:
153153
- /sys/fs/cgroup/:/host/sys/fs/cgroup:ro
154154
networks:
155155
- storedog-net
156+
ads-java:
157+
build:
158+
context: ./services/ads/java
159+
environment:
160+
- DD_SERVICE=ads-java
161+
- DD_AGENT_HOST=dd-agent
162+
- DD_LOGS_INJECTION=true
163+
- DD_TRACE_ANALYTICS_ENABLED=true
164+
- DD_PROFILING_ENABLED=true
165+
ports:
166+
- "3030:8080"
167+
networks:
168+
- storedog-net
156169
attackbox:
157170
build:
158171
context: ./services/attackbox

services/ads/java/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# compile via gradle
2+
FROM gradle:jdk11 AS TEMP_BUILD_IMAGE
3+
ENV APP_HOME=/usr/app/
4+
WORKDIR $APP_HOME
5+
COPY build.gradle settings.gradle $APP_HOME
6+
7+
COPY gradle $APP_HOME/gradle
8+
COPY --chown=gradle:gradle . /home/gradle/src
9+
USER root
10+
RUN chown -R gradle /home/gradle/src
11+
12+
COPY . .
13+
RUN gradle clean build
14+
15+
# app container
16+
FROM alpine:latest
17+
RUN apk --update add openjdk11-jre
18+
ENV ARTIFACT_NAME=ads-java-0.0.1-SNAPSHOT.jar
19+
ENV APP_HOME=/usr/app/
20+
21+
WORKDIR $APP_HOME
22+
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/$ARTIFACT_NAME .
23+
24+
RUN wget -O dd-java-agent.jar 'https://dtdg.co/latest-java-tracer'
25+
26+
ENTRYPOINT exec java -javaagent:/usr/app/dd-java-agent.jar -Ddd.logs.injection=true -Ddd.service=ads-java -Ddd.env=dev -jar ${ARTIFACT_NAME}

services/ads/java/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
id 'org.springframework.boot' version '2.7.5'
3+
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
4+
id 'java'
5+
}
6+
7+
group = 'com.example'
8+
version = '0.0.1-SNAPSHOT'
9+
sourceCompatibility = '11'
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
implementation (
17+
'commons-io:commons-io:2.4',
18+
'org.springframework.boot:spring-boot-starter-web'
19+
)
20+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
21+
}
22+
23+
tasks.named('test') {
24+
useJUnitPlatform()
25+
}
59.3 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

services/ads/java/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'ads-java'
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package adsjava;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.bind.annotation.CrossOrigin;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.util.StreamUtils;
10+
import java.io.*;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
import org.apache.commons.io.IOUtils;
13+
import java.util.concurrent.ThreadLocalRandom;
14+
import java.util.HashMap;
15+
import org.springframework.web.bind.annotation.RequestParam;
16+
17+
@SpringBootApplication
18+
@RestController
19+
public class AdsJavaApplication {
20+
21+
@CrossOrigin(origins = "http://localhost:3000")
22+
@RequestMapping(
23+
value = "/banners/{id}",
24+
produces = MediaType.IMAGE_JPEG_VALUE
25+
)
26+
public @ResponseBody byte[] getImageWithMediaType() throws IOException {
27+
int randomNum = ThreadLocalRandom.current().nextInt(1, 3 + 1);
28+
String imagePath = "/static/ads/ad" + randomNum + ".jpg";
29+
InputStream in = getClass()
30+
.getResourceAsStream(imagePath);
31+
return IOUtils.toByteArray(in);
32+
}
33+
34+
@RequestMapping("/")
35+
public String home() {
36+
return "Hello from Advertisements (Java)";
37+
}
38+
39+
@CrossOrigin(origins = "http://localhost:3000")
40+
@RequestMapping(
41+
value = "/ads",
42+
produces = MediaType.APPLICATION_JSON_VALUE
43+
)
44+
public HashMap[] ads() {
45+
HashMap<String, String> map1 = new HashMap<>();
46+
map1.put("id", "1");
47+
map1.put("path", "ad1.jpg");
48+
49+
HashMap<String, String> map2 = new HashMap<>();
50+
map2.put("id", "2");
51+
map2.put("path", "ad2.jpg");
52+
53+
HashMap<String, String> map3 = new HashMap<>();
54+
map3.put("id", "3");
55+
map3.put("path", "ad3.jpg");
56+
57+
HashMap[] myArr = { map1, map2, map3 };
58+
return myArr;
59+
}
60+
61+
public static void main(String[] args) {
62+
SpringApplication.run(AdsJavaApplication.class, args);
63+
}
64+
65+
}

0 commit comments

Comments
 (0)