Repository for GoExpertPostGrad-FastesAPI-challenge and solution.
In this challenge, you will use what we have learned about Multithreading and APIs to fetch the fastest result between two distinct APIs. The requests will be made simultaneously to the following APIs:
https://brasilapi.com.br/api/cep/v1/{cep}http://viacep.com.br/ws/{cep}/json/
- Accept the response from the API that delivers the fastest result and discard the slower one.
- The result should be displayed on the command line with the address data, as well as which API sent it.
- Limit the response time to 1 second. Otherwise, a timeout error should be displayed.
- Go (Golang)
-
Clone this repository.
-
Navigate to the project directory.
-
Run
go mod tidyto ensure all dependencies are properly installed. -
Start the local server by running
go run ./cmd/api/main.go. This will start the server on port8080. -
Making a Request:
- To request address information using a CEP, use a browser,
curl, or any HTTP client, pointing tohttp://localhost:8080/api/cep/{CEP_HERE}, replacing{CEP_HERE}with the desired CEP.
Example using
curl:curl http://localhost:8080/api/cep/01001000
- To request address information using a CEP, use a browser,
-
The result will be displayed in the terminal, showing the address data and which API sent it.
-
The field
sourcewill indicate which API sent the response.
To access the Swagger-generated API documentation, navigate to http://localhost:8080/swagger/ after starting the server. This will provide an interactive UI where you can view and test the API endpoint.
We welcome contributions! Please open an issue or submit a pull request for any improvements or additional features you would like to add.
This project is under the MIT license. See the LICENSE file for more details.
Code made with ♥ by Bruno Oliveira! LinkedIn. 💙
Neste desafio, você usará o que aprendemos sobre Multithreading e APIs para buscar o resultado mais rápido entre duas APIs distintas. As requisições serão feitas simultaneamente para as seguintes APIs:
https://brasilapi.com.br/api/cep/v1/{cep}http://viacep.com.br/ws/{cep}/json/
- Go (Golang)
- Acatar a resposta da API que entregar o resultado mais rápido e descartar a mais lenta.
- O resultado deve ser exibido no command line com os dados do endereço, bem como qual API o enviou.
- Limitar o tempo de resposta em 1 segundo. Caso contrário, deve ser exibido o erro de timeout.
-
Clone este repositório.
-
Navegue até o diretório do projeto.
-
Execute
go mod tidypara garantir que todas as dependências estejam devidamente instaladas. -
Execute o comando
go run main.godentro do diretorio 'src/cmd/api'. Este comando compila e executa o seu código Go que inicia o servidor HTTP, tipicamente configurado para escutar na porta8080. -
Para solicitar informações de endereço usando um CEP, utilize um navegador, curl ou qualquer cliente HTTP, apontando para http://localhost:8080/api/cep/{CEP_AQUI}, substituindo {CEP_AQUI} pelo CEP desejado.
Exemplo usando
curl:curl http://localhost:8080/api/cep/01001000
-
O resultado será exibido no terminal, mostrando os dados do endereço e qual API o enviou.
Para acessar a documentação da API gerada pelo Swagger, navegue até http://localhost:8080/swagger/index.html após iniciar o servidor. Isso proporcionará uma UI interativa onde você pode visualizar e testar os endpoints da API.
Contribuições são bem-vindas! Por favor, abra uma issue ou envie um pull request para quaisquer melhorias ou funcionalidades adicionais que você gostaria de adicionar.
Esse projeto está sob a licença MIT. Veja o arquivo LICENSE para mais detalhes.
Códigos feitos com ♥ by Bruno Oliveira! LinkedIn. 💙