Skip to content

Commit d3aec4f

Browse files
BupycHukc0ze
authored andcommitted
java support is added (#611)
1 parent 7743230 commit d3aec4f

File tree

9 files changed

+140
-10
lines changed

9 files changed

+140
-10
lines changed

examples/hello/java/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.class
2+
*.iml
3+
4+
func.yaml

examples/hello/java/Func.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import com.google.gson.Gson;
2+
import com.google.gson.stream.JsonReader;
3+
4+
import java.io.InputStreamReader;
5+
6+
public class Func {
7+
8+
/**
9+
* @param args the command line arguments
10+
*/
11+
public static void main(String[] args) {
12+
JsonReader br = new JsonReader(new InputStreamReader(System.in));
13+
Gson gson = new Gson();
14+
Payload payload = gson.fromJson(br, Payload.class);
15+
if (payload == null) {
16+
payload = new Payload("world");
17+
}
18+
19+
System.out.printf("Hello %s!\n", payload.getName());
20+
21+
}
22+
}
23+
24+

examples/hello/java/Payload.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Payload {
2+
private String name;
3+
4+
public Payload() {
5+
}
6+
7+
public Payload(String name) {
8+
this.name = name;
9+
}
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
}

examples/hello/java/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Quick Example for a Java Function (3 minutes)
2+
3+
This example will show you how to test and deploy Java code to IronFunctions.
4+
5+
```sh
6+
# create your func.yaml file
7+
fn init <YOUR_DOCKERHUB_USERNAME>/hello
8+
# build the function
9+
fn build
10+
# test it
11+
cat hello.payload.json | fn run
12+
# push it to Docker Hub
13+
fn push
14+
# Create a route to this function on IronFunctions
15+
fn routes create myapp /hello
16+
```
17+
18+
Now you can call your function on IronFunctions:
19+
20+
```sh
21+
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/myapp/hello
22+
```
23+
24+
25+
## Dependencies
26+
27+
Be sure you're dependencies are in the `lib/` directory and that's it.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "Johnny"
3+
}
186 KB
Binary file not shown.

fn/init.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ import (
2323

2424
var (
2525
fileExtToRuntime = map[string]string{
26-
".go": "go",
27-
".js": "node",
28-
".rb": "ruby",
29-
".py": "python",
30-
".rs": "rust",
31-
".cs": "dotnet",
32-
".fs": "dotnet",
26+
".go": "go",
27+
".js": "node",
28+
".rb": "ruby",
29+
".py": "python",
30+
".rs": "rust",
31+
".cs": "dotnet",
32+
".fs": "dotnet",
33+
".java": "java",
34+
}
35+
36+
filenames = []string{
37+
"func",
38+
"Func",
3339
}
3440

3541
fnInitRuntimes []string
@@ -194,9 +200,11 @@ func (a *initFnCmd) buildFuncFile(c *cli.Context) error {
194200

195201
func detectRuntime(path string) (runtime string, err error) {
196202
for ext, runtime := range fileExtToRuntime {
197-
fn := filepath.Join(path, fmt.Sprintf("func%s", ext))
198-
if exists(fn) {
199-
return runtime, nil
203+
for _, filename := range filenames {
204+
fn := filepath.Join(path, fmt.Sprintf("%s%s", filename, ext))
205+
if exists(fn) {
206+
return runtime, nil
207+
}
200208
}
201209
}
202210
return "", fmt.Errorf("no supported files found to guess runtime, please set runtime explicitly with --runtime flag")

fn/langs/base.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func GetLangHelper(lang string) LangHelper {
1515
return &RustLangHelper{}
1616
case "dotnet":
1717
return &DotNetLangHelper{}
18+
case "java":
19+
return &JavaLangHelper{}
1820
case "lambda-nodejs4.3":
1921
return &LambdaNodeHelper{}
2022
}

fn/langs/java.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package langs
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
)
8+
9+
type JavaLangHelper struct {
10+
BaseHelper
11+
}
12+
13+
func (lh *JavaLangHelper) Entrypoint() string {
14+
return "java -cp lib/*:. Func"
15+
}
16+
17+
func (lh *JavaLangHelper) HasPreBuild() bool {
18+
return true
19+
}
20+
21+
// PreBuild for Go builds the binary so the final image can be as small as possible
22+
func (lh *JavaLangHelper) PreBuild() error {
23+
wd, err := os.Getwd()
24+
if err != nil {
25+
return err
26+
}
27+
28+
cmd := exec.Command(
29+
"docker", "run",
30+
"--rm", "-v",
31+
wd+":/java", "-w", "/java", "iron/java:dev",
32+
"/bin/sh", "-c", "javac -cp lib/*:. *.java",
33+
)
34+
cmd.Stderr = os.Stderr
35+
cmd.Stdout = os.Stdout
36+
if err := cmd.Run(); err != nil {
37+
return fmt.Errorf("error running docker build: %v", err)
38+
}
39+
return nil
40+
}
41+
42+
func (lh *JavaLangHelper) AfterBuild() error {
43+
return nil
44+
}

0 commit comments

Comments
 (0)