Skip to content

Commit

Permalink
feat: add debug support
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshadhin committed Dec 15, 2022
1 parent 49e25eb commit b31fb56
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 25 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ test-integration-pgsql: ## Run pgsql integration tests
clean: ## Cleans output directory
$(shell rm -rf $(BIN_OUT_DIR)/*)
$(shell rm -rf cmd/migrations)
$(shell rm -rf dist)
$(shell rm -rf ./*.db ./it/*.db coverage.txt _doc/docs.go _doc/swagger.json _doc/swagger.yaml)

dl-deps: ## Get dependencies
Expand Down
4 changes: 4 additions & 0 deletions app/location/delivery/http/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"ot-recorder/app/response"
"ot-recorder/app/validation"

"github.com/sirupsen/logrus"

"github.com/labstack/echo/v4"
)

Expand Down Expand Up @@ -46,8 +48,10 @@ func (u *LocationHandler) Ping(c echo.Context) error {
if ok, err := validation.Validate(&pingReq); !ok {
valErrors, valErr := validation.FormatErrors(err)
if valErr != nil {
logrus.Error(valErr)
return c.JSON(response.RespondError(response.ErrBadRequest, valErr))
}
logrus.Error(valErrors)

return c.JSON(response.RespondValidationError(response.ErrBadRequest, valErrors))
}
Expand Down
3 changes: 2 additions & 1 deletion app/location/delivery/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ type PingRequest struct {

func mapLocationRequestToModel(req *PingRequest, headers *http.Header) *model.Location {
createdAt := parseEpochTimeToLocal(req.Tst, config.Get().App.TimeZone)
createdAtC := model.CustomDateTime(createdAt.Format("2006-01-02 15:04:05"))

return &model.Location{
Username: headers.Get("x-limit-u"),
Device: headers.Get("x-limit-d"),
CreatedAt: createdAt.Format("2006-01-02 15:04:05"),
CreatedAt: createdAtC,
Acc: req.Acc,
Alt: req.Alt,
Batt: req.Batt,
Expand Down
2 changes: 1 addition & 1 deletion app/location/repository/mysql/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestCreateLocation(t *testing.T) {
l := &model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: nowTime,
CreatedAt: model.CustomDateTime(nowTime),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down
2 changes: 1 addition & 1 deletion app/location/repository/pgsql/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestCreateLocation(t *testing.T) {
l := &model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: nowTime,
CreatedAt: model.CustomDateTime(nowTime),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down
2 changes: 1 addition & 1 deletion app/location/repository/sqlite/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestCreateLocation(t *testing.T) {
l := &model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: nowTime,
CreatedAt: model.CustomDateTime(nowTime),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down
2 changes: 1 addition & 1 deletion app/location/usecase/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (u *locationUsecase) LastLocation(
location = &model.LocationDetails{
Username: l.Username,
Device: l.Device,
DateTime: l.CreatedAt,
DateTime: l.CreatedAt.String(),
Accuracy: l.Acc,
Altitude: l.Alt,
BatteryLevel: fmt.Sprintf("%d%s", l.Batt, "%"),
Expand Down
4 changes: 2 additions & 2 deletions app/location/usecase/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestCreateLocation(t *testing.T) {
mockLocation := model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: nowTime,
CreatedAt: model.CustomDateTime(nowTime),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestGetUserLastLocation(t *testing.T) {
mockLocation := model.Location{
Username: "dev",
Device: "phoneAndroid",
CreatedAt: nowTime,
CreatedAt: model.CustomDateTime(nowTime),
Acc: 13,
Alt: -42,
Batt: 40,
Expand Down
60 changes: 42 additions & 18 deletions app/model/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,51 @@ package model

import (
"context"
"database/sql/driver"
"time"
)

type CustomDateTime string

func (cdt CustomDateTime) Value() (driver.Value, error) {
return string(cdt), nil
}

func (cdt *CustomDateTime) Scan(value interface{}) error {
if t, ok := value.(time.Time); ok {
*cdt = CustomDateTime(t.Format("2006-01-02 15:04:05"))
} else if t, ok := value.(string); ok {
*cdt = CustomDateTime(t)
} else if t, ok := value.([]uint8); ok {
*cdt = CustomDateTime(t)
}

return nil
}

func (cdt CustomDateTime) String() string {
return string(cdt)
}

type Location struct {
ID int64 `json:"id"`
Username string `json:"username"`
Device string `json:"device"`
CreatedAt string `json:"created_at"`
Acc int8 `json:"acc"`
Alt int8 `json:"alt"`
Batt int8 `json:"batt"`
Bs int8 `json:"bs"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
M int8 `json:"m"`
T string `json:"t"`
Tid string `json:"tid"`
Vac int8 `json:"vac"`
Vel int8 `json:"vel"`
Bssid string `json:"bssid"`
Ssid string `json:"ssid"`
IP string `json:"ip"`
ID int64 `json:"id"`
Username string `json:"username"`
Device string `json:"device"`
CreatedAt CustomDateTime `json:"created_at"`
Acc int8 `json:"acc"`
Alt int8 `json:"alt"`
Batt int8 `json:"batt"`
Bs int8 `json:"bs"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
M int8 `json:"m"`
T string `json:"t"`
Tid string `json:"tid"`
Vac int8 `json:"vac"`
Vel int8 `json:"vel"`
Bssid string `json:"bssid"`
Ssid string `json:"ssid"`
IP string `json:"ip"`
}

type LocationDetails struct {
Expand Down
1 change: 1 addition & 0 deletions infrastructure/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type AppConfig struct {
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
ContextTimeout time.Duration `mapstructure:"context_timeout"`
Port int `mapstructure:"port"`
Debug bool `mapstructure:"debug"`
}

// DatabaseConfig DB specific config
Expand Down
10 changes: 10 additions & 0 deletions infrastructure/middlewares/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package middlewares
import (
"ot-recorder/infrastructure/config"

"github.com/sirupsen/logrus"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
Expand All @@ -20,5 +22,13 @@ func Attach(e *echo.Echo) error {
e.Use(middleware.BodyLimit(cfg.RequestBodyLimit))
e.Pre(middleware.RemoveTrailingSlash())

// only for debug usage
if cfg.Debug {
e.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debug(string(reqBody))
}))
}

return nil
}
1 change: 1 addition & 0 deletions it/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ app:
context_timeout: 2s
data_path: ./
time_zone: 'Asia/Dhaka'
debug: false

database:
type: sqlite
Expand Down
1 change: 1 addition & 0 deletions it/mysql/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ app:
context_timeout: 2s
data_path: ./
time_zone: 'Asia/Dhaka'
debug: false

database:
type: mysql
Expand Down
1 change: 1 addition & 0 deletions it/pgsql/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ app:
context_timeout: 2s
data_path: ./
time_zone: 'Asia/Dhaka'
debug: false

database:
type: postgres
Expand Down

0 comments on commit b31fb56

Please sign in to comment.