Skip to content
This repository has been archived by the owner. It is now read-only.

Commit 569fb6f

Browse files
committedJan 20, 2022
feat: common db driver
1 parent 96b6417 commit 569fb6f

File tree

9 files changed

+24
-39
lines changed

9 files changed

+24
-39
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tracker app
22

3-
This app is intended to track user statistics of valid/invalid API calls.
3+
This app is intended to track users statistics of valid/invalid API calls.
44

55
## Install
66

@@ -12,7 +12,7 @@ API build on top of the Go, Echo, sqlx and Postgres. All infra tasks (start API,
1212
cp env.example.docker .env
1313
```
1414

15-
2. Start database:
15+
2.Start database (on Linux, you may need to use sudo for the following commands):
1616

1717
```bash
1818
docker-compose --env-file .env up -d postgres

‎cmd/migrate/main.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ import (
1313
)
1414

1515
const databaseMigrationPath = "file://migrations/"
16-
const databaseDriverName = "postgres"
16+
const databaseDriverName = "pgx"
1717

1818
func main() {
1919
cfg := config.New()
20-
dsn := fmt.Sprintf("%s://%s:%s/%s?user=%s&password=%s&sslmode=disable", databaseDriverName, cfg.Database.Host, cfg.Database.Port, cfg.Database.Name, cfg.Database.User, cfg.Database.Pass)
20+
dsn := fmt.Sprintf(
21+
"host=%s port=%s user=%s dbname=%s sslmode=disable password=%s",
22+
cfg.Database.Host,
23+
cfg.Database.Port,
24+
cfg.Database.User,
25+
cfg.Database.Name,
26+
cfg.Database.Pass,
27+
)
2128
db, err := sql.Open(databaseDriverName, dsn)
2229
if err != nil {
2330
log.Fatalf("Error opening database: %v", err)

‎cmd/server/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// @title Tracker
1212
// @version 0.1.0
13-
// @description Track user statistics of valid/invalid API calls
13+
// @description Track users statistics of valid/invalid API calls
1414
// @host localhost:6000
1515
// @BasePath /
1616
func main() {

‎examples/collector.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ curl --request POST \
5959
- Blocked IP so the request will be counted as invalid
6060

6161
```bash
62-
curl --request GET \
63-
--url content-type:%20application/json \
64-
--header 'user-agent: vscode-restclient' \
62+
curl --request POST \
63+
--url http://localhost:6000/api/v1/collect/ \
64+
--header 'content-type: application/json' \
6565
--data '{"customerID": 1,"tagID": 2,"userID": "aaaaaaaa-bbbb-cccc-1111-222222222222","remoteIP": "213.070.64.33","timestamp": 1500000000}'
6666
```
6767

‎examples/stats.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ curl --request GET \
99
--url http://localhost:6000/api/v1/stats/
1010
```
1111

12-
- Filter stats for the customer
12+
- Filter stats for a given customer
1313

1414
```bash
1515
curl --request GET \

‎internal/health/repository/repository.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package repository
22

33
import "github.com/jmoiron/sqlx"
44

5-
type repository struct {
5+
type healthRepo struct {
66
db *sqlx.DB
77
}
88

9-
func NewHealthRepository(db *sqlx.DB) *repository {
10-
return &repository{db: db}
9+
func NewHealthRepository(db *sqlx.DB) *healthRepo {
10+
return &healthRepo{db: db}
1111
}
1212

13-
func (r *repository) Readiness() error {
13+
func (r *healthRepo) Readiness() error {
1414
return r.db.Ping()
1515
}

‎internal/stats/handler/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ func NewStatsHander(logger *zap.SugaredLogger, statsUC stats.UseCase) stats.Hand
1717
return &statsHandler{logger: logger, statsUC: statsUC}
1818
}
1919

20-
func (s *statsHandler) GetStats() echo.HandlerFunc {
20+
func (h *statsHandler) GetStats() echo.HandlerFunc {
2121
return func(c echo.Context) error {
2222
filter := &stats.Filter{}
2323
if err := filter.Bind(c); err != nil {
2424
return c.JSON(response.Error(httperrors.NewBadRequestError(err)))
2525
}
2626

2727
ctx := c.Request().Context()
28-
stats, err := s.statsUC.GetStats(ctx, filter)
28+
stats, err := h.statsUC.GetStats(ctx, filter)
2929
if err != nil {
3030
return c.JSON(response.Error(err))
3131
}

‎internal/stats/handler/register.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package handler
22

33
import "github.com/labstack/echo/v4"
44

5-
func (s *statsHandler) RegisterHTTPEndPoints(version *echo.Group) {
5+
func (h *statsHandler) RegisterHTTPEndPoints(version *echo.Group) {
66
group := version.Group("/stats")
7-
group.GET("/", s.GetStats())
7+
group.GET("/", h.GetStats())
88
}

‎internal/stats/handler/utils.go

-22
This file was deleted.

0 commit comments

Comments
 (0)