-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from input-output-hk/task/java-sample
Add examples in Java (Spring Boot) for PSG gRPC Services
- Loading branch information
Showing
19 changed files
with
791 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# PSG Services - JAVA example (Spring Boot) | ||
|
||
## Prerequisites | ||
Install the following tools: | ||
|
||
- [Java](https://www.oracle.com/java/technologies/downloads/) | ||
- [Protocol Buffer Compiler](https://grpc.io/docs/protoc-installation/) | ||
|
||
**NOTE:** make sure, that **for development and testing purposes** you are using **[PSG Services - Testnet](https://psg-testnet.iog.services/)** | ||
|
||
## How to run example service: | ||
|
||
1. Make sure, that you have registered an account in [PSG Self Serve UI](https://psg.iog.services/), purchased a package and generated API Token | ||
|
||
2. Replace CLIENT_ID and API_TOKEN with your PSG Self Serve UI account values in the src/resources/application.properties file: | ||
```shell | ||
clientId=CLIENT_ID | ||
token=API_TOKEN | ||
``` | ||
|
||
3. Replace AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_S3_BUCKET and BUCKET_REGION with your S3 account values and replace demo-file-path with your desired path in src/resources/application.properties: | ||
```shell | ||
s3.key=AWS_ACCCESS_KEY | ||
s3.secret=AWS_SECRET_KEY | ||
s3.bucket=AWS_S3_BUCKET | ||
s3.region=BUCKET_REGION | ||
``` | ||
|
||
3. Run Spring Boot application by executing the following command in terminal: | ||
```shell | ||
$ mvn spring-boot:run | ||
``` | ||
### Metadata service | ||
- ListMetadata: ```$ curl -X GET "http://localhost:8080/listmetadata"``` | ||
|
||
- SubmitMetadata:```$ curl -X POST "http://localhost:8080/submitmetadata?metadata=SOMEDATA"``` | ||
|
||
### StoreAndHash service | ||
- StoreAndHash (save file at AWS S3): ```$ curl -X POST "http://localhost:8080/storeAws?path=test&content=test"``` | ||
- StoreAndHash (save file at IPFS): ```$ curl -X POST "http://localhost:8080/storeIpfs?host=IPFS_HOST&port=IPFS_PORT&content=testcontent"``` | ||
**NOTE**: Do not forget to use your own IPFS_HOST and IPFS_PORT values while using POST /storeIpfs endpoint | ||
- StoreAndHash (get results): ```$ curl -X GET "http://localhost:8080/store/result"``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<?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>org.example</groupId> | ||
<artifactId>psg-spring-boot-demo</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.5.6</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<spring-cloud.version>2020.0.4</spring-cloud.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
<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> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.22</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- For both --> | ||
<dependency> | ||
<groupId>net.devh</groupId> | ||
<artifactId>grpc-spring-boot-starter</artifactId> | ||
<version>2.12.0.RELEASE</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-netty-shaded</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<!-- For the client (only) --> | ||
<dependency> | ||
<groupId>net.devh</groupId> | ||
<artifactId>grpc-client-spring-boot-starter</artifactId> | ||
<version>2.12.0.RELEASE</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-netty-shaded</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency><dependency> | ||
<groupId>net.devh</groupId> | ||
<artifactId>grpc-client-spring-boot-autoconfigure</artifactId> | ||
<version>2.12.0.RELEASE</version> | ||
<type>pom</type> | ||
</dependency> | ||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>${spring-cloud.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<build> | ||
<extensions> | ||
<extension> | ||
<groupId>kr.motd.maven</groupId> | ||
<artifactId>os-maven-plugin</artifactId> | ||
<version>1.6.1</version> | ||
</extension> | ||
</extensions> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<failIfNoTests>true</failIfNoTests> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.xolstice.maven.plugins</groupId> | ||
<artifactId>protobuf-maven-plugin</artifactId> | ||
<version>0.6.1</version> | ||
<configuration> | ||
<protocArtifact> | ||
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier} | ||
</protocArtifact> | ||
<pluginId>grpc-java</pluginId> | ||
<pluginArtifact> | ||
io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier} | ||
</pluginArtifact> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>compile</goal> | ||
<goal>compile-custom</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spring-snapshots</id> | ||
<name>Spring Snapshots</name> | ||
<url>http://repo.spring.io/libs-snapshot-local</url> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
</snapshots> | ||
</repository> | ||
<repository> | ||
<id>spring-milestones</id> | ||
<name>Spring Milestones</name> | ||
<url>http://repo.spring.io/libs-milestone-local</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</repository> | ||
<repository> | ||
<id>spring-releases</id> | ||
<name>Spring Releases</name> | ||
<url>http://repo.spring.io/release</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</repository> | ||
</repositories> | ||
<pluginRepositories> | ||
<pluginRepository> | ||
<id>spring-snapshots</id> | ||
<name>Spring Snapshots</name> | ||
<url>http://repo.spring.io/libs-snapshot-local</url> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
</snapshots> | ||
</pluginRepository> | ||
<pluginRepository> | ||
<id>spring-milestones</id> | ||
<name>Spring Milestones</name> | ||
<url>http://repo.spring.io/libs-milestone-local</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</pluginRepository> | ||
<pluginRepository> | ||
<id>spring-plugin-snapshots</id> | ||
<name>Spring Snapshots</name> | ||
<url>http://repo.spring.io/plugins-snapshot-local</url> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
</snapshots> | ||
</pluginRepository> | ||
<pluginRepository> | ||
<id>spring-plugin-milestones</id> | ||
<name>Spring Milestones</name> | ||
<url>http://repo.spring.io/plugins-release-local</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</pluginRepository> | ||
</pluginRepositories> | ||
|
||
</project> |
11 changes: 11 additions & 0 deletions
11
examples/java/psg-spring-boot-demo/src/main/java/io/psg/demo/PsgBootApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.psg.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class PsgBootApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(PsgBootApplication.class, args); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...es/java/psg-spring-boot-demo/src/main/java/io/psg/demo/controller/MetadataController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.psg.demo.controller; | ||
|
||
import io.psg.demo.model.TransactionStatus; | ||
import io.psg.demo.service.DataService; | ||
import lombok.extern.java.Log; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@Log | ||
public class MetadataController { | ||
@Autowired | ||
DataService dataService; | ||
|
||
@GetMapping("/listmetadata") | ||
public List<TransactionStatus> list() { | ||
return dataService.getMetadata(); | ||
} | ||
|
||
@PostMapping("/submitmetadata") | ||
public List<TransactionStatus> submit(@RequestParam("metadata") String metadata) { | ||
return dataService.submitMetadata(metadata); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/java/psg-spring-boot-demo/src/main/java/io/psg/demo/controller/StoreController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.psg.demo.controller; | ||
|
||
import io.psg.demo.model.StoreResult; | ||
import io.psg.demo.service.StoreService; | ||
import lombok.extern.java.Log; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@Log | ||
public class StoreController { | ||
@Autowired | ||
StoreService storeService; | ||
|
||
@PostMapping("/storeAws") | ||
public void storeFileAtAws(@RequestParam("path") String path, @RequestParam("content") String fileContent) { | ||
storeService.storeAtAws(path, fileContent); | ||
} | ||
|
||
@PostMapping("/storeIpfs") | ||
public void storeFileAtIpfs(@RequestParam("host") String host, @RequestParam("port") Integer port, @RequestParam("content") String fileContent) { | ||
storeService.storeAtIpfs(host, port, fileContent); | ||
} | ||
|
||
@GetMapping("/store/result") | ||
public List<StoreResult> result() { | ||
return storeService.getResults(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/java/psg-spring-boot-demo/src/main/java/io/psg/demo/model/StoreResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.psg.demo.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class StoreResult { | ||
private String hash; | ||
private String url; | ||
private String errorCode; | ||
private String errorMessage; | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/java/psg-spring-boot-demo/src/main/java/io/psg/demo/model/TransactionStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.psg.demo.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class TransactionStatus { | ||
private String id; | ||
private String state; | ||
private String metadata; | ||
private String errorCode; | ||
private String errorMessage; | ||
} |
Oops, something went wrong.