Skip to content

Commit 464715b

Browse files
committed
Switch to io from ioutil to read body
Tested with local docker build, also enables Go modules as a default for golang-http, which was missing but meant to be enabled via #63 Fixes #67 Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 5cd3573 commit 464715b

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ package function
213213
import (
214214
"encoding/json"
215215
"fmt"
216-
"io/ioutil"
216+
"io"
217217
"net/http"
218218
"os"
219219
)
@@ -225,7 +225,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
225225
defer r.Body.Close()
226226

227227
// read request payload
228-
reqBody, err := ioutil.ReadAll(r.Body)
228+
reqBody, err := io.ReadAll(r.Body)
229229

230230
if err != nil {
231231
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -268,7 +268,7 @@ package function
268268
import (
269269
"database/sql"
270270
"fmt"
271-
"io/ioutil"
271+
"io"
272272
"net/http"
273273
"strings"
274274
_ "github.com/go-sql-driver/mysql"
@@ -298,7 +298,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
298298
defer r.Body.Close()
299299

300300
// read request payload
301-
body, err := ioutil.ReadAll(r.Body)
301+
body, err := io.ReadAll(r.Body)
302302

303303
if err != nil {
304304
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -400,14 +400,14 @@ Imagine you have a package which you want to store outside of the `handler.go` f
400400
package handlers
401401

402402
import (
403-
"io/ioutil"
403+
"io"
404404
"net/http"
405405
)
406406

407407
func Echo(w http.ResponseWriter, r *http.Request) {
408408
if r.Body != nil {
409409
defer r.Body.Close()
410-
b, _ := ioutil.ReadAll(r.Body)
410+
b, _ := io.ReadAll(r.Body)
411411
w.Write(b)
412412
}
413413
}

template/golang-http/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ RUN mkdir -p /go/src/handler
1717
WORKDIR /go/src/handler
1818
COPY . .
1919

20-
ARG GO111MODULE="off"
20+
ARG GO111MODULE="on"
2121
ARG GOPROXY=""
2222
ARG GOFLAGS=""
2323
ARG DEBUG=0

template/golang-http/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"log"
88
"net/http"
99
"os"
@@ -83,7 +83,7 @@ func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
8383
if r.Body != nil {
8484
defer r.Body.Close()
8585

86-
bodyBytes, bodyErr := ioutil.ReadAll(r.Body)
86+
bodyBytes, bodyErr := io.ReadAll(r.Body)
8787

8888
if bodyErr != nil {
8989
log.Printf("Error reading body from request.")

template/golang-middleware/function/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package function
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
)
88

@@ -12,7 +12,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
1212
if r.Body != nil {
1313
defer r.Body.Close()
1414

15-
body, _ := ioutil.ReadAll(r.Body)
15+
body, _ := io.ReadAll(r.Body)
1616

1717
input = body
1818
}

0 commit comments

Comments
 (0)