So the scope of what im trying to do is something like:
- Query Fake API endpoint
- Save Data
- Insert Data into my Database
- Create my own endpoint
func PublicRoutes(router *chi.Mux) {
// Routes for GET method:
router.Get("/api/v1/user/{id}", controllers.GetUser)
router.Get("/api/v1/users", controllers.GetUsers)
router.Get("/api/v1", controllers.Index)
router.Get("/api/v1/countries", controllers.GetAllCountries)
}
/api/v1/countries returns a 404 page not found, the other endpoints work properly.
the code for my controller is as it follows:
https://pastebin.com/f0Zw6EFX
I should probably start by making it smaller.
My api call is:
httpClient := http.Client{}
req, err := http.NewRequest("GET", "http://localhost:3000/data", nil)
if err != nil {
panic(err)
}
//q := req.URL.Query()
//q.Add("access_key", "YOUR_ACCESS_KEY")
//req.URL.RawQuery = q.Encode()
res, err := httpClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}```
And my queries used on the pastbin controller:
```go
func (q *CountryQueries) GetCountries() ([]models.Country, error) {
// Define countries variable.
var countries []models.Country
// Send query to database.
if err := q.Select(&countries, `SELECT * FROM country`); err != nil {
return []models.Country{}, err
}
return countries, nil
}
Any new eyes could guide me on what I am doing wrong please?
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.