Repository navigation
Update dependencies in go.mod #63
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
Open
teobaldotech
wants to merge
4
commits into
robertoduessmann:master
Choose a base branch
from
teobaldotech:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
15e63b2
Update dependencies in go.mod
teobaldotech d93f214
Update .gitignore to include various file types
teobaldotech 6221656
Enhance .gitignore with new patterns and translations
teobaldotech b89ff3f
Refactor main.go to v2 and update weather service
teobaldotech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,125 @@ | ||
| # Binaries for programs and plugins | ||
|
|
||
| # ================================================== | ||
| # Go - arquivos binários compilados | ||
| # ================================================== | ||
| *.exe | ||
| *.exe~ | ||
| *.dll | ||
| *.so | ||
| *.dylib | ||
| *.a | ||
| *.o | ||
| *.obj | ||
| *.test | ||
| *.out | ||
|
|
||
| # Test binary, build with `go test -c` | ||
| # Binários gerados pelo projeto | ||
| /weather-api | ||
| /weather-api.exe | ||
| /bin/ | ||
| /build/ | ||
| /dist/ | ||
|
|
||
| # ================================================== | ||
| # Go - dependências e ferramentas | ||
| # ================================================== | ||
| vendor/ | ||
|
|
||
| # ================================================== | ||
| # Testes e cobertura | ||
| # ================================================== | ||
| coverage.out | ||
| coverage.txt | ||
| coverage.html | ||
| coverage.xml | ||
| coverage.json | ||
| cover.out | ||
| coverprofile.out | ||
| *.coverprofile | ||
| *.test | ||
|
|
||
| # Output of the go coverage tool, specifically when used with LiteIDE | ||
| *.out | ||
| # ================================================== | ||
| # Profiling e diagnósticos | ||
| # ================================================== | ||
| *.prof | ||
| *.pprof | ||
| *.cpuprofile | ||
| *.memprofile | ||
| *.heapprofile | ||
| *.trace | ||
| *.cover | ||
|
|
||
| # ================================================== | ||
| # Variáveis de ambiente e segredos | ||
| # ================================================== | ||
| .env | ||
| .env.* | ||
| !.env.example | ||
| !.env.*.example | ||
|
|
||
| # Arquivos de credenciais e chaves privadas | ||
| *.pem | ||
| *.key | ||
| *.p12 | ||
| *.pfx | ||
| *.keystore | ||
| secrets.yaml | ||
| secrets.yml | ||
| secrets.json | ||
| credentials.json | ||
|
|
||
| # ================================================== | ||
| # Logs | ||
| # ================================================== | ||
| *.log | ||
| logs/ | ||
| log/ | ||
|
|
||
| # ================================================== | ||
| # Arquivos temporários | ||
| # ================================================== | ||
| *.tmp | ||
| *.temp | ||
| *.bak | ||
| *.swp | ||
| *.swo | ||
| *.orig | ||
| *~ | ||
| .temp/ | ||
| tmp/ | ||
| temp/ | ||
|
|
||
| # ================================================== | ||
| # Sistemas operacionais | ||
| # ================================================== | ||
| .DS_Store | ||
| .DS_Store? | ||
| ._* | ||
| Thumbs.db | ||
| Desktop.ini | ||
|
|
||
| # ================================================== | ||
| # IDEs e editores | ||
| # ================================================== | ||
| .vscode/ | ||
| .idea/ | ||
| *.iml | ||
| *.sublime-workspace | ||
| *.sublime-project | ||
| *.code-workspace | ||
| .project | ||
| .classpath | ||
| .settings/ | ||
|
|
||
| # dep vendor folder | ||
| vendor | ||
| # ================================================== | ||
| # Go - arquivos gerados por ferramentas | ||
| # ================================================== | ||
| go.work.sum | ||
|
|
||
| # build app file | ||
| weather-api | ||
| # ================================================== | ||
| # CI/CD e ferramentas locais | ||
| # ================================================== | ||
| coverage/ | ||
| .nyc_output/ | ||
| .cache/ | ||
| .pytest_cache/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,21 @@ | ||
| ```go | ||
| module github.com/robertoduessmann/weather-api | ||
|
|
||
| go 1.23.0 | ||
|
|
||
| toolchain go1.23.2 | ||
|
|
||
| require ( | ||
| github.com/PuerkitoBio/goquery v1.10.0 | ||
| github.com/PuerkitoBio/goquery v1.10.3 | ||
| github.com/caarlos0/env v3.5.0+incompatible | ||
| github.com/gorilla/handlers v1.5.2 | ||
| github.com/gorilla/mux v1.8.1 | ||
| github.com/stretchr/testify v1.12.1 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/andybalholm/cascadia v1.3.2 // indirect | ||
| github.com/felixge/httpsnoop v1.0.3 // indirect | ||
| github.com/stretchr/testify v1.9.0 // indirect | ||
| golang.org/x/net v0.38.0 // indirect | ||
| ) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,71 @@ | ||
| package main | ||
| package v2 | ||
|
|
||
| import ( | ||
| "log" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/gorilla/handlers" | ||
| "github.com/gorilla/mux" | ||
| "github.com/robertoduessmann/weather-api/config" | ||
| "github.com/robertoduessmann/weather-api/controller" | ||
| v2 "github.com/robertoduessmann/weather-api/controller/v2" | ||
| "net/url" | ||
| "time" | ||
| ) | ||
|
|
||
| func main() { | ||
| log.Println("Starting application") | ||
|
|
||
| weather := mux.NewRouter() | ||
|
|
||
| log.Println("Creating routes") | ||
| weather. | ||
| Path("/weather/{city}"). | ||
| Methods(http.MethodGet). | ||
| HandlerFunc(controller.CurrentWeather) | ||
|
|
||
| weather. | ||
| Path("/v2/weather/{city}"). | ||
| Queries("unit", "{unit}"). | ||
| Methods(http.MethodGet). | ||
| HandlerFunc(v2.CurrentWeather) | ||
|
|
||
| weather. | ||
| Path("/v2/weather/{city}"). | ||
| Methods(http.MethodGet). | ||
| HandlerFunc(v2.CurrentWeather) | ||
|
|
||
| log.Println("Created routes") | ||
| port := config.Get().Port | ||
| log.Println("Initialize server on port: " + port) | ||
| if err := http.ListenAndServe(":"+port, handlers.CORS()(weather)); err != nil { | ||
| log.Fatal(err) | ||
| // Estrutura do retorno da API OpenWeatherMap | ||
| type OpenWeatherResponse struct { | ||
| Main struct { | ||
| Temp float64 `json:"temp"` | ||
| } `json:"main"` | ||
| Name string `json:"name"` | ||
| } | ||
|
|
||
| type WeatherService struct { | ||
| APIKey string | ||
| HTTPClient *http.Client | ||
| } | ||
|
|
||
| func NewWeatherService(apiKey string) *WeatherService { | ||
| return &WeatherService{ | ||
| APIKey: apiKey, | ||
| HTTPClient: &http.Client{ | ||
| Timeout: 10 * time.Second, // Timeout para evitar requisições presas | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (s *WeatherService) FetchWeather(ctx context.Context, city, unit string) (*OpenWeatherResponse, error) { | ||
| // Mapeia unidades para o padrão da OpenWeatherMap (metric / imperial) | ||
| unitsParam := "metric" | ||
| if unit == "fahrenheit" { | ||
| unitsParam = "imperial" | ||
| } | ||
|
|
||
| endpoint := fmt.Sprintf( | ||
| "https://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", | ||
| url.QueryEscape(city), unitsParam, s.APIKey, | ||
| ) | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("erro ao criar requisição: %w", err) | ||
| } | ||
|
|
||
| resp, err := s.HTTPClient.Do(req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("erro ao chamar API externa: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode == http.StatusNotFound { | ||
| return nil, fmt.Errorf("cidade não encontrada") | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("resposta inesperada da API: status %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var data OpenWeatherResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { | ||
| return nil, fmt.Errorf("erro ao decodificar JSON: %w", err) | ||
| } | ||
|
|
||
| return &data, nil | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.