initial
This commit is contained in:
parent
aa7c30950c
commit
72716e1c28
41 changed files with 15461 additions and 0 deletions
3
backend/.gitignore
vendored
Normal file
3
backend/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
backend/target
|
||||
|
||||
.DS_Store
|
||||
9
backend/README.md
Normal file
9
backend/README.md
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# Start the Server
|
||||
|
||||
```bash
|
||||
mvn clean spring-boot:run
|
||||
```
|
||||
|
||||
That's it. The server will be using port 8081 which can be changed in `application.properties`
|
||||
|
||||
Now go to the `../frontend` folder and follow the `README` instructions
|
||||
41
backend/pom.xml
Normal file
41
backend/pom.xml
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.5.7</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.mycompany.backend</groupId>
|
||||
<artifactId>demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>25</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</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>
|
||||
13
backend/src/main/java/com/mycompany/backend/demo/Main.java
Normal file
13
backend/src/main/java/com/mycompany/backend/demo/Main.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package com.mycompany.backend.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
static void main(String[] args) {
|
||||
SpringApplication.run(Main.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.mycompany.backend.demo.api;
|
||||
|
||||
import com.mycompany.backend.demo.data.DataType;
|
||||
import com.mycompany.backend.demo.data.DataTypeConfiguration;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
public interface DataService {
|
||||
|
||||
DataTypeConfiguration getConfiguration();
|
||||
|
||||
@Nullable
|
||||
DataType getDataType(String id);
|
||||
|
||||
DataTypeConfiguration updateType(DataType newEntity);
|
||||
|
||||
void deleteType(String id);
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package com.mycompany.backend.demo.config;
|
||||
|
||||
import com.mycompany.backend.demo.api.DataService;
|
||||
import com.mycompany.backend.demo.data.DataServiceImpl;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* These beans will be created on boot time and can be later injected in any service/component.
|
||||
* @see org.springframework.context.annotation.Configuration
|
||||
*/
|
||||
@Configuration
|
||||
public class AppConfiguration implements WebMvcConfigurer {
|
||||
private static final Logger LOGGER = Logger.getLogger(AppConfiguration.class.getName());
|
||||
|
||||
@Bean
|
||||
public DataService repository() {
|
||||
LOGGER.info("Initializing data repository bean...");
|
||||
return new DataServiceImpl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**") // all endpoints
|
||||
.allowedOrigins("*") // no restrictions
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("*");
|
||||
|
||||
LOGGER.info("CORS policies configured for the REST API");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
package com.mycompany.backend.demo.data;
|
||||
|
||||
import com.mycompany.backend.demo.api.DataService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* A simple in-memory database
|
||||
*/
|
||||
public class DataServiceImpl implements DataService {
|
||||
private static final List<DataType> INITIAL_DATA = List.of(
|
||||
new DataType("id1", "Asset", List.of(
|
||||
new DataType.Property("category", "string")
|
||||
)),
|
||||
|
||||
new DataType("id2", "Employee", List.of(
|
||||
new DataType.Property("name", "string"),
|
||||
new DataType.Property("email", "string")
|
||||
))
|
||||
);
|
||||
|
||||
private final Map<String, DataType> dataTypes = new ConcurrentHashMap<>();
|
||||
|
||||
public DataServiceImpl() {
|
||||
// Initialize with initial data (won't be needed with a database layer)
|
||||
for (DataType s : INITIAL_DATA) {
|
||||
dataTypes.put(s.id(), s);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataTypeConfiguration getConfiguration() {
|
||||
var sorted = new ArrayList<>(dataTypes.values());
|
||||
sorted.sort(Comparator.comparing(DataType::name));
|
||||
return new DataTypeConfiguration(sorted);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataType getDataType(String id) {
|
||||
return dataTypes.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataTypeConfiguration updateType(DataType newEntity) {
|
||||
dataTypes.put(newEntity.id(), newEntity);
|
||||
// 1. Do other stuff here, e.g. update the related objects.
|
||||
// This part must be performed in a single transaction
|
||||
|
||||
// 2. Return the updated configuration with all the changes included
|
||||
return getConfiguration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteType(String id) {
|
||||
dataTypes.remove(id);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.mycompany.backend.demo.data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record DataType(String id, String name, List<Property > properties) {
|
||||
|
||||
record Property(String name, String type) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.mycompany.backend.demo.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A dummy global configuration structure, like customer-related ontologies.
|
||||
* For now, only
|
||||
* @param dataTypes
|
||||
*/
|
||||
public record DataTypeConfiguration(List<DataType> dataTypes) implements Serializable {
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
/**
|
||||
* Mocks of the data-layer and service classes.
|
||||
*/
|
||||
package com.mycompany.backend.demo.data;
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
package com.mycompany.backend.demo.rest;
|
||||
|
||||
import com.mycompany.backend.demo.api.DataService;
|
||||
import com.mycompany.backend.demo.data.DataType;
|
||||
import com.mycompany.backend.demo.data.DataTypeConfiguration;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/data")
|
||||
public class StaticRoutes {
|
||||
private static final Logger LOGGER = Logger.getLogger(StaticRoutes.class.getName());
|
||||
|
||||
final DataService dataService;
|
||||
|
||||
/**
|
||||
* Spring Boot will automatically find the {@link DataService} bean as it is
|
||||
* configured in the {@link com.mycompany.backend.demo.config.AppConfiguration}
|
||||
*
|
||||
* @param dataService bean
|
||||
*/
|
||||
public StaticRoutes(DataService dataService) {
|
||||
this.dataService = dataService;
|
||||
LOGGER.info("Basic static routes created.");
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple "get all" endpoint
|
||||
*/
|
||||
@GetMapping
|
||||
public ResponseEntity<DataTypeConfiguration> listDataTypes() {
|
||||
LOGGER.info("Returning all data types...");
|
||||
return ResponseEntity.ok(dataService.getConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple "get by id" endpoint
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<DataType> getDataType(@PathVariable("id") String id) {
|
||||
LOGGER.info("Returning all data types...");
|
||||
var found = dataService.getDataType(id);
|
||||
if (found == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
} else {
|
||||
return ResponseEntity.ok(found);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This endpoint updates a single data structure element and returns a new list
|
||||
* of ontologies (they may be interconnected)
|
||||
*
|
||||
* @param modified the updated object
|
||||
* @return the list
|
||||
*/
|
||||
@PostMapping
|
||||
public ResponseEntity<DataTypeConfiguration> updateConfiguration(@RequestBody DataType modified) {
|
||||
LOGGER.info("Updating data type and related data: " + modified);
|
||||
return ResponseEntity.ok(dataService.updateType(modified));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable("id") String id) {
|
||||
LOGGER.info("Deleting data type with id: " + id);
|
||||
dataService.deleteType(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
3
backend/src/main/resources/application-dev.properties
Normal file
3
backend/src/main/resources/application-dev.properties
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# This "dev" profile will override the default application.properties
|
||||
# See the Spring Boot docs on how to start different profiles in IDE on via command line
|
||||
logging.level.root = trace
|
||||
3
backend/src/main/resources/application.properties
Normal file
3
backend/src/main/resources/application.properties
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
spring.application.name=DEMO APP
|
||||
server.port=8081
|
||||
server.servlet.context-path=/v1
|
||||
17
backend/src/main/resources/static/index.html
Normal file
17
backend/src/main/resources/static/index.html
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Hi There!</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h3>Hi There!</h3>
|
||||
<p>
|
||||
In addition to the REST endpoints, Spring Boot can also serve static resources.
|
||||
</p>
|
||||
<p>
|
||||
For REST-only production deployments, serving static pages is usually disabled.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.mycompany.backend.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class MainTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue