I am new to golang and working on a restful service in golang using chi. I am trying to create a route as below:
func NewRouter(dm *storage.DatabaseManager) chi.Router {
var router = chi.NewRouter()
router.Method("POST", "/restaurant/search?name={name}",
&SearchRestaurants{DB: dm})
}
Here is how I fetch from mysql db:
func (rm *RestaurantManager) SearchRestaurantsFromDB(name string)
([]Restaurant, error) {
var restaurants []Restaurant
var stmt = fmt.Sprintf(`SELECT * FROM %s WHERE name=%s `,
rm.tableName, name)
var err error
if err = rm.Select(&restaurants, stmt); err != nil {
return nil, err
}
return restaurants, nil
}
...and how I get from these:
func (h SearchRestaurants) ServeHTTP(w http.ResponseWriter, r
*http.Request) {
var err error
var result []storage.Restaurant
name := r.URL.Query().Get("name")
if result, err = h.DB.SearchRestaurantsFromDB(name); err != nil {
log.Fatal("Database Error: ", err)
}
fmt.Print(result)
api.WriteJSON(w, result)
log.Print(r.RequestURI)
}
But I try to hit this endpoint but I get a 404 not found: http://localhost:8000/project/restaurant/search?name={n}
May I know the issue here?
P.S.there is router.Mount("/project/", restaurant.NewRouter(dm))
in the main func.
http://localhost:8000/restaurant/search?name={n}
POST
method. then hitting it from the browser will result 404 error. you should try to to hit it with POST method/
from therouter.Mount("/project/
? Try doingrouter.Mount("/project", restaurant.NewRouter(dm))