Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenge Bravo - Just a guy trying to have some fun #318

Open
wants to merge 99 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
0bf36d9
feat: initial setup
wesleyfebarretos Aug 30, 2024
45aad2f
feat: config and db connection
wesleyfebarretos Aug 31, 2024
6627c66
feat: currency entity and domain exceptions
wesleyfebarretos Aug 31, 2024
28e29bd
feat: user entity, enum and use cases
wesleyfebarretos Aug 31, 2024
ed9d6a9
feat: put connection pool max connections in config
wesleyfebarretos Aug 31, 2024
100bfff
feat: sqlc config
wesleyfebarretos Aug 31, 2024
6faab6a
feat: abstract db connection for can rollback transactions in future …
wesleyfebarretos Aug 31, 2024
119f213
fix: user entity metadata
wesleyfebarretos Aug 31, 2024
e543122
feat: add db driver to config
wesleyfebarretos Aug 31, 2024
e3e0cc6
feat: user and currency tables
wesleyfebarretos Aug 31, 2024
fb07d31
feat: improvements in makefile
wesleyfebarretos Aug 31, 2024
65083c7
fix: currency tables and entity
wesleyfebarretos Aug 31, 2024
1941a7a
feat: first user queries and override int32 sqlc default type to int
wesleyfebarretos Aug 31, 2024
575b8e9
feat: override pg_catalog.timestamp to time.Time
wesleyfebarretos Aug 31, 2024
dec46c3
feat: user repository
wesleyfebarretos Aug 31, 2024
7e54a98
feat: create and update user handler
wesleyfebarretos Aug 31, 2024
ae39697
fix: some fix in queries, handlers, and repositories
wesleyfebarretos Aug 31, 2024
87777cc
feat: start-services makef ile
wesleyfebarretos Aug 31, 2024
76e75c1
chore: remove unused config in docker compose
wesleyfebarretos Sep 3, 2024
905211e
feat: add transactional context to repositories
wesleyfebarretos Sep 3, 2024
67c384d
feat: exception handler
wesleyfebarretos Sep 3, 2024
659180e
feat: add utils pkg
wesleyfebarretos Sep 3, 2024
d5b65b8
feat: hashing in user password on create
wesleyfebarretos Sep 3, 2024
d46943c
feat: jwt service and middleware
wesleyfebarretos Sep 3, 2024
6d53b7d
WIP: get user by email
wesleyfebarretos Sep 3, 2024
cbd2530
feat: finish jwt middleware
wesleyfebarretos Sep 4, 2024
e9e7b55
fix: exception handler to abort request on panic
wesleyfebarretos Sep 4, 2024
20d544d
feat: sign in use case and fix in jwt
wesleyfebarretos Sep 4, 2024
2e86c06
feat: error response improvement
wesleyfebarretos Sep 4, 2024
232b3c2
feat: log middleware
wesleyfebarretos Sep 5, 2024
a839597
fix: interfaces
wesleyfebarretos Sep 5, 2024
0b75d70
fix: logger order params
wesleyfebarretos Sep 5, 2024
e405023
feat: currency queries
wesleyfebarretos Sep 5, 2024
4ccc95e
feat: override pg_catalog.numeric type to float64
wesleyfebarretos Sep 5, 2024
bbfeeb4
feat: currency repository
wesleyfebarretos Sep 5, 2024
3101a9b
feat: override some default values from sqlc
wesleyfebarretos Sep 5, 2024
bfc93e4
feat: currency use cases
wesleyfebarretos Sep 5, 2024
fa51708
fix: remove unnecessary column
wesleyfebarretos Sep 5, 2024
f4e0105
feat: create currency
wesleyfebarretos Sep 5, 2024
9164ce7
feat: currency handlers
wesleyfebarretos Sep 6, 2024
491dd58
feat: currency managment crud
wesleyfebarretos Sep 6, 2024
2011347
fix: get by code use case
wesleyfebarretos Sep 6, 2024
f82943a
feat: currency conversion service
wesleyfebarretos Sep 6, 2024
97cfd59
feat: add cron dependency
wesleyfebarretos Sep 6, 2024
8f829a8
feat: redis package
wesleyfebarretos Sep 10, 2024
add61f4
feat: add go mod
wesleyfebarretos Sep 10, 2024
383c210
refactor: go workspace
wesleyfebarretos Sep 10, 2024
bfb68ca
fix: makefile .env include
wesleyfebarretos Sep 10, 2024
323e957
fix: root path on init app
wesleyfebarretos Sep 11, 2024
f9d4e19
fix: docker compose context
wesleyfebarretos Sep 11, 2024
ffd77aa
chore: air log default file
wesleyfebarretos Sep 11, 2024
ed905c1
feat: add dependencies on pkg
wesleyfebarretos Sep 11, 2024
917d710
feat: scheduler
wesleyfebarretos Sep 11, 2024
e6ac95a
feat: job management by http requesitions
wesleyfebarretos Sep 12, 2024
d8a2335
build: app-job
wesleyfebarretos Sep 13, 2024
b5d3814
feat: dynamic response path to currencies search api's
wesleyfebarretos Sep 13, 2024
862e8e6
feat: default currencies updater based in usd value service
wesleyfebarretos Sep 13, 2024
0cef986
feat: job api currency updater task
wesleyfebarretos Sep 14, 2024
8818259
feat: add endpoint to run task immediately
wesleyfebarretos Sep 18, 2024
f500357
chore: remove unused package
wesleyfebarretos Sep 18, 2024
d8cd389
feat: add cache to currency rate map
wesleyfebarretos Sep 18, 2024
8988873
feat: update cache when create or update a currency
wesleyfebarretos Sep 18, 2024
23dd1bb
feat: automatic migrations on application start
wesleyfebarretos Sep 18, 2024
131d726
feat: abstract db get connection to rollback all queries maded to wor…
wesleyfebarretos Sep 18, 2024
7ce8582
feat: maximize default pool connection
wesleyfebarretos Sep 18, 2024
3926157
refactor: readability
wesleyfebarretos Sep 18, 2024
bf88124
feat: abstration in test tx enviroment
wesleyfebarretos Sep 18, 2024
b030a52
feat: test container pg setup
wesleyfebarretos Sep 24, 2024
619d9b2
feat: init setup to integration test
wesleyfebarretos Sep 24, 2024
46d7731
feat: keep develop integration test setup
wesleyfebarretos Sep 24, 2024
1aa942c
fix
wesleyfebarretos Sep 24, 2024
9014486
fix
wesleyfebarretos Sep 24, 2024
fbe6181
feat: abstraction in repositories to run in integration tests
wesleyfebarretos Sep 25, 2024
44108a7
feat: update tx without override repository connection pointer
wesleyfebarretos Sep 25, 2024
b81ecbe
chore: org
wesleyfebarretos Sep 25, 2024
a2a197d
feat: currency it test
wesleyfebarretos Sep 25, 2024
43a5545
fix
wesleyfebarretos Sep 25, 2024
93384f0
fix: integration tests
wesleyfebarretos Sep 25, 2024
d68c0fa
fix
wesleyfebarretos Sep 25, 2024
ca1f57f
feat: update user handler test
wesleyfebarretos Sep 25, 2024
14dc898
feat: update currency handler test
wesleyfebarretos Sep 25, 2024
56cd749
feat: delete currency handler test
wesleyfebarretos Sep 26, 2024
24ac616
feat: find currency by id handler test
wesleyfebarretos Sep 26, 2024
5a82935
feat: find all currency handler test
wesleyfebarretos Sep 26, 2024
f83c388
feat: find currency by code handler test
wesleyfebarretos Sep 26, 2024
5c252c0
feat: currency conversion handler test
wesleyfebarretos Sep 26, 2024
713feeb
docs: swagger implementation & create user
wesleyfebarretos Sep 26, 2024
e89c737
docs: currency conversion handler
wesleyfebarretos Sep 26, 2024
7795571
docs: delete currency handler
wesleyfebarretos Sep 26, 2024
3333c8d
docs: find all currencies handler
wesleyfebarretos Sep 26, 2024
c9cb04d
docs: find currency handlers
wesleyfebarretos Sep 26, 2024
6b317f9
docs: handlers & use cases
wesleyfebarretos Sep 26, 2024
85f8b52
fix
wesleyfebarretos Sep 26, 2024
f323658
chore: readme improvements
wesleyfebarretos Sep 26, 2024
f4617d5
chore: readme improvements
wesleyfebarretos Sep 26, 2024
9935ccb
feat: init swagger in jobs
wesleyfebarretos Sep 26, 2024
21fcda1
docs: job api swagger
wesleyfebarretos Sep 26, 2024
20b8e2f
docs: jobs api swagger
wesleyfebarretos Sep 26, 2024
f70c9a0
chore: write pull_request.txt
wesleyfebarretos Sep 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .editorconfig

