-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (40 loc) · 1.13 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"flag"
"net/http"
"strconv"
. "github.com/cciuenf/rinha/internal"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
port := flag.String("port", ":4000", "port that server will listen")
e := echo.New()
// middlewares
// e.Use(middleware.RequestID())
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))
e.POST("/clientes/:id/transacoes", handleTransaction)
e.Logger.Fatal(e.Start(*port))
}
func handleTransaction(c echo.Context) error {
param := c.Param("id")
var req TransactionRequest
err := (&echo.DefaultBinder{}).BindBody(c, &req)
if err != nil {
return c.JSON(http.StatusInternalServerError, "bind req")
}
customerID, err := strconv.Atoi(param)
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, "atoi")
}
var response TransactionResponse
cc, err := MakeTransaction(customerID, req)
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err.Error())
}
response.Saldo = cc.Balance
response.Limite = cc.MaxLimit
return c.JSON(http.StatusOK, response)
}