Skip to content

Commit

Permalink
Add sample code Java and Rust (#2)
Browse files Browse the repository at this point in the history
* Add sample code Java

* Add sample for Rust
  • Loading branch information
pragnagopa authored Mar 17, 2020
1 parent fbd1c9d commit 623bf7b
Show file tree
Hide file tree
Showing 24 changed files with 1,163 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,4 @@ MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
/go/*.exe
/Java/com.java/target/*
18 changes: 18 additions & 0 deletions Java/BlobTrigger/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"bindings" : [ {
"type" : "blobTrigger",
"direction" : "in",
"name" : "triggerBlob",
"path" : "test-triggerinput-httpworker/{name}",
"dataType" : "binary",
"connection" : "AzureWebJobsStorage"
},
{
"type" : "blob",
"direction" : "out",
"name" : "$return",
"path" : "test-output-httpworker/{name}",
"dataType" : "binary",
"connection" : "AzureWebJobsStorage"
} ]
}
26 changes: 26 additions & 0 deletions Java/HttpTriggerStringReturnValue/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"name": "$return",
"type": "queue",
"direction": "out",
"queueName": "test-output-node",
"connection": "AzureWebJobsStorage"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"name": "$return",
"type": "queue",
"direction": "out",
"queueName": "test-output-node",
"connection": "AzureWebJobsStorage"
}
]
}
26 changes: 26 additions & 0 deletions Java/HttpTriggerWithOutputs/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"name": "$return",
"type": "queue",
"direction": "out",
"queueName": "test-output-node",
"connection": "AzureWebJobsStorage"
}
]
}
18 changes: 18 additions & 0 deletions Java/QueueTrigger/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "test-input-node",
"connection": "AzureWebJobsStorage"
},
{
"name": "$return",
"type": "queue",
"direction": "out",
"queueName": "test-output-node",
"connection": "AzureWebJobsStorage"
}
]
}
25 changes: 25 additions & 0 deletions Java/QueueTriggerWithOutputs/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "test-inputmultiout-node",
"connection": "AzureWebJobsStorage"
},
{
"name": "$return",
"type": "queue",
"direction": "out",
"queueName": "test-output-node",
"connection": "AzureWebJobsStorage"
},
{
"name": "output1",
"type": "queue",
"direction": "out",
"queueName": "test-output1-node",
"connection": "AzureWebJobsStorage"
}
]
}
35 changes: 35 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Azure Functions Http Worker - Java API Sample

## Pre-reqs
- Java 8
- Maven (for packaging)

## Build + Run the Java API standalone
- Use the following configuration in your `launch.json` to run the Java API:
```json
"configurations": [
{
"type": "java",
"name": "Debug (Launch)",
"request": "launch",
"mainClass": "",
"env": {
"FUNCTIONS_HTTPWORKER_PORT": 5000
}
},
```
- Hit the endpoints at `http://localhost:5000/SimpleHttpTrigger`

## Run with Functions
- Package the Java api into a jar file:
```bash
mvn package -f "java/com.java/pom.xml"
```
- In a terminal, `cd` to the `functions` directory and run:
```bash
func start
```
> All being well you should see the Spring ascii logo, where the functions runtime has started the proces.

- Hit the endpoints at `http://localhost:7071/api/SimpleHttpTrigger`

19 changes: 19 additions & 0 deletions Java/SimpleHttpTrigger/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
15 changes: 15 additions & 0 deletions Java/SimpleHttpTriggerWithReturn/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"bindings": [{
"type": "httpTrigger",
"authLevel": "anonymous",
"direction": "in",
"methods": ["GET",
"POST"],
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}]
}
43 changes: 43 additions & 0 deletions Java/com.java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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>com.java</groupId>
<artifactId>com.java</artifactId>
<version>1.0</version>

<name>com.java</name>
<url></url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
12 changes: 12 additions & 0 deletions Java/com.java/src/main/java/com/java/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
82 changes: 82 additions & 0 deletions Java/com.java/src/main/java/com/java/JavaAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.java;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import com.java.models.*;

@RestController
public class JavaAPI {

@PostMapping("/QueueTrigger")
public InvokeResponse QueueTriggerHandler(@RequestBody InvokeRequest request){
System.out.println("Java queue trigger handler");
InvokeResponse resp = new InvokeResponse();
resp.Logs.add("Java: test log1");
resp.Logs.add("Java: test log2");
resp.ReturnValue = "HelloWorld";
return resp;
}

@PostMapping("/QueueTriggerWithOutputs")
public InvokeResponse QueueTriggerWithOutputsHandler(@RequestBody InvokeRequest request){
System.out.println("Java queue trigger handler");
InvokeResponse resp = new InvokeResponse();
resp.Logs.add("Java: test log1");
resp.Logs.add("Java: test log2");
resp.Outputs.put("output1", "output from Java");
resp.ReturnValue = 100;
return resp;
}

@PostMapping("/BlobTrigger")
public InvokeResponse BlobTriggerHandler(@RequestBody InvokeRequest request){
InvokeResponse resp = new InvokeResponse();
resp.Logs.add("Java: test log1");
resp.Logs.add("Java: test log2");
resp.ReturnValue = request.Data;
return resp;
}

@RequestMapping(value = {"/SimpleHttpTrigger", "/SimpleHttpTriggerWithReturn"})
public Map<String, String> SimpleHttpTrigger(){
System.out.println("Java: Simple Http Trigger");

Map<String, String> dynOutput = new HashMap<String, String>();
dynOutput.put("home", "123-456-789");
dynOutput.put("office", "987-654-321");

return dynOutput;
}

@PostMapping(value = {"/HttpTriggerWithOutputs", "/HttpTriggerStringReturnValue"})
public InvokeResponse HttpTriggerWithOutputs(){
System.out.println("Java: Http Trigger with Outputs");
InvokeResponse resp = new InvokeResponse();
resp.ReturnValue = "return val";

Map<String, String> dynOutput = new HashMap<String, String>();
dynOutput.put("home", "123-456-789");
dynOutput.put("office", "987-654-321");

resp.Outputs.put("output1", "Mark Taylor");
resp.Outputs.put("output2", dynOutput);

Map<String, Object> headers = new HashMap<String, Object>();
headers.put("header1", "header1Val");
headers.put("header2", "header2Val");

Map<String, Object> httpRes = new HashMap<String,Object>();
httpRes.put("statusCode", "201");
httpRes.put("body", "my world");
httpRes.put("headers", headers);

resp.Outputs.put("res", httpRes);
return resp;
}
}
16 changes: 16 additions & 0 deletions Java/com.java/src/main/java/com/java/ServletConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.java;

import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
int portNumber = Integer.parseInt(System.getenv("FUNCTIONS_HTTPWORKER_PORT"));
container.setPort(portNumber);
});
}
}
Loading

0 comments on commit 623bf7b

Please sign in to comment.