Skip to content

Commit

Permalink
Switch to io from ioutil to read body
Browse files Browse the repository at this point in the history
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) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Mar 2, 2022
1 parent 5cd3573 commit 464715b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -213,7 +213,7 @@ package function
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
)
Expand All @@ -225,7 +225,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

// read request payload
reqBody, err := ioutil.ReadAll(r.Body)
reqBody, err := io.ReadAll(r.Body)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -268,7 +268,7 @@ package function
import (
"database/sql"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
_ "github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -298,7 +298,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

// read request payload
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)

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

import (
"io/ioutil"
"io"
"net/http"
)

func Echo(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
b, _ := io.ReadAll(r.Body)
w.Write(b)
}
}
Expand Down
2 changes: 1 addition & 1 deletion template/golang-http/Dockerfile
Expand Up @@ -17,7 +17,7 @@ RUN mkdir -p /go/src/handler
WORKDIR /go/src/handler
COPY . .

ARG GO111MODULE="off"
ARG GO111MODULE="on"
ARG GOPROXY=""
ARG GOFLAGS=""
ARG DEBUG=0
Expand Down
4 changes: 2 additions & 2 deletions template/golang-http/main.go
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -83,7 +83,7 @@ func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
if r.Body != nil {
defer r.Body.Close()

bodyBytes, bodyErr := ioutil.ReadAll(r.Body)
bodyBytes, bodyErr := io.ReadAll(r.Body)

if bodyErr != nil {
log.Printf("Error reading body from request.")
Expand Down
4 changes: 2 additions & 2 deletions template/golang-middleware/function/handler.go
Expand Up @@ -2,7 +2,7 @@ package function

import (
"fmt"
"io/ioutil"
"io"
"net/http"
)

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

body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)

input = body
}
Expand Down

0 comments on commit 464715b

Please sign in to comment.