Skip to content

Commit e3f5a6c

Browse files
committed
feat: add total CEPs count logging and endpoint response
1 parent 586bdc0 commit e3f5a6c

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Projeto open source, rápido, eficiente e fácil de usar.
1111
## Sumário
1212
- [Sumário](#sumário)
1313
- [Como rodar](#como-rodar)
14-
- [Build local](#build-local)
14+
- [Docker (base de CEPs pré fabricada inclusa)](#docker-base-de-ceps-pré-fabricada-inclusa)
15+
- [Desenvolvimento / sem docker](#desenvolvimento--sem-docker)
1516
- [Configurações via ENV](#configurações-via-env)
1617
- [Importação da base DNE](#importação-da-base-dne)
1718
- [Modo seed: importação da base DNE](#modo-seed-importação-da-base-dne)
@@ -31,6 +32,13 @@ Projeto open source, rápido, eficiente e fácil de usar.
3132

3233
## Como rodar
3334

35+
### Docker (base de CEPs pré fabricada inclusa)
36+
```bash
37+
docker run -p 8080:8080 brasilcep/api:latest
38+
```
39+
40+
### Desenvolvimento / sem docker
41+
3442
1. Clone o repositório:
3543
```sh
3644
git clone https://github.com/brasilcep/api.git
@@ -50,8 +58,6 @@ Projeto open source, rápido, eficiente e fácil de usar.
5058
make run
5159
```
5260

53-
## Build local
54-
5561
Para compilar o binário localmente:
5662
```sh
5763
make build

api/api.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ func (api *API) Listen() {
148148
api.logger.Info("Starting HTTP server", zap.Int("port", port))
149149
api.logger.Info(fmt.Sprintf("Listening on :%d", port))
150150

151+
count, _ := count()
152+
153+
api.logger.Info("Total CEPs in database loaded", zap.Int("total_ceps", count))
154+
151155
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", port)))
152156
}
153157

@@ -263,10 +267,21 @@ func (api *API) list(c echo.Context) error {
263267
}
264268

265269
func (api *API) count(c echo.Context) error {
266-
db := database.GetDB()
267270

268-
count := 0
271+
count, err := count()
272+
273+
if err != nil {
274+
return c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "Erro ao contar CEPs"})
275+
}
276+
277+
return c.JSON(http.StatusOK, map[string]interface{}{
278+
"total_ceps": count,
279+
})
280+
}
269281

282+
func count() (int, error) {
283+
var count int
284+
db := database.GetDB()
270285
err := db.View(func(txn *badger.Txn) error {
271286
opts := badger.DefaultIteratorOptions
272287
opts.PrefetchValues = false // Não precisa dos valores, só contar
@@ -279,14 +294,11 @@ func (api *API) count(c echo.Context) error {
279294
}
280295
return nil
281296
})
282-
283297
if err != nil {
284-
return c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "Erro ao contar CEPs"})
298+
return 0, err
285299
}
286300

287-
return c.JSON(http.StatusOK, map[string]interface{}{
288-
"total_ceps": count,
289-
})
301+
return count, nil
290302
}
291303

292304
func (api *API) stats(c echo.Context) error {

0 commit comments

Comments
 (0)