This file was deleted.

5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tmp
.env
NOTES.md
*/logs
*logs
57 changes: 57 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
include ./app/.env

DATABASE=${DB_DRIVER}://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME}?sslmode=disable

start-services:
@docker compose up -d

restart-services:
@docker compose down -d
@docker compose up -d

restart-db:
@docker compose down postgres --volumes
@docker compose up postgres -d

rebuild:
@docker compose down --volumes
@docker compose build
@docker compose up -d

# Migrations
create-table:
@migrate create -ext=sql -dir=./app/internal/migration -seq $(shell echo $(filter-out $@,$(MAKECMDGOALS))_table)

create-seed:
@migrate create -ext sql -dir ./app/internal/migration -seq $(shell echo $(filter-out $@,$(MAKECMDGOALS))_seeder)

create-view:
@migrate create -ext sql -dir ./app/internal/migration -seq $(shell echo $(filter-out $@,$(MAKECMDGOALS))_view)

create-schema:
@migrate create -ext sql -dir ./app/internal/migration -seq $(shell echo $(filter-out $@,$(MAKECMDGOALS))_schema)

migrations-up:
@migrate -path ./app/internal/migration -database $(DATABASE) -verbose up

migrations-down:
@migrate -path ./app/internal/migration -database $(DATABASE) -verbose down -all

