Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions java-springboot/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Compiled class files
target/
*.class

# Log files
*.log

# Package files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# Virtual machine crash logs
hs_err_pid*

# IDE files
.idea/
.vscode/
*.iml
*.ipr
*.iws

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Maven
.mvn/
mvnw
mvnw.cmd

# Git
.git/
.gitignore

# Documentation
README.md
*.md

# Suga files
.suga/
suga.yaml
suga_gen/

# Test files
user_names.txt
48 changes: 48 additions & 0 deletions java-springboot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Multi-stage Docker build for Java Spring Boot application

# Build stage
FROM maven:3.8.4-openjdk-8-slim AS build

# Set working directory
WORKDIR /app

# Copy Maven configuration files
COPY pom.xml .

# Download dependencies (this layer will be cached if pom.xml doesn't change)
RUN mvn dependency:go-offline -B

# Copy source code
COPY src ./src

# Build the application
RUN mvn clean package -DskipTests

# Runtime stage
FROM openjdk:8-jre-slim

# Set working directory
WORKDIR /app

# Create a non-root user for security
RUN groupadd -r appuser && useradd -r -g appuser appuser

# Copy the built JAR from build stage
COPY --from=build /app/target/hello-world-api-1.0.0.jar app.jar

# Change ownership to appuser
RUN chown -R appuser:appuser /app
USER appuser

# Expose port 8080
EXPOSE 8080

# Set JVM options for containerized environment
ENV JAVA_OPTS="-Xmx512m -Xms256m"

# Health check - using a simple endpoint since actuator is not included
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/ || exit 1

# Run the application
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
108 changes: 108 additions & 0 deletions java-springboot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Hello World API Server (Java)

This project contains a Hello World API server built with Java and Spring Boot framework.

## Features

- RESTful API endpoints for Hello World functionality
- Integration with Suga to manage infrastructure
- Docker containerization
- Logging configuration with Logback

## Files Structure

- `pom.xml` - Maven build configuration with Spring Boot dependencies
- `src/main/java/com/example/helloworldapi/HelloWorldApiApplication.java` - Main Spring Boot application
- `src/main/java/com/example/helloworldapi/controller/HelloWorldController.java` - REST controller
- `src/main/resources/application.properties` - Application configuration
- `src/main/resources/logback.xml` - Logging configuration
- `Dockerfile` - Multi-stage Docker build configuration
- `.dockerignore` - Docker ignore patterns

## Prerequisites

- Java 8 or later
- Maven 3.6 or later (or use Maven wrapper)
- Internet connection for downloading dependencies

## To run locally

To run the application locally using the Suga development environment:

```bash
cd java-springboot
suga dev
```

This command will start the Spring Boot server on port 4000.

Alternatively, you can run it directly using Maven:

```bash
cd java-springboot
./mvnw spring-boot:run
```

Or using Maven directly:
```bash
cd java-springboot
mvn spring-boot:run
```

## To build

```bash
cd java-springboot
./mvnw clean install
```

## API Endpoints

- `GET /api/hello` - Returns "Hello, World!"
- `GET /api/hello/name?name=YourName` - Returns "Hello, YourName!" and logs the name to S3 bucket
- `GET /api/` - Returns welcome message with available endpoints
- `GET /api/logs` - Returns logged user names from S3 bucket

## Testing the API

Once the server is running (either on port 4000 directly or via the Suga Load Balancer on port 3000), you can test the endpoints using:

### curl

```bash
# Direct access (if not using Suga dev)
curl http://localhost:4000/api/hello
curl "http://localhost:4000/api/hello/name?name=John"
curl http://localhost:4000/api/logs

# Via Suga Load Balancer
curl http://localhost:3000/api/hello
curl "http://localhost:3000/api/hello/name?name=John"
curl http://localhost:3000/api/logs
```

## S3 Bucket Logging Feature

The server includes S3 bucket logging functionality using the Suga client:

- When users access `/api/hello/name?name=YourName` with a custom name (not "World"), the server logs the name to an S3 bucket
- Each entry includes a timestamp and the user's name
- The log file is stored as `user_names.txt` in the configured S3 bucket
- Example log entry: `[2025-10-20 17:52:30] User name: John`
- Access logged entries via the `/api/logs` endpoint

## Technology Stack

- **Java** - Popular, high-performance programming language
- **Spring Boot** - Framework for building production-ready Spring applications
- **Maven** - Build automation tool
- **Logback** - Logging framework
- **Suga Client** - Infrastructure Orchestrator
- **Docker** - Containerization

## Notes

- The server runs on port 4000 by default (configurable via PORT environment variable)
- The loadbalancer locally uses port 3000 and forwards to the server as needed.
- Spring Boot provides extensive features for rapid application development
- User names are only logged when they differ from the default "World" value
54 changes: 54 additions & 0 deletions java-springboot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>hello-world-api</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>Hello World API</name>
<description>A simple Hello World API server built with Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.addsuga</groupId>
<artifactId>suga-client</artifactId>
<version>0.0.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.helloworldapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApiApplication {

public static void main(String[] args) {
SpringApplication.run(HelloWorldApiApplication.class, args);
}
}
Loading