// HandleIndex serves a get request at /pages func HandleIndex(context router.Context) error { // Authorise err := authorise.Path(context) if err != nil { return router.NotAuthorizedError(err) } // Fetch the pages q := pages.Query().Order("url asc") // Filter if necessary filter := context.Param("filter") if len(filter) > 0 { filter = strings.Replace(filter, "&", "", -1) filter = strings.Replace(filter, " ", "", -1) filter = strings.Replace(filter, " ", " & ", -1) q.Where("(to_tsvector(name) || to_tsvector(summary) || to_tsvector(url) @@ to_tsquery(?) )", filter) } pageList, err := pages.FindAll(q) if err != nil { context.Logf("#error Error indexing pages %s", err) return router.InternalError(err) } // Serve template view := view.New(context) view.AddKey("filter", filter) view.AddKey("pages", pageList) return view.Render() }
// HandleShowPath serves requests to a custom page url func HandleShowPath(context router.Context) error { // Setup context for template path := context.Path() // If no pages or users exist, redirect to set up page if missingUsersAndPages() { return router.Redirect(context, "/fragmenta/setup") } q := pages.Query().Where("url=?", path).Limit(1) pages, err := pages.FindAll(q) if err != nil || len(pages) == 0 { return router.NotFoundError(err) } // Get the first of pages to render page := pages[0] // For show path of pages, we authorise showing the page FOR ALL users if it is published if !page.IsPublished() { // Authorise err = authorise.Resource(context, page) if err != nil { return router.NotAuthorizedError(err) } } return renderPage(context, page) }