-
Notifications
You must be signed in to change notification settings - Fork 6
/
carsdata.go
38 lines (29 loc) · 800 Bytes
/
carsdata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package handlers
import (
"bytes"
"encoding/gob"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/EngineerKamesh/igb/igweb/common"
"github.com/EngineerKamesh/igb/igweb/shared/models"
)
func CarsDataHandler(env *common.Env) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var cars []models.Car
var carsDataBuffer bytes.Buffer
dec := gob.NewDecoder(&carsDataBuffer)
body, err := ioutil.ReadAll(r.Body)
carsDataBuffer = *bytes.NewBuffer(body)
err = dec.Decode(&cars)
w.Header().Set("Content-Type", "text/plain")
if err != nil {
log.Println(err)
w.Write([]byte("Something went wrong, look into it"))
} else {
fmt.Printf("Cars Data: %#v\n", cars)
w.Write([]byte("Thanks, I got the slice of cars you sent me!"))
}
})
}