Skip to content

Commit

Permalink
move binary env to go
Browse files Browse the repository at this point in the history
  • Loading branch information
pfandzelter committed Jul 3, 2023
1 parent ba723d4 commit f7a6022
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 42 deletions.
12 changes: 10 additions & 2 deletions runtimes/binary/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
FROM python:3.11-alpine
FROM golang:1.20-alpine AS builder

WORKDIR /usr/src/build
COPY functionhandler.go .
RUN GO111MODULE=off CGO_ENABLED=0 go build -o handler.bin .

FROM alpine

EXPOSE 8000

# Create app directory
WORKDIR /usr/src/app

COPY --from=builder /usr/src/build/handler.bin .

COPY . .
RUN rm functionhandler.go
RUN mv fn/* .
RUN chmod +x fn.sh

CMD [ "python3", "functionhandler.py" ]
CMD [ "./handler.bin" ]
56 changes: 56 additions & 0 deletions runtimes/binary/functionhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"os/exec"
)

func main() {
port := ":8000"

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
if r.URL.Path == "/health" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "OK")
log.Println("reporting health: OK")
return
}
w.WriteHeader(http.StatusNotFound)
return

case http.MethodPost:
data, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
cmd := exec.Command("./fn.sh")
cmd.Stdin = bytes.NewReader(data)
output, err := cmd.CombinedOutput()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(output)
return
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
})

log.Printf("Server listening on port %s\n", port)
err := http.ListenAndServe(port, nil)
if err != nil {
log.Fatal(err)
}
}
39 changes: 0 additions & 39 deletions runtimes/binary/functionhandler.py

This file was deleted.

1 change: 0 additions & 1 deletion test/fns/echo-binary/fn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ INPUT=$(cat)

# echo the variable to stdout
echo -n "$INPUT"

0 comments on commit f7a6022

Please sign in to comment.