Day 21: basic Micropub endpoint

Today I added a very basic Micropub endpoint to my Indieweb Toolkit. A Micropub endpoint is a hard thing to keep generic, because every site has a different way of storing data. The idea about the endpoint in the toolkit is that it does all the things that are the same about Micropub.

So here's an example of what it can do. This one simply writes a YAML file with the received fields to a folder. Please note that it overwrites any post with an existing slug. It's only an example.

endpoint::micropub([
  'create' => function($type, $fields, $slug) {
    $map = [
      'entry' => 'blog',
      'event' => 'events',
      'review' => 'reviews',
    ];

    if(!isset($map[$type])) return false;
    if(!$slug) $slug = uniqid();

    yaml::write(__DIR__ . DS . $map[$type] . DS . $slug . '.yml', $fields);

    return url::makeAbsolute('/'.$map[$type] . '/' . $slug . '.yml');
  }
]);

The idea is that you can put this code where-ever you want. It can be at /micropub.php or in some route you define. The new endpoint::micropub() does all the authentication for you and returns the appropriate header. All you need to do is pass in a 'create' callback function, which takes the fields and returns a URL. endpoint::micropub() will do the redirect with a 201 header.

At this moment it only supports 'create' with x-www-form-urlencoded, but JSON and 'delete', 'undelete' and 'update' will follow. I also want callbacks for different mp-synticate-tos, and of course there is the ?q=config query. But I like this idea, and will use this idea to clean out the code of my own endpoint.

Last caveat: at this moment I have not implemented the IndieAuth class in my toolkit. You can steal the class from my kirby-micropub plugin, but I want to clean it first before putting it in the toolkit.

Mijn broer Wouter staat in de finale van de Dutch Barista Championships en heeft koffie nodig. Dus maakte ik ikkoffiejijkoffie.nl voor hem. Doe mee!

Inzichten op de late avond: het probleem is niet dat ik niet op Facebook zit. Het probleem is dat jullie er allemaal wel op zitten, en dat normaal vinden.

En, min of meer door Tegenlicht van vanavond: zwart-wit denken los je niet op met grijstinten. Het tegenovergestelde van zwart-wit is kleur, een totaal andere manier van kijken.

En dat gaat dan dus niet over ‘Trump denkt zwart-wit’ of ‘mensen die Trump erg vinden hebben een psychose’, want dat is in zichzelf nog steeds zwart-wit. Dat relativeren (wat ik net deed) is denk ik een grijstint. Kleur is de nuance proberen te zien.