mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Addressing certain comments
What's new? - better error handling - still need to decode JSON from function because we need status code and body - prevent request body to be a problem by deferring its close - moving examples around: putting http and json samples into one folder
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for {
|
||||
res := http.Response{
|
||||
Proto: "HTTP/1.1",
|
||||
ProtoMajor: 1,
|
||||
ProtoMinor: 1,
|
||||
StatusCode: 200,
|
||||
Status: "OK",
|
||||
}
|
||||
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
req, err := http.ReadRequest(r)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err != nil {
|
||||
res.StatusCode = 500
|
||||
res.Status = http.StatusText(res.StatusCode)
|
||||
fmt.Fprintln(&buf, err)
|
||||
} else {
|
||||
l, _ := strconv.Atoi(req.Header.Get("Content-Length"))
|
||||
p := make([]byte, l)
|
||||
r.Read(p)
|
||||
fmt.Fprintf(&buf, "Hello %s\n", p)
|
||||
for k, vs := range req.Header {
|
||||
fmt.Fprintf(&buf, "ENV: %s %#v\n", k, vs)
|
||||
}
|
||||
}
|
||||
|
||||
res.Body = ioutil.NopCloser(&buf)
|
||||
res.ContentLength = int64(buf.Len())
|
||||
res.Write(os.Stdout)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
FROM jjanzic/docker-python3-opencv
|
||||
|
||||
RUN mkdir /code
|
||||
ADD . /code/
|
||||
WORKDIR /code
|
||||
RUN pip3 install -r requirements.txt
|
||||
|
||||
WORKDIR /code/
|
||||
ENTRYPOINT ["python3", "func.py"]
|
||||
@@ -1,42 +0,0 @@
|
||||
import asyncio
|
||||
import sys
|
||||
import uvloop
|
||||
|
||||
from hotfn.http import request
|
||||
from hotfn.http import response
|
||||
|
||||
|
||||
class MyProtocol(asyncio.Protocol):
|
||||
|
||||
def connection_made(self, transport):
|
||||
print('pipe opened', file=sys.stderr, flush=True)
|
||||
super(MyProtocol, self).connection_made(transport=transport)
|
||||
|
||||
def data_received(self, data):
|
||||
print('received: {!r}'.format(data), file=sys.stderr, flush=True)
|
||||
data = data.decode()
|
||||
req = request.RawRequest(data)
|
||||
(method, url, dict_params,
|
||||
headers, http_version, req_data) = req.parse_raw_request()
|
||||
|
||||
rs = response.RawResponse(
|
||||
http_version, 200, "OK",
|
||||
response_data=req_data)
|
||||
print(rs.dump(), file=sys.stdout, flush=True)
|
||||
super(MyProtocol, self).data_received(data)
|
||||
|
||||
def connection_lost(self, exc):
|
||||
print('pipe closed', file=sys.stderr, flush=True)
|
||||
super(MyProtocol, self).connection_lost(exc)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("/dev/stdin", "rb", buffering=0) as stdin:
|
||||
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
||||
loop = asyncio.get_event_loop()
|
||||
try:
|
||||
stdin_pipe_reader = loop.connect_read_pipe(MyProtocol, stdin)
|
||||
loop.run_until_complete(stdin_pipe_reader)
|
||||
loop.run_forever()
|
||||
finally:
|
||||
loop.close()
|
||||
@@ -1,7 +0,0 @@
|
||||
name: fnproject/hotfn-py
|
||||
version: 0.0.1
|
||||
runtime: docker
|
||||
type: sync
|
||||
memory: 521
|
||||
format: http
|
||||
path: /hotfn-py
|
||||
@@ -1,2 +0,0 @@
|
||||
hotfn
|
||||
uvloop
|
||||
Reference in New Issue
Block a user