Skip to content

Commit f7a6022

Browse files
committed
move binary env to go
1 parent ba723d4 commit f7a6022

File tree

4 files changed

+66
-42
lines changed

4 files changed

+66
-42
lines changed

runtimes/binary/Dockerfile

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
FROM python:3.11-alpine
1+
FROM golang:1.20-alpine AS builder
22

3+
WORKDIR /usr/src/build
4+
COPY functionhandler.go .
5+
RUN GO111MODULE=off CGO_ENABLED=0 go build -o handler.bin .
6+
7+
FROM alpine
38

49
EXPOSE 8000
510

611
# Create app directory
712
WORKDIR /usr/src/app
813

14+
COPY --from=builder /usr/src/build/handler.bin .
15+
916
COPY . .
17+
RUN rm functionhandler.go
1018
RUN mv fn/* .
1119
RUN chmod +x fn.sh
1220

13-
CMD [ "python3", "functionhandler.py" ]
21+
CMD [ "./handler.bin" ]

runtimes/binary/functionhandler.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"log"
8+
"net/http"
9+
"os/exec"
10+
)
11+
12+
func main() {
13+
port := ":8000"
14+
15+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
16+
switch r.Method {
17+
case http.MethodGet:
18+
if r.URL.Path == "/health" {
19+
w.WriteHeader(http.StatusOK)
20+
fmt.Fprint(w, "OK")
21+
log.Println("reporting health: OK")
22+
return
23+
}
24+
w.WriteHeader(http.StatusNotFound)
25+
return
26+
27+
case http.MethodPost:
28+
data, err := io.ReadAll(r.Body)
29+
if err != nil {
30+
w.WriteHeader(http.StatusInternalServerError)
31+
fmt.Fprint(w, err)
32+
return
33+
}
34+
cmd := exec.Command("./fn.sh")
35+
cmd.Stdin = bytes.NewReader(data)
36+
output, err := cmd.CombinedOutput()
37+
if err != nil {
38+
w.WriteHeader(http.StatusInternalServerError)
39+
fmt.Fprint(w, err)
40+
return
41+
}
42+
w.WriteHeader(http.StatusOK)
43+
w.Write(output)
44+
return
45+
default:
46+
w.WriteHeader(http.StatusMethodNotAllowed)
47+
return
48+
}
49+
})
50+
51+
log.Printf("Server listening on port %s\n", port)
52+
err := http.ListenAndServe(port, nil)
53+
if err != nil {
54+
log.Fatal(err)
55+
}
56+
}

runtimes/binary/functionhandler.py

-39
This file was deleted.

test/fns/echo-binary/fn.sh

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ INPUT=$(cat)
55

66
# echo the variable to stdout
77
echo -n "$INPUT"
8-

0 commit comments

Comments
 (0)