gopocketbase

Pocketbase custom routing, can't curl


I am currently trying to setup my pocketbase backend to listen for webhooks from paddle. I am trying to follow the extend with go docs to setup just a custom route, but I am getting no where with it.

Here is the code I am trying:

func main() {
    app := pocketbase.New()
    
    if err := app.Start(); err != nil {
        log.Fatal(err)
    }
    
    // serves static files from the provided public dir (if exists)
    app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
        
        e.Router.GET("/hello/:name", func(c echo.Context) error {
            fmt.Printf("hello world!")
            name := c.PathParam("name")
    
            return c.JSON(http.StatusOK, map[string]string{"message": "Hello " + name})
        }, /* optional middlewares */)
    
        return nil
    })




}

When I send a curl to this curl http://localhost:8090/hello/a I get an error Not Found. └─ code=404, message=Not Found


Solution

  • I think you put the wrong order, you should call OnBeforeServe before Start

    func main() {
        app := pocketbase.New()
    
        // serves static files from the provided public dir (if exists)
        app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
    
            e.Router.GET("/hello/:name", func(c echo.Context) error {
                fmt.Printf("hello world!")
                name := c.PathParam("name")
    
                return c.JSON(http.StatusOK, map[string]string{"message": "Hello " + name})
            } /* optional middlewares */)
    
            return nil
        })
    
        if err := app.Start(); err != nil {
            log.Fatal(err)
        }
    
    }