aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2022-01-25 23:57:08 +0000
committerKevin J Hoerr <kjhoerr@protonmail.com>2022-01-25 23:57:08 +0000
commit11264e508d887a0f8e4317963c0ee032b551023d (patch)
treec6d3601cf6f01892c9763686e3f72b18f918c875
parentfee02c8d30869f93a8bb8ac13609bb43d08e08fa (diff)
downloadpantry-11264e508d887a0f8e4317963c0ee032b551023d.tar.gz
pantry-11264e508d887a0f8e4317963c0ee032b551023d.tar.bz2
pantry-11264e508d887a0f8e4317963c0ee032b551023d.zip
Add compose for MariaDB
-rw-r--r--.devcontainer/devcontainer.json18
-rw-r--r--.devcontainer/docker-compose.yml48
-rw-r--r--pom.xml4
-rw-r--r--src/main/java/dev/submelon/pantry/Item.java51
-rw-r--r--src/main/java/dev/submelon/pantry/ItemController.java33
-rw-r--r--src/main/java/dev/submelon/pantry/ItemRepository.java7
-rw-r--r--src/main/java/dev/submelon/pantry/PantryApplication.java14
-rw-r--r--src/main/resources/application.properties7
8 files changed, 164 insertions, 18 deletions
diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
index 9e846c0..a89c455 100644
--- a/.devcontainer/devcontainer.json
+++ b/.devcontainer/devcontainer.json
@@ -2,19 +2,9 @@
// https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/java
{
"name": "Java",
- "build": {
- "dockerfile": "Dockerfile",
- "args": {
- // Update the VARIANT arg to pick a Java version: 11, 17
- // Append -bullseye or -buster to pin to an OS version.
- // Use the -bullseye variants on local arm64/Apple Silicon.
- "VARIANT": "17",
- // Options
- "INSTALL_MAVEN": "true",
- "INSTALL_GRADLE": "false",
- "NODE_VERSION": "lts/*"
- }
- },
+ "dockerComposeFile": "docker-compose.yml",
+ "service": "pantry",
+ "workspaceFolder": "/workspace",
// Set *default* container specific settings.json values on container create.
"settings": {
@@ -30,7 +20,7 @@
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
- // "postCreateCommand": "java -version",
+ "postCreateCommand": "mvn dependency:resolve",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml
new file mode 100644
index 0000000..2818283
--- /dev/null
+++ b/.devcontainer/docker-compose.yml
@@ -0,0 +1,48 @@
+version: '3'
+
+services:
+ pantry:
+ build:
+ context: .
+ dockerfile: Dockerfile
+ args:
+ # Volume directory for hosted files. This directory is created on image build with proper permissions. This is set as an env-var in the Dockerfile
+ VARIANT: "17"
+
+ # Options
+ INSTALL_MAVEN: "true"
+ INSTALL_GRADLE: "false"
+ NODE_VERSION: "lts/*"
+
+ environment:
+ MARIADB_HOST: "pantry-db"
+
+ volumes:
+ - ..:/workspace:cached
+
+ # Overrides default command so things don't shut down after the process ends.
+ command: sleep infinity
+
+ # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
+ network_mode: service:pantry-db
+
+ # Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
+ # (Adding the "ports" property to this file will not forward from a Codespace.)
+
+ pantry-db:
+ image: mariadb:10.7
+ restart: unless-stopped
+ volumes:
+ - pantrydb-data:/var/lib/mysql
+
+ environment:
+ MARIADB_RANDOM_ROOT_PASSWORD: "true"
+ MARIADB_DATABASE: "pantry-db"
+ MARIADB_USER: "pantry-user"
+ MARIADB_PASSWORD: "i2j9ds0g"
+
+ # Add "forwardPorts": ["27017"] to **devcontainer.json** to forward MongoDB locally.
+ # (Adding the "ports" property to this file will not forward from a Codespace.)
+
+volumes:
+ pantrydb-data: \ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 5f06bb3..b6bcac5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -43,6 +43,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
+ </dependency>
</dependencies>
<build>
diff --git a/src/main/java/dev/submelon/pantry/Item.java b/src/main/java/dev/submelon/pantry/Item.java
new file mode 100644
index 0000000..1df7e91
--- /dev/null
+++ b/src/main/java/dev/submelon/pantry/Item.java
@@ -0,0 +1,51 @@
+package dev.submelon.pantry;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+@Entity
+public class Item {
+ @Id
+ @GeneratedValue(strategy=GenerationType.AUTO)
+ private Integer id;
+
+ private String name;
+
+ private String description;
+
+ private double quantity;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public double getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(double quantity) {
+ this.quantity = quantity;
+ }
+}
diff --git a/src/main/java/dev/submelon/pantry/ItemController.java b/src/main/java/dev/submelon/pantry/ItemController.java
new file mode 100644
index 0000000..cd2f3ff
--- /dev/null
+++ b/src/main/java/dev/submelon/pantry/ItemController.java
@@ -0,0 +1,33 @@
+package dev.submelon.pantry;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+@Controller
+@RequestMapping(path="/item")
+public class ItemController {
+ @Autowired
+ private ItemRepository itemRepository;
+
+ @PostMapping(path="/add")
+ public @ResponseBody String addNewItem (@RequestParam String name, @RequestParam String description, @RequestParam double quantity) {
+ Item item = new Item();
+ item.setName(name);
+ item.setDescription(description);
+ item.setQuantity(quantity);
+
+ itemRepository.save(item);
+ return "Ok";
+ }
+
+ @GetMapping(path="/all")
+ public @ResponseBody Iterable<Item> getAllItems() {
+ return itemRepository.findAll();
+ }
+
+}
diff --git a/src/main/java/dev/submelon/pantry/ItemRepository.java b/src/main/java/dev/submelon/pantry/ItemRepository.java
new file mode 100644
index 0000000..c1c9bb5
--- /dev/null
+++ b/src/main/java/dev/submelon/pantry/ItemRepository.java
@@ -0,0 +1,7 @@
+package dev.submelon.pantry;
+
+import org.springframework.data.repository.CrudRepository;
+
+public interface ItemRepository extends CrudRepository<Item, Integer> {
+
+}
diff --git a/src/main/java/dev/submelon/pantry/PantryApplication.java b/src/main/java/dev/submelon/pantry/PantryApplication.java
index 5522f3d..f63fd04 100644
--- a/src/main/java/dev/submelon/pantry/PantryApplication.java
+++ b/src/main/java/dev/submelon/pantry/PantryApplication.java
@@ -2,12 +2,20 @@ package dev.submelon.pantry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
+@RestController
public class PantryApplication {
- public static void main(String[] args) {
- SpringApplication.run(PantryApplication.class, args);
- }
+ @RequestMapping("/")
+ public String home() {
+ return "Check this out: http://localhost:8080/item/all";
+ }
+
+ public static void main(String[] args) {
+ SpringApplication.run(PantryApplication.class, args);
+ }
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 8b13789..da7f829 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1 +1,6 @@
-
+spring.jpa.hibernate.ddl-auto=create-drop
+spring.datasource.url=jdbc:mysql://${MARIADB_HOST:localhost}:3306/pantry-db
+spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
+spring.datasource.username=pantry-user
+spring.datasource.password=i2j9ds0g
+spring.jpa.show-sql=true