# Tests
app-integration-test:
@go test ./app/test/integration/...

app-integration-test-verbose:
@go test ./app/test/integration/... -v

# Swagger
generate-app-swagger-docs:
@rm -rf ./app/docs
@swag init -g ./app/cmd/app/main.go -o ./app/docs --exclude ./job

generate-job-swagger-docs:
@rm -rf ./job/docs
@swag init -g ./job/cmd/main.go -o ./job/docs --exclude ./app

format-swagger-configs:
@swag fmt
104 changes: 35 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,48 @@
# <img src="https://avatars1.githubusercontent.com/u/7063040?v=4&s=200.jpg" alt="Hurb" width="24" /> Bravo Challenge
# Bravo Challenge

[[English](README.md) | [Portuguese](README.pt.md)]
## Summary

Build an API, which responds to JSON, for currency conversion. It must have a backing currency (USD) and make conversions between different currencies with **real and live values**.
The application consists of:
- Postgres
- Redis
- Currency API
- Manages all operations related to authentication and user and currency functionalities
- Jobs API
- Shares the same authentication, and you must be logged in to use the features.
- By default, the job that updates the values ​​of coins in the database and cache runs every 12 hours, if you want to run it immediately after starting the application, you can do this via the **Run Task** endpoint available in the swagger that is cited below.

The API must convert between the following currencies:
## Docs
To use endpoints that require authentication, you must use the **Auth Sign In** endpoint to take the returned token and insert it into Swagger's global authorization.

- USD
- BRL
- EUR
- BTC
- ETH

Other coins could be added as usage.
After running the application, we have a swagger available at the addresses below:

Ex: USD to BRL, USD to BTC, ETH to BRL, etc...
**Currency API**
```http
http://localhost:8080/v1/swagger/index.html
```

The request must receive as parameters: The source currency, the amount to be converted and the final currency.
**Jobs API**
```http
http://localhost:8081/v1/swagger/index.html
```

Ex: `?from=BTC&to=EUR&amount=123.45`

Also build an endpoint to add and remove API supported currencies using HTTP verbs.
## Running
```bash
git clone https://github.com/wesleyfebarretos/challenge-bravo.git
cd challenge-bravo && make start-services
```

The API must support conversion between FIAT, crypto and fictitious. Example: BRL->HURB, HURB->ETH
## Testing
In the project **cwd** execute the command below:

"Currency is the means by which monetary transactions are effected." (Wikipedia, 2021).
```bash
make app-integration-test
```

Therefore, it is possible to imagine that new coins come into existence or cease to exist, it is also possible to imagine fictitious coins such as Dungeons & Dragons coins being used in these transactions, such as how much is a Gold Piece (Dungeons & Dragons) in Real or how much is the GTA$1 in Real.
If you want test logs in verbose mode to see test-containers setup and logs execute this one:

Let's consider the PSN quote where GTA$1,250,000.00 cost R$83.50 we clearly have a relationship between the currencies, so it is possible to create a quote. (Playstation Store, 2021).

Ref:
Wikipedia [Institutional Website]. Available at: <https://pt.wikipedia.org/wiki/Currency>. Accessed on: 28 April 2021.
Playstation Store [Virtual Store]. Available at: <https://store.playstation.com/pt-br/product/UP1004-CUSA00419_00-GTAVCASHPACK000D>. Accessed on: 28 April 2021.

You can use any programming language for the challenge. Below is the list of languages ​​that we here at Hurb have more affinity:

