This project consists of two Go programs (client.go
and server.go
) that demonstrate how to consume an external API, save data in a SQLite database, and manipulate files using standard Go packages and external packages.
- client/client.go: A client that makes an HTTP request to the server (
server/server.go
) to request the USD to BRL exchange rate and saves the received value in a text file. - server/server.go: A server that consumes an external API to obtain the USD to BRL exchange rate, returns the result to the client in JSON format, and logs the exchange rate in a SQLite database.
- Go 1.18 or higher
- SQLite3
-
Initialize Go Modules:
go mod init github.com/pansani/client-server-api
-
Install Dependencies:
go get github.com/mattn/go-sqlite3
-
Structure: Ensure your project structure looks like this:
client-server-api-go/ ├── client/ │ └── client.go ├── server/ │ └── server.go ├── cotacao.txt ├── cotacoes.db ├── go.mod └── go.sum
- Open a terminal and navigate to the
server
directory. - Run the server:
go run server.go
- The server will start and listen on port 8080.
- Open another terminal and navigate to the
client
directory. - Run the client:
go run client.go
- The client will make a request to the server, receive the exchange rate, and save it in the
cotacao.txt
file.
-
Server:
- Fetches the USD to BRL exchange rate from the API
https://economia.awesomeapi.com.br/json/last/USD-BRL
with a timeout of 200ms. - Saves the fetched exchange rate in a SQLite database with a timeout of 10ms.
- Provides an endpoint
/cotacao
that returns the current exchange rate in JSON format.
- Fetches the USD to BRL exchange rate from the API
-
Client:
- Makes a request to the server's
/cotacao
endpoint with a timeout of 300ms. - Saves the received exchange rate in a file named
cotacao.txt
in the format:Dólar: {value}
.
- Makes a request to the server's
- The server logs errors if the request to the external API or the database operation exceeds the timeout.
- The client logs errors if the request to the server exceeds the timeout or if there is an error saving to the file.
- cotacao.txt:
Dólar: 5.42