package main import ( "embed" "io/fs" "mime" "net/http" "github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/core" ) //go:embed all:web/build var webBuild embed.FS // apiMethods are the methods the catch-all for unknown API routes covers. HEAD is // left out because it would conflict with every GET route of the router; GET // patterns answer HEAD requests anyway. var apiMethods = []string{ http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodOptions, } func registerUI(app core.App) { _ = mime.AddExtensionType(".webmanifest", "application/manifest+json") sub, err := fs.Sub(webBuild, "web/build") if err != nil { panic(err) } notFound := func(e *core.RequestEvent) error { return e.NotFoundError("Not found.", nil) } app.OnServe().BindFunc(func(se *core.ServeEvent) error { // Without this the SPA catch-all below answers unknown API routes with // the index page and status 200, which the client SDK cannot parse. // The routes registered before keep precedence because they are more // specific than this catch-all. for _, method := range apiMethods { if !se.Router.HasRoute(method, "/api/{path...}") { se.Router.Route(method, "/api/{path...}", notFound) } } if !se.Router.HasRoute("GET", "/{path...}") { se.Router.GET("/{path...}", apis.Static(sub, true)) } return se.Next() }) }