Reset pouch for GPII development config
Tony Atkins
tony at raisingthefloor.org
Tue Aug 30 08:37:47 UTC 2016
Hi, Cindy:
First, apologies in advance for the code in the body of the message,
pastie.org is down at the moment... :|
The first problem is the use of "gpii.express.router". Although this was
not always the case, the "gpii.express.router" grade is currently mainly
intended to contain other pieces of middleware (for example, to organize
middleware into a hierarchy of paths), or to wrap third-party software that
wants to act like an express router rather than middleware.
In your case, you need to either use the "gpii.express.middleware" grade,
or, if you want to use a handler, you need to use one of the middleware
grades that support handlers:
https://github.com/GPII/gpii-express/blob/master/src/js/requestAwareMiddleware.js
https://github.com/GPII/gpii-express/blob/master/src/js/contentAwareMiddleware.js
To use the first of these, your component options would need to be updated
to look more like the following (in place of "resetRouter" in your example):
resetMiddleware: {
>
> type: "gpii.express.middleware.requestAware",
>
> options: {
>
> path: "/reset-pouch",
>
> namespace: "resetMiddleware",
>
> method: "get", // This is actually the default, you can omit it
>
> priority: "first",
>
> handlerGrades: ["gpii.pouchManager.reset.handler"]
>
> }
>>
> }
>
>
That's enough to ensure that a handler is created and that your request
handling function is called. You should update your handler function to
send a response, as in:
// "handler" approach
>
> gpii.pouchManager.reset.resetPouch = function (that, request, response,
>> next) {
>
> // Do something here.
>
> console.log("===== reset-pouch");
>
> // If there's an error, report it.
>
> // that.sendError(500, { ok: false, message: "not cool, dude." });
>
>
>> // Otherwise, return a response.
>
> that.sendResponse(200, {ok: false, message: "I haven't figured out
>> what to say to you just yet..."});
>
> };
>
>
However, as you are not managing a high volume of simultaneous transactions
or keeping track of information per request, you can use a simpler
approach, which is to write a middleware function. Here is an example
which illustrates the approach:
fluid.defaults("gpii.pouchManager.bindResetHandlers", {
>
> gradeNames: ["fluid.component"],
>
> components: {
>
> resetMiddleware: {
>
> type: "gpii.express.middleware",
>
> options: {
>
> path: "/reset-pouch",
>
> namespace: "resetMiddleware",
>
> priority: "first",
>
> invokers: {
>
> middleware: {
>
> funcName: "gpii.pouchManager.reset.resetPouch",
>
> args: ["{that}", "{arguments}.0", "{arguments}.1",
>> "{arguments}.2"] // request, response, next
>
> }
>
> }
>
> }
>
> }
>
> }
>
> });
>
>
>> // "middleware" approach
>
> gpii.pouchManager.reset.resetPouch = function (that, request, response,
>> next) {
>
> // Do something here.
>
> console.log("===== reset-pouch");
>
> // If there's an error, call `next` with it.
>
> // next({ ok: false, message: "not cool, dude."});
>
>
>> // Otherwise, return a response via response.send (see the Express and/or
>> gpii-express docs).
>
> response.status(200).send({ok: false, message: "I haven't figured out
>> what to say to you just yet..."});
>
> };
>
>
If you follow this approach, you can remove your handler grade definition.
I also added examples of sending a response and error, as it's slightly
different than with a handler function.
Anyway, I hope that helps clarify at least somewhat. Happy to pair on it
from 17:00-17:30 CET or chat online.
Cheers,
Tony
On Mon, Aug 29, 2016 at 10:56 PM, Li, Cindy <cli at ocadu.ca> wrote:
> Hi Tony,
>
> As we discussed in today’s pouch meeting, I’m adding a http request
> handler for resetting pouch to its initial state when GPII runs in the
> development mode and the pouch DB is used as the data storage for the
> authorization server.
>
> My thought is to add a request handler of /reset-pouch at the component
> level of “pouchHarness.express”. Speaking in code, this component sits at
> https://github.com/the-t-in-rtf/gpii-pouchdb/blob/GPII-
> 1897/src/js/harness.js#L31
>
> So, i’m learning gpii-express to understand how to add a request handler
> with it. My experiment so far, which is not very successful:
> https://github.com/cindyli/universal/blob/GPII-1821/gpii/
> node_modules/pouchManager/src/pouchManager.js#L62
>
> This resetHandler component is distributed to “pouchHarness.express” at
> line 55.
>
> The issue is the actual reset handler at line 85 is not hooked up.
>
> Could you shed a light on how to connect “gpii.express.router” and
> “gpii.express.handler”? Let me know if I’m using wrong components at all.
> Thanks.
>
> The other thing I’d like to discuss is that, in the meeting we were
> talking about adding a createOnEvent on the pouchHarness and fire that
> event at the reset to trigger it to recreate. I wonder if we could do it by
> directly calling initDBs() once the clean up is finished since initDb() and
> cleanUp() are from the same component (gpii.pouch.express) API, less
> destructive and hopefully not less cleaner. :)
>
> Thanks.
>
> Cindy
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.idrc.ocad.ca/pipermail/fluid-work/attachments/20160830/dec32499/attachment.htm>
More information about the fluid-work
mailing list