Skip to content

Commit

Permalink
Diagnostics and CORS
Browse files Browse the repository at this point in the history
Added a logger and inserted some diagnostics relating to communication
with the UI.

Also removed the hard-coded CORS, putting it into an environment variable.
  • Loading branch information
Gerhard Salvini committed Nov 27, 2024
1 parent 44216b2 commit 9dd3557
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 11 deletions.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
FROM eclipse-temurin:17-jre
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
ENTRYPOINT ["java", "-jar", "application.jar"]
#ENTRYPOINT ["java", "-jar", "application.jar"]

# Remote debugging with VS
EXPOSE 5005
ENTRYPOINT ["java", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", "-jar", "application.jar"]
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@
<artifactId>stax-api</artifactId>
<version>1.0-2</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.samply.feedbackagent;

import jakarta.annotation.PostConstruct;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.samply.feedbackagent.controller.CorsConfig;

@SpringBootApplication
public class FeedbackAgentApplication {
private static final Logger logger = LogManager.getLogger(FeedbackAgentApplication.class);

@Autowired
private ProxyRequestPoller proxyRequestPoller;

public static void main(String[] args) {
logger.info("Starting Feedback Agent");
SpringApplication.run(FeedbackAgentApplication.class, args);
}

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/samply/feedbackagent/controller/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.samply.feedbackagent.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
private static final Logger logger = LogManager.getLogger(CorsConfig.class);

// Load the CORS origin dynamically, e.g., from environment variables or properties
@Value("${fa.cors.origin}")
String corsOrigin;

@Bean
public CorsFilter corsFilter() {
logger.info("corsFilter: corsOrigin: " + corsOrigin);
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin(corsOrigin); // Set your dynamic origin
config.addAllowedHeader("*"); // Allow all headers
config.addAllowedMethod("*"); // Allow all HTTP methods
config.setAllowCredentials(true); // Allow cookies if necessary

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);

return new CorsFilter(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,64 @@
import com.samply.feedbackagent.repository.SpecimenFeedbackRepository;
import com.samply.feedbackagent.exception.SpecimenFeedbackNotFoundException;
import com.samply.feedbackagent.model.SpecimenFeedback;
import com.samply.feedbackagent.service.Configuration;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import jakarta.validation.Valid;
import java.util.List;

@RestController
public class SpecimenFeedbackController {
private static final Logger logger = LogManager.getLogger(SpecimenFeedbackController.class);

@Autowired
SpecimenFeedbackRepository specimenFeedbackRepository;

@Autowired
Configuration configuration;

// Check endpoint
@CrossOrigin(origins = "http://localhost:9000") // Sollte mit einem Umbegbungsvariabel ersetzt werden
//@CrossOrigin(origins = "http://localhost:9000") // TODO: should be replaced by an environment variable
@GetMapping("/info")
public String info() {
logger.info("info: Info endpoint called");
return "OK";
}

// Get all SpecimenFeedback
@CrossOrigin(origins = "http://localhost:9000")
//@CrossOrigin(origins = "http://localhost:9000")
@GetMapping("/specimen-feedback")
public List<SpecimenFeedback> getAllSpecimenFeedback() {
logger.info("getAllSpecimenFeedback: GET specimen-feedback endpoint called");
return specimenFeedbackRepository.findAll();
}

// Create a new SpecimenFeedback
@CrossOrigin(origins = "http://localhost:9000")
//@CrossOrigin(origins = "http://localhost:9000")
@PostMapping("/specimen-feedback")
public SpecimenFeedback createSpecimenFeedback(@Valid @RequestBody SpecimenFeedback specimenFeedback) {
logger.info("createSpecimenFeedback: POST specimen-feedback endpoint called");
return specimenFeedbackRepository.save(specimenFeedback);
}
// Create multiple SpecimenFeedback
@CrossOrigin(origins = "http://localhost:9000")
//@CrossOrigin(origins = "http://localhost:9000")
@PostMapping("/multiple-specimen-feedback")
public List<SpecimenFeedback> createSpecimenFeedback(@Valid @RequestBody SpecimenFeedbackDto specimenFeedbackDto) {
return specimenFeedbackRepository.saveAll(specimenFeedbackDto.getFeedbackList());
logger.info("createSpecimenFeedback: POST multiple-specimen-feedback endpoint called");
List<SpecimenFeedback> feedbackList = specimenFeedbackDto.getFeedbackList();
logger.info("createSpecimenFeedback: feedbackList length: " + feedbackList.size());
List<SpecimenFeedback> specimenFeedback = specimenFeedbackRepository.saveAll(feedbackList);
logger.info("createSpecimenFeedback: returning");
return specimenFeedback;
}

// Get a Single SpecimenFeedback
@CrossOrigin(origins = "http://localhost:9000")
//@CrossOrigin(origins = "http://localhost:9000")
@GetMapping("/specimen-feedback/{id}")
public SpecimenFeedback getSpecimenFeedbackById(@PathVariable(value = "id") Long specimenFeedbackId) throws SpecimenFeedbackNotFoundException {
logger.info("getSpecimenFeedbackById: GET specimen-feedback/{id} endpoint called");
return specimenFeedbackRepository.findById(specimenFeedbackId)
.orElseThrow(() -> new SpecimenFeedbackNotFoundException(specimenFeedbackId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.samply.feedbackagent.controller.SpecimenFeedbackController;

public class SpecimenFeedbackDto {
private static final Logger logger = LogManager.getLogger(SpecimenFeedbackDto.class);

private List<SpecimenFeedback> feedbackList;

public SpecimenFeedbackDto() {
Expand All @@ -14,6 +21,7 @@ public SpecimenFeedbackDto(List<SpecimenFeedback> feedbackList) {
}

public List<SpecimenFeedback> getFeedbackList() {
logger.info("getFeedbackList: entered, this.feedbackList: " + this.feedbackList);
return this.feedbackList;
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/samply/feedbackagent/service/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.samply.feedbackagent.service;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/** Environment configuration parameters. */
@Data
@Component
public class Configuration {
@Value("${fa.cors.origin}")
private String corsOrigin;
}

6 changes: 4 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
server.port=8072
#spring.h2.console.enabled=true

logging.file = /var/tmp/mylog.log
#logging.file = /var/tmp/mylog.log

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
Expand All @@ -12,4 +12,6 @@ spring.datasource.password=docker
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.initialize=true

fa.cors.origin=http://localhost:9000

0 comments on commit 9dd3557

Please sign in to comment.