Skip to content

Commit

Permalink
add Google App Engine deployment configuration and health check contr…
Browse files Browse the repository at this point in the history
…oller
  • Loading branch information
ElinaZoldnere committed Jul 9, 2024
1 parent 9d6f84f commit bcded6c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 14 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@
.env

# Log
*.log
*.log

# Misc
.gcloudignore
gcloud.txt
app.yaml
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ In this implementation, when a HEIC format attachment is uploaded in a text chan
converts it to a JPEG image. The bot then posts the converted image in the same text channel.

The project is developed with Java Spring Boot using Java Discord API and built with Gradle.
The app is deployed on Google App Engine.

The image conversion service is separated from the app, developed as a Google Cloud Function written
in Python with Flask and using Pillow for image processing. It communicates with the
app through HTTP requests.
app via HTTP requests.

## Project Structure

Expand All @@ -25,9 +27,23 @@ app through HTTP requests.
- `src/`: Contains the Java source code.
- `build.gradle`: The Gradle build file.

## Environment Variables
Sensitive data for the project is specified as environment variables. In the Google Cloud Function,
they are set directly during initial deployment. In the Spring Boot application, they are loaded
from a `.env` file. The `application.properties` file contains the loading configuration and
variable placeholders for reference.
## Deployment
The deployment configuration for Google App Engine is specified in the app.yaml file. Manual instance
scaling is used to ensure continuous operation, as there is no direct traffic to the app (the app
does not have its own endpoints to serve). Google App Engine performs periodic health checks to
assess the app's liveness, so a REST controller is implemented to respond to these health checks.

## Sensitive Data Handling

Sensitive data for the project is specified as environment variables. The handling of these variables
is as follows:

- **Google Cloud Function:**
- Environment variables are set directly during the initial deployment process.

- **Spring Boot Application:**
- Local Development:
- Environment variables are loaded from a `.env` file. The `application.properties` file contains
the configuration to load these variables and placeholders for reference.
- Deployment:
- Environment variables are specified in the `app.yaml` file.
18 changes: 16 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
}

sourceCompatibility = '18'
targetCompatibility = '18'
//sourceCompatibility = '18' // local development
//targetCompatibility = '18' // local development

sourceCompatibility = '17' // deploying on Google App Engine
targetCompatibility = '17' // deploying on Google App Engine

compileJava {
options.encoding = "UTF-8"
Expand All @@ -33,3 +36,14 @@ test {
useJUnitPlatform()
}

jar {
enabled = false
}

bootJar {
enabled = true
archiveBaseName = 'discord-bot'
archiveVersion = '1.0.0'
}


2 changes: 1 addition & 1 deletion src/main/java/com/my/discordbot/BotInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.stereotype.Component;

@Component
public class BotInitializer {
class BotInitializer {

@Autowired
private EventListener eventListener;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/my/discordbot/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.stereotype.Component;

@Component
public class EventListener extends ListenerAdapter {
class EventListener extends ListenerAdapter {

private static final Logger logger = LoggerFactory.getLogger(EventListener.class);

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/my/discordbot/HealthCheckRestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.my.discordbot;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
class HealthCheckRestController {

@GetMapping("/")
public ResponseEntity<String> checkHealth() {
return new ResponseEntity<>("OK", HttpStatus.OK);
}

}
3 changes: 1 addition & 2 deletions src/main/java/com/my/discordbot/ProcessConversionToJpg.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand All @@ -20,7 +19,7 @@
import java.nio.file.Files;

@Component
public class ProcessConversionToJpg {
class ProcessConversionToJpg {

@Value("${CLOUD_FUNCTION_URL}")
private String cloudFunctionUrl;
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# sensitive properties, loaded from .env
spring.config.import=optional:file:.env[.properties]
# configuration for local development only!
# spring.config.import=optional:file:.env[.properties]

# for deployment on Google App Engine environment variables are
# specified in app.yaml

# environment variables used in this application (list for reference only)
# DISCORD_BOT_TOKEN=${DISCORD_BOT_TOKEN}
# OWNER_ID=${OWNER_ID}
# CLOUD_FUNCTION_URL=${CLOUD_FUNCTION_URL}
Expand Down

0 comments on commit bcded6c

Please sign in to comment.