Continue reading Deploy Grafana & Prometheus metrcs tracking system for your Go program
Tag: golang
Deploy Golang function on Openfaas
this post will show you how to deploy a Sendtweet golang function in a OpenFaas Plaform.
Note of GoRoutines
Goroutines explain
in Go, each concurrently executing activity called a groutine
when a program starts, it has a groutine that calls the Main function
, we call it _Main groutine_
.
Goroutines have no notion of identity that is accessible to the programmer
Echo frameworks context bind twice without losing data
Retrieve data from echo middleware, may cause you get code=400, message=EOF
error.
Context.Bind
only can bind once, after the request passed the middleware,the request data reader is running out, Context.Bind()
function read request body data from the socket buffer, once you took it out, it just gone
that’s why it returns EOF error.
solution
func main() {
echoServer := echo.New()
echoServer.POST("/api", webfunc1, middleware1)
echoServer.Start(":8080")
}
func middleware1(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// read origin body bytes
var bodyBytes []byte
if c.Request().Body != nil {
bodyBytes, _ = ioutil.ReadAll(c.Request().Body)
// write back to request body
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
// parse json data
reqData := struct {
ID string `json:"id"`
}{}
err := json.Unmarshal(bodyBytes, &reqData)
if err != nil {
return c.JSON(400, "error json.")
}
fmt.Println(reqData.ID)
}
return next(c)
}
}
func webfunc1(c echo.Context) error {
reqData := struct {
Data string `json:"data"`
}{}
c.Bind(&reqData)
fmt.Println(reqData.Data)
return c.HTML(200, "200 OK.")
}
Golang: Read from an io.ReadWriter without losing its content