mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
php langhelper (needs funcy/php), tutorial
This commit is contained in:
@@ -1,23 +1,41 @@
|
|||||||
## Quick Example for a NodeJS Function (4 minutes)
|
# Tutorial 1: NodeJS Function w/ Input (3 minutes)
|
||||||
|
|
||||||
This example will show you how to test and deploy a Node function to Oracle Functions.
|
This example will show you how to test and deploy Node code to Oracle Functions. It will also demonstrate passing data in through stdin.
|
||||||
|
|
||||||
|
### First, run the following commands:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# create your func.yaml file
|
# Initialize your function creating a func.yaml file
|
||||||
fn init <YOUR_DOCKERHUB_USERNAME>/hello
|
fn init <DOCKERHUB_USERNAME>/hello
|
||||||
# build the function
|
|
||||||
fn build
|
# Test your function.
|
||||||
# test it
|
# This will run inside a container exactly how it will on the server. It will also install and vendor dependencies from Gemfile
|
||||||
|
fn run
|
||||||
|
|
||||||
|
# Now try with an input
|
||||||
cat hello.payload.json | fn run
|
cat hello.payload.json | fn run
|
||||||
# push it to Docker Hub
|
|
||||||
fn push
|
# Deploy your functions to the Oracle Functions server (default localhost:8080)
|
||||||
# Create a route to this function on Oracle Functions
|
# This will create a route to your function as well
|
||||||
fn routes create myapp /hello
|
fn deploy myapp
|
||||||
|
```
|
||||||
|
### Now call your function:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl http://localhost:8080/r/myapp/hello
|
||||||
```
|
```
|
||||||
|
|
||||||
Now surf to: http://localhost:8080/r/myapp/hello
|
Or call from a browser: [http://localhost:8080/r/myapp/hello](http://localhost:8080/r/myapp/hello)
|
||||||
|
|
||||||
## Dependencies
|
And now with the JSON input:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/myapp/hello
|
||||||
|
```
|
||||||
|
|
||||||
|
That's it!
|
||||||
|
|
||||||
|
### Note on Dependencies
|
||||||
|
|
||||||
Create a [package.json](https://docs.npmjs.com/getting-started/using-a-package.json) file in your functions directory.
|
Create a [package.json](https://docs.npmjs.com/getting-started/using-a-package.json) file in your functions directory.
|
||||||
|
|
||||||
@@ -39,3 +57,33 @@ request('http://www.google.com', function (error, response, body) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
# In Review
|
||||||
|
|
||||||
|
1. We piped JSON data into the function at the command line
|
||||||
|
```sh
|
||||||
|
cat hello.payload.json | fn run
|
||||||
|
```
|
||||||
|
|
||||||
|
2. We received our function input through **stdin**
|
||||||
|
```node
|
||||||
|
obj = JSON.parse(fs.readFileSync('/dev/stdin').toString())
|
||||||
|
```
|
||||||
|
|
||||||
|
3. We wrote our output to **stdout**
|
||||||
|
```node
|
||||||
|
console.log
|
||||||
|
```
|
||||||
|
|
||||||
|
4. We sent **stderr** to the server logs
|
||||||
|
```node
|
||||||
|
console.error
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
# Next Up
|
||||||
|
## [Tutorial 2: Input Parameters](examples/tutorial/params)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,3 +7,5 @@ try {
|
|||||||
}
|
}
|
||||||
} catch(e) {}
|
} catch(e) {}
|
||||||
console.log("Hello", name, "from Node!");
|
console.log("Hello", name, "from Node!");
|
||||||
|
|
||||||
|
console.error("Stderr goes to the server logs...");
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
FROM funcy/php
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
ADD . /app
|
|
||||||
|
|
||||||
ENTRYPOINT ["php", "hello.php"]
|
|
||||||
@@ -1,10 +1,41 @@
|
|||||||
## Quick Example for a PHP Function (4 minutes)
|
# Tutorial 1: PHP Function w/ Input (3 minutes)
|
||||||
|
|
||||||
This example will show you how to test and deploy Go (Golang) code to Oracle Functions.
|
This example will show you how to test and deploy PHP code to Oracle Functions. It will also demonstrate passing data in through stdin.
|
||||||
|
|
||||||
### 1. Prepare the `func.yaml` file:
|
### First, run the following commands:
|
||||||
|
|
||||||
At func.yaml you will find:
|
```sh
|
||||||
|
# Initialize your function creating a func.yaml file
|
||||||
|
fn init <DOCKERHUB_USERNAME>/hello
|
||||||
|
|
||||||
|
# Test your function.
|
||||||
|
# This will run inside a container exactly how it will on the server. It will also install and vendor dependencies from Gemfile
|
||||||
|
fn run
|
||||||
|
|
||||||
|
# Now try with an input
|
||||||
|
cat hello.payload.json | fn run
|
||||||
|
|
||||||
|
# Deploy your functions to the Oracle Functions server (default localhost:8080)
|
||||||
|
# This will create a route to your function as well
|
||||||
|
fn deploy myapp
|
||||||
|
```
|
||||||
|
### Now call your function:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl http://localhost:8080/r/myapp/hello
|
||||||
|
```
|
||||||
|
|
||||||
|
Or call from a browser: [http://localhost:8080/r/myapp/hello](http://localhost:8080/r/myapp/hello)
|
||||||
|
|
||||||
|
And now with the JSON input:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/myapp/hello
|
||||||
|
```
|
||||||
|
|
||||||
|
That's it!
|
||||||
|
|
||||||
|
### Note on Dependencies
|
||||||
|
|
||||||
```yml
|
```yml
|
||||||
name: USERNAME/hello
|
name: USERNAME/hello
|
||||||
@@ -14,25 +45,6 @@ build:
|
|||||||
- docker run --rm -v "$PWD":/worker -w /worker funcy/php:dev composer install
|
- docker run --rm -v "$PWD":/worker -w /worker funcy/php:dev composer install
|
||||||
```
|
```
|
||||||
|
|
||||||
The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note:
|
|
||||||
the application name is `phpapp` and the route for incoming requests is `/hello`. These informations are relevant for
|
|
||||||
the moment you try to test this function.
|
|
||||||
|
|
||||||
### 2. Build:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# build the function
|
|
||||||
fn build
|
|
||||||
# test it
|
|
||||||
cat hello.payload.json | fn run
|
|
||||||
# push it to Docker Hub
|
|
||||||
fn push
|
|
||||||
# Create a route to this function on Oracle Functions
|
|
||||||
fn routes create phpapp /hello
|
|
||||||
```
|
|
||||||
|
|
||||||
`-v` is optional, but it allows you to see how this function is being built.
|
|
||||||
|
|
||||||
### 3. Queue jobs for your function
|
### 3. Queue jobs for your function
|
||||||
|
|
||||||
Now you can start jobs on your function. Let's quickly queue up a job to try it out.
|
Now you can start jobs on your function. Let's quickly queue up a job to try it out.
|
||||||
@@ -41,8 +53,30 @@ Now you can start jobs on your function. Let's quickly queue up a job to try it
|
|||||||
cat hello.payload.json | fn call phpapp /hello
|
cat hello.payload.json | fn call phpapp /hello
|
||||||
```
|
```
|
||||||
|
|
||||||
Here's a curl example to show how easy it is to do in any language:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/phpapp/hello
|
# In Review
|
||||||
```
|
|
||||||
|
1. We piped JSON data into the function at the command line
|
||||||
|
```sh
|
||||||
|
cat hello.payload.json | fn run
|
||||||
|
```
|
||||||
|
|
||||||
|
2. We received our function input through **stdin**
|
||||||
|
```node
|
||||||
|
obj = JSON.parse(fs.readFileSync('/dev/stdin').toString())
|
||||||
|
```
|
||||||
|
|
||||||
|
3. We wrote our output to **stdout**
|
||||||
|
```node
|
||||||
|
console.log
|
||||||
|
```
|
||||||
|
|
||||||
|
4. We sent **stderr** to the server logs
|
||||||
|
```node
|
||||||
|
console.error
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
# Next Up
|
||||||
|
## [Tutorial 2: Input Parameters](examples/tutorial/params)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
name: USERNAME/hello
|
name: carimura2/hello
|
||||||
version: 0.0.1
|
version: 0.0.1
|
||||||
|
runtime: php
|
||||||
|
entrypoint: php func.php
|
||||||
path: /hello
|
path: /hello
|
||||||
build:
|
max_concurrency: 1
|
||||||
- docker run --rm -v "$PWD":/worker -w /worker funcy/php:dev composer install
|
|
||||||
|
|||||||
15
fn/init.go
15
fn/init.go
@@ -23,13 +23,14 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
fileExtToRuntime = map[string]string{
|
fileExtToRuntime = map[string]string{
|
||||||
".go": "go",
|
".go": "go",
|
||||||
".js": "node",
|
".js": "node",
|
||||||
".rb": "ruby",
|
".rb": "ruby",
|
||||||
".py": "python",
|
".py": "python",
|
||||||
".rs": "rust",
|
".php": "php",
|
||||||
".cs": "dotnet",
|
".rs": "rust",
|
||||||
".fs": "dotnet",
|
".cs": "dotnet",
|
||||||
|
".fs": "dotnet",
|
||||||
}
|
}
|
||||||
|
|
||||||
fnInitRuntimes []string
|
fnInitRuntimes []string
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ func GetLangHelper(lang string) LangHelper {
|
|||||||
return &RubyLangHelper{}
|
return &RubyLangHelper{}
|
||||||
case "python":
|
case "python":
|
||||||
return &PythonHelper{}
|
return &PythonHelper{}
|
||||||
|
case "php":
|
||||||
|
return &PhpLangHelper{}
|
||||||
case "rust":
|
case "rust":
|
||||||
return &RustLangHelper{}
|
return &RustLangHelper{}
|
||||||
case "dotnet":
|
case "dotnet":
|
||||||
|
|||||||
49
fn/langs/php.go
Normal file
49
fn/langs/php.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package langs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PhpLangHelper struct {
|
||||||
|
BaseHelper
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lh *PhpLangHelper) Entrypoint() string {
|
||||||
|
return "php func.php"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lh *PhpLangHelper) HasPreBuild() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lh *PhpLangHelper) PreBuild() error {
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exists(filepath.Join(wd, "composer.json")) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
pbcmd := fmt.Sprintf("docker run --rm -v %s:/worker -w /worker funcy/php:dev composer install", wd)
|
||||||
|
fmt.Println("Running prebuild command:", pbcmd)
|
||||||
|
parts := strings.Fields(pbcmd)
|
||||||
|
head := parts[0]
|
||||||
|
parts = parts[1:len(parts)]
|
||||||
|
cmd := exec.Command(head, parts...)
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return fmt.Errorf("error running docker build: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lh *PhpLangHelper) AfterBuild() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -37,7 +37,7 @@ func newFn() *cli.App {
|
|||||||
app.Version = vers.Version
|
app.Version = vers.Version
|
||||||
app.Authors = []cli.Author{{Name: "Oracle Corporation"}}
|
app.Authors = []cli.Author{{Name: "Oracle Corporation"}}
|
||||||
app.Description = "Oracle Functions command line tools"
|
app.Description = "Oracle Functions command line tools"
|
||||||
app.UsageText = `Check the manual at https://github.com/treeder/functions/blob/master/fn/README.md`
|
app.UsageText = `Check the manual at https://gitlab.oracledx.com/odx/functions/blob/master/fn/README.md`
|
||||||
|
|
||||||
cli.AppHelpTemplate = `{{.Name}} {{.Version}}{{if .Description}}
|
cli.AppHelpTemplate = `{{.Name}} {{.Version}}{{if .Description}}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user