- JavaScript (NodeJS)
- Python
- Go
- Ruby
- C++
- PHP

## Requirements

- Fork this challenge and create your project (or workspace) using your version of that repository, as soon as you finish the challenge, submit a _pull request_.
- If you have any reason not to submit a _pull request_, create a private repository on Github, do every challenge on the **main** branch and don't forget to fill in the `pull-request.txt` file. As soon as you finish your development, add the user `automator-hurb` to your repository as a contributor and make it available for at least 30 days. **Do not add the `automator-hurb` until development is complete.**
- If you have any problem creating the private repository, at the end of the challenge fill in the file called `pull-request.txt`, compress the project folder - including the `.git` folder - and send it to us by email.
- The code needs to run on macOS or Ubuntu (preferably as a Docker container)
- To run your code, all you need to do is run the following commands:
- git clone \$your-fork
- cd \$your-fork
- command to install dependencies
- command to run the application
- The API can be written with or without the help of _frameworks_
- If you choose to use a _framework_ that results in _boilerplate code_, mark in the README which piece of code was written by you. The more code you make, the more content we will have to rate.
- The API needs to support a volume of 1000 requests per second in a stress test.
- The API needs to include real and current quotes through integration with public currency quote APIs

## Evaluation criteria

- **Organization of code**: Separation of modules, view and model, back-end and front-end
- **Clarity**: Does the README explain briefly what the problem is and how can I run the application?
- **Assertiveness**: Is the application doing what is expected? If something is missing, does the README explain why?
- **Code readability** (including comments)
- **Security**: Are there any clear vulnerabilities?
- **Test coverage** (We don't expect full coverage)
- **History of commits** (structure and quality)
- **UX**: Is the interface user-friendly and self-explanatory? Is the API intuitive?
- **Technical choices**: Is the choice of libraries, database, architecture, etc. the best choice for the application?

## Doubts

Any questions you may have, check the [_issues_](https://github.com/HurbCom/challenge-bravo/issues) to see if someone hasn't already and if you can't find your answer, open one yourself. new issue!

Godspeed! ;)

<p align="center">
<img src="ca.jpg" alt="Challange accepted" />
</p>
```bash
make app-integration-test-verbose
```
2 changes: 1 addition & 1 deletion README.pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ Boa sorte e boa viagem! ;)

<p align="center">
<img src="ca.jpg" alt="Challange accepted" />
</p>
</p>
51 changes: 51 additions & 0 deletions app/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "./app/tmp"

[build]
args_bin = []
bin = "./app/tmp/main"
cmd = "go build -o ./app/tmp/main ./app/cmd/app/main.go"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "air.logs"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[proxy]
app_port = 0
enabled = false
proxy_port = 0

[screen]
clear_on_rebuild = false
keep_scroll = true
17 changes: 17 additions & 0 deletions app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Application
API_TOKEN=
APP_ENV=
PORT=

# Database
DB_USER=
DB_PASSWORD=
DB_PORT=
DB_NAME=
DB_HOST=

# Redis
REDIS_PASSWORD=
REDIS_HOST=
REDIS_PORT=
REDIS_USER=
8 changes: 8 additions & 0 deletions app/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DB_USER="root"
DB_PASSWORD="root"
DB_PORT="5432"
DB_NAME="challenge_bravo"
DB_DRIVER="postgres"
DB_HOST="localhost"

REDIS_HOST="localhost"
59 changes: 59 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Pin specific version for stability
# Use separate stage for building image
# Use debian for easier build utilities
FROM golang:1.23-bullseye AS build-base

WORKDIR /app

# Copy only files required to install dependencies (better layer caching)
COPY go.mod go.sum ./

# Use cache mount to speed up install of existing dependencies
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download

FROM build-base AS dev

# Install air for hot reload & delve for debugging
RUN go install github.com/air-verse/air@latest

COPY . .

CMD ["air", "-c", "./app/.air.toml"]

FROM build-base AS build-production

# Add non root user
RUN useradd -u 1001 nonroot

COPY . .

# Compile application during build rather than at runtime
# Add flags to statically link binary
RUN go build \
-ldflags="-linkmode external -extldflags -static" \
-tags netgo \
-o main ./cmd/app/main.go

# Use separate stage for deployable image
FROM scratch

# Set gin mode
ENV GIN_MODE=release

WORKDIR /

# Copy the passwd file
COPY --from=build-production /etc/passwd /etc/passwd

# Copy the app binary from the build stage
COPY --from=build-production /app/main main

# Use nonroot user
USER nonroot

# Indicate expected port
EXPOSE 8080

CMD ["/main"]
Loading