From d3f77ea51dd72b055307ac259c3caf8091307aa6 Mon Sep 17 00:00:00 2001 From: Mole Shang <135e2@135e2.dev> Date: Sun, 15 Dec 2024 00:00:13 +0800 Subject: init --- src/main/java/seu/se/Application.java | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/main/java/seu/se/Application.java (limited to 'src/main/java/seu/se/Application.java') diff --git a/src/main/java/seu/se/Application.java b/src/main/java/seu/se/Application.java new file mode 100644 index 0000000..928bc28 --- /dev/null +++ b/src/main/java/seu/se/Application.java @@ -0,0 +1,102 @@ +package seu.se; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.session.MapSessionRepository; +import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.servlet.NoHandlerFoundException; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +@EnableTransactionManagement +@EnableNeo4jRepositories +@SpringBootApplication +public class Application { + private static final Logger log = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @ControllerAdvice + public static class ExHandler { + @ExceptionHandler(Exception.class) + public ResponseEntity handle(Exception ex, HttpServletRequest request, HttpServletResponse response) { + HttpStatus code = HttpStatus.INTERNAL_SERVER_ERROR; + String status = "internal server error"; + if (ex instanceof NullPointerException) { + code = HttpStatus.BAD_REQUEST; + status = "bad request"; + } + if (ex instanceof NoHandlerFoundException) { + code = HttpStatus.NOT_FOUND; + status = "not found"; + } + var retObj = new ObjectMapper().createObjectNode(); + retObj.put("status", status); + retObj.put("message", ex.getMessage()); + return ResponseEntity.status(code).body(retObj); + } + } + + // in-mem session + @EnableSpringHttpSession + @Configuration(proxyBeanMethods = false) + public static class SpringHttpSessionConfig { + @Bean + public MapSessionRepository sessionRepository() { + return new MapSessionRepository(new ConcurrentHashMap<>()); + } + } + + @Bean + public CommandLineRunner init(UserRepository ur, TreeNodeRepository tnr) { + return (args) -> { + // Load users + try { + List users = new ObjectMapper().readValue( + new ClassPathResource("users.json").getFile(), + new TypeReference<>() { + } + ); + ur.saveAll(users); + } catch (IOException e) { + log.error("Error loading users from JSON file", e); + } + + // Load tree nodes + TreeNode.tnRepo = tnr; + try { + List treeNodes = new ObjectMapper().readValue( + new ClassPathResource("treeNodes.json").getFile(), + new TypeReference<>() { + } + ); + tnr.saveAll(treeNodes); + } catch (IOException e) { + log.error("Error loading tree nodes from JSON file", e); + } + }; + } + +} -- cgit v1.2.3