distrakt/OmEspHelpers

Add example showing how to serve data from SPIFFS/FatFS

Closed this issue · 10 comments

I actually like the fact that OSMEspHelpers does not require FatFS or SPIFFS, but it would be nice to have mixed examples if you have pages/pictures that are easier to read from 'storage'

Right on!
The example "WebServerSketch1" now includes serving a jpeg image, and the "all controls" page has a button that links to that internal URL. (Library files have some changes as well, update all.)

The jpeg in this case is hardcoded in a file, lots 0x23 for a 10k array.

The serving code looks like this:

  p.addUrlHandler("ginger.jpg",  [] (OmXmlWriter & w, OmWebRequest & r, int ref1, void *ref2)
  {
    p.renderHttpResponseHeader("image/jpg", 200);
    for(unsigned int ix = 0; ix < jpgImageSize; ix++)
    {
      // the OmXmlWriter can also emit raw binary bytes, one at a time, like this:
      w.put(jpgImage[ix]);
    }
  });

Should do the trick?

the ginger example looks great, thank you, working on integrating this with my code
(in my case, I need a simple file browser to serve files from FATFS, including a config file that I modify from code, and I need to read and display from FATFS to make sure the file I just wrote looks ok. It's a lot easier if I can just read it and display it as a web page, but I forsee other examples where reading static data from FATFS is going to be a lot simpler than having to generate it from C code :).
Thank you (will close when I have my code working, but I'm sure it won't be a problem)

actually one idea already (damn those users, they keep having more ideas :) ):
p.addUrlHandler("ginger.jpg", [] (OmXmlWriter & w, OmWebRequest & r, int ref1, void *ref2)
works, but if I have 10 pages to serve, that means I need 10 handlers with hardcoded names.

It'd be easier to have a generic serving handler that returns the unknown URL (i.e. URL not served by anything else) as a string, and then the callback receives that string in void *ref2 and is free to serve any page related to the string given (which would be both page name and even ?get=foo HTTP GET data if needed, which I don't need now, but it would help write your own custom handlers for things not supported natively by your lib)

I've been seduced by C lambdas... but they're not always the best.

p.addUrlHandler("ginger2.jpg", handyHandler, 123, NULL);
// last two args are int ref1 and void *ref2

and

void handyHandler(OmXmlWriter & w, OmWebRequest & r, int ref1, void *ref2)
{
  // you could use ref1, ref2 any way you like...
  // OmWebRequest r.path is the url without query terms,
  // OmWebRequest r.query is a vector of char *s, alternating key & value 
  // this handler ignores both
  p.renderHttpResponseHeader("image/jpg", 200);
  for (unsigned int ix = 0; ix < jpgImageSize; ix++)
  {
    // the OmXmlWriter can also emit raw binary bytes, one at a time, like this:
    w.put(jpgImage[ix]);
  }
}

Shows part of what you describe. (Will add to example as well, thanks!)
Need also the "wildcard" handler.

oops accidentally closed, reopining! that is, reopening.

Added ability for a wildcard handler, which is invoked if no page or specific URL handler matches.

WebServerSketch1 example includes it, wildcardProc(), and shows reading the request path and the request query arguments if any.

And it's added by OmWebPages::addUrlHandler(wildcardProc);.

Awesome, just used the original hook marcmerlin/NeoMatrix-FastLED-IR@61d1eb1
will try the new one you just added, no hardcoding = much better

Thank you.
marcmerlin/NeoMatrix-FastLED-IR@9c29803
now adds code to read any random file from SPIFFS/FATFS, thanks for the generic handler.

I tried to make a button with
p.addButtonWithLink("link", "/demo.txt", wildcardProc, 0);
and that didn't quite compile, not sure if there is an easy way to write that, but I made it work with a simple a href html link instead.

as discussed in IM, addButtonWithLink could trigger a local call before navigating away to the target link, but it's not very likely to be used much
my suggestion is to leave that functionality if it's needed for a corner case, but to default the function signature to NULL, 0 NULL for the last 3 args since most calls will really only be
p.addButtonWithLink("link", "/demo.txt")

This was fixed, but I never closed this issue. Syntax is
p->addButtonWithLink("Demo Map", "/demo_map.txt", NULL, 0);
Thank you.