Faas semi worfking
This commit is contained in:
30
faas/examples/hello-world/Dockerfile
Normal file
30
faas/examples/hello-world/Dockerfile
Normal file
@ -0,0 +1,30 @@
|
||||
# Build stage
|
||||
FROM golang:1.23-alpine AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy go mod files
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
# Download dependencies
|
||||
RUN go mod download
|
||||
|
||||
# Copy source code
|
||||
COPY . .
|
||||
|
||||
# Build the binary
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o handler .
|
||||
|
||||
# Final stage
|
||||
FROM alpine:latest
|
||||
|
||||
# Install ca-certificates
|
||||
RUN apk --no-cache add ca-certificates
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the binary from builder stage
|
||||
COPY --from=builder /app/handler .
|
||||
|
||||
# Run the handler
|
||||
CMD ["./handler"]
|
||||
75
faas/examples/hello-world/README.md
Normal file
75
faas/examples/hello-world/README.md
Normal file
@ -0,0 +1,75 @@
|
||||
# Hello World Function Example
|
||||
|
||||
This is a simple example function that demonstrates how to create and deploy functions in the Skybridge FaaS platform.
|
||||
|
||||
## Function Description
|
||||
|
||||
The function takes a JSON input with an optional `name` field and returns a greeting message.
|
||||
|
||||
### Input Format
|
||||
```json
|
||||
{
|
||||
"name": "John"
|
||||
}
|
||||
```
|
||||
|
||||
### Output Format
|
||||
```json
|
||||
{
|
||||
"message": "Hello, John!",
|
||||
"input": {
|
||||
"name": "John"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Building the Function
|
||||
|
||||
To build the function as a Docker image:
|
||||
|
||||
```bash
|
||||
docker build -t hello-world-function .
|
||||
```
|
||||
|
||||
## Testing the Function Locally
|
||||
|
||||
To test the function locally:
|
||||
|
||||
```bash
|
||||
# Test with a name
|
||||
docker run -e FUNCTION_INPUT='{"name": "Alice"}' hello-world-function
|
||||
|
||||
# Test without a name (defaults to "World")
|
||||
docker run hello-world-function
|
||||
```
|
||||
|
||||
## Deploying to Skybridge FaaS
|
||||
|
||||
Once you have the Skybridge FaaS platform running, you can deploy this function using the API:
|
||||
|
||||
1. Create the function:
|
||||
```bash
|
||||
curl -X POST http://localhost:8083/api/functions \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "X-User-Email: test@example.com" \
|
||||
-d '{
|
||||
"name": "hello-world",
|
||||
"image": "hello-world-function",
|
||||
"runtime": "custom",
|
||||
"memory": 128,
|
||||
"timeout": "30s"
|
||||
}'
|
||||
```
|
||||
|
||||
2. Deploy the function:
|
||||
```bash
|
||||
curl -X POST http://localhost:8083/api/functions/{function-id}/deploy \
|
||||
-H "X-User-Email: test@example.com"
|
||||
```
|
||||
|
||||
3. Execute the function:
|
||||
```bash
|
||||
curl -X POST http://localhost:8083/api/functions/{function-id}/execute \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "X-User-Email: test@example.com" \
|
||||
-d '{"input": {"name": "Bob"}}'
|
||||
23
faas/examples/hello-world/build.sh
Executable file
23
faas/examples/hello-world/build.sh
Executable file
@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Build script for hello-world function
|
||||
|
||||
set -e
|
||||
|
||||
echo "Building hello-world function..."
|
||||
|
||||
# Build the Docker image
|
||||
docker build -t hello-world-function .
|
||||
|
||||
echo "Testing the function locally..."
|
||||
|
||||
# Test without input
|
||||
echo "Test 1: No input"
|
||||
docker run --rm hello-world-function
|
||||
|
||||
echo ""
|
||||
echo "Test 2: With name input"
|
||||
docker run --rm -e FUNCTION_INPUT='{"name": "Alice"}' hello-world-function
|
||||
|
||||
echo ""
|
||||
echo "Function built and tested successfully!"
|
||||
5
faas/examples/hello-world/go.mod
Normal file
5
faas/examples/hello-world/go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module hello-world-function
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.4
|
||||
44
faas/examples/hello-world/main.go
Normal file
44
faas/examples/hello-world/main.go
Normal file
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Read input from environment variable
|
||||
input := os.Getenv("FUNCTION_INPUT")
|
||||
if input == "" {
|
||||
input = "{}"
|
||||
}
|
||||
|
||||
// Parse input
|
||||
var inputData map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(input), &inputData); err != nil {
|
||||
fmt.Printf("Error parsing input: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Process the input and generate output
|
||||
name, ok := inputData["name"].(string)
|
||||
if !ok {
|
||||
name = "World"
|
||||
}
|
||||
|
||||
message := fmt.Sprintf("Hello, %s!", name)
|
||||
|
||||
// Output result as JSON
|
||||
result := map[string]interface{}{
|
||||
"message": message,
|
||||
"input": inputData,
|
||||
}
|
||||
|
||||
output, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
fmt.Printf("Error marshaling output: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(string(output))
|
||||
}
|
||||
Reference in New Issue
Block a user