github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/examples/hotfunctions/http/func.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"os"
    10  	"strconv"
    11  )
    12  
    13  func main() {
    14  	for {
    15  		res := http.Response{
    16  			Proto:      "HTTP/1.1",
    17  			ProtoMajor: 1,
    18  			ProtoMinor: 1,
    19  			StatusCode: 200,
    20  			Status:     "OK",
    21  		}
    22  
    23  		r := bufio.NewReader(os.Stdin)
    24  		req, err := http.ReadRequest(r)
    25  
    26  		var buf bytes.Buffer
    27  		if err != nil {
    28  			res.StatusCode = 500
    29  			res.Status = http.StatusText(res.StatusCode)
    30  			fmt.Fprintln(&buf, err)
    31  		} else {
    32  			l, _ := strconv.Atoi(req.Header.Get("Content-Length"))
    33  			p := make([]byte, l)
    34  			r.Read(p)
    35  			fmt.Fprintf(&buf, "Hello %s\n", p)
    36  			for k, vs := range req.Header {
    37  				fmt.Fprintf(&buf, "ENV: %s %#v\n", k, vs)
    38  			}
    39  		}
    40  
    41  		res.Body = ioutil.NopCloser(&buf)
    42  		res.ContentLength = int64(buf.Len())
    43  		res.Write(os.Stdout)
    44  	}
    45  }