PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/content/posts/2019-10-30-reader-mail-hugo-micropub.md

https://gitlab.com/jamietanna/jvt.me
Markdown | 101 lines | 66 code | 35 blank | 0 comment | 0 complexity | bf96cd45f5844787b0c5b684a0c01cde MD5 | raw file
  1. ---
  2. title: "Reader Mail: Getting Started with Hugo and Micropub"
  3. description: "Answering a question by a reader about how to get started with writing a Micropub endpoint for use with Hugo."
  4. tags:
  5. - www.jvt.me
  6. - micropub
  7. - hugo
  8. - reader-mail
  9. license_code: Apache-2.0
  10. license_prose: CC-BY-NC-SA-4.0
  11. date: 2019-10-30T21:12:17+0000
  12. slug: "reader-mail-hugo-micropub"
  13. image: https://media.jvt.me/445ff8a8fc.png
  14. ---
  15. I received an email yesterday from <span class="h-card"><a class="u-url" href="https://www.barbersmith.com">Ben Barbersmith</a></span>, and with Ben's consent thought I would reply publicly.
  16. The email in question:
  17. > Hi Jamie,
  18. > I've read a ton of your blog posts and hope you don't mind me emailing you!
  19. > First up, thanks a ton for your posts about IndieWeb. I was completely unaware of this movement until I saw some of your blog posts on lobste.rs, but it seems really cool.
  20. > I've recently migrated my own site to Hugo ([www.barbersmith.com](https://www.barbersmith.com)), and your posts about the micropub API caught my attention. I'd sorely love to be able to add to my hugo site from a decent mobile client, and micropub seems like the correct solution. You mentioned you had solved the problem already, but I guess the code is very specific to your setup.
  21. > Any hints or tips for how I might set up a similar solution? I'm familiar with writing and deploying backend services, just wondering if there are any gotchas or words of wisdom you have specifically about implementing the micropub server API, using it to generate hugo files, trigger the site build or auto commit to git.
  22. > If you do have any code to share, no matter how scrappy, I'd be grateful!
  23. > Thanks in advance for any time or advice you can provide.
  24. > All the best, Ben
  25. A great email, I appreciate you getting in touch and am more than happy to chat more about my work.
  26. > If you do have any code to share, no matter how scrappy, I'd be grateful!
  27. Firstly, it turns out that I had not updated my [blog post announcing my Micropub support](/posts/2019/08/26/setting-up-micropub/) to share the underlying source code - woops! That is now addressed.
  28. My code can be found at [<i class="fa fa-gitlab"></i> jamietanna/www-api](https://gitlab.com/jamietanna/www-api/tree/master/www-api-web/micropub) and is available as Free Software, under the [GNU Affero General Public License v3](https://www.gnu.org/licenses/agpl-3.0.en.html) so please do use it, but remember to abide by the license terms.
  29. > specific to your setup
  30. It's true that a lot of this will be specific to the way that I create content, and that my content is made up, but there should be at least some ideas for what to implement yourself.
  31. > Any hints or tips for how I might set up a similar solution?
  32. Firstly, I would ask how the site's content is stored? Is it stored in a git repo and put into GitLab.com, GitHub.com, or some other source control offering? Or is it not tracked anywhere?
  33. Once you know how it's stored, that'll determine what your underlying endpoint will need to work with.
  34. From what I understand [nanopub](https://indieweb.org/nanopub) will publish to disk, which means that if you're not using source control it's a better fit.
  35. If you are, you'll need to do some work to make it handle the authentication with their API, and programmatically creating content on your site from the API.
  36. > any gotchas or words of wisdom you have specifically about implementing the micropub server API
  37. My daily driver for Micropub interactions is [Indigenous for Android](https://github.com/swentel/indigenous-android), but I also publish content using [MicroPublish.net](https://micropublish.net) and [Quill](https://quill.p3k.io) as well as implementing my own client for my [step counts](/kind/steps/).
  38. I found that being a well-formed [OAuth2 Resource Server](https://www.oauth.com/oauth2-servers/the-resource-server/) can be a bit of work, and then also being a well-formed IndieAuth Resource Server, too.
  39. I found that one thing that tripped me up initially was that I didn't support both `x-www-form-urlencoded` and `multipart/form-data` content types.
  40. > using it to generate hugo files
  41. For this, I settled on my own JSON structure for a request, which was mapped very closely to the underlying Microformats2 data that the post would contain, with some extra properties for Hugo.
  42. For instance, this is a like:
  43. ```json
  44. {
  45. "kind" : "likes",
  46. "slug" : "2019/10/3NnXz",
  47. "client_id" : "https://micropublish.net",
  48. "date" : "2019-10-25T19:03:02.264+02:00",
  49. "h" : "h-entry",
  50. "properties" : {
  51. "name" : [ "Like of https://www.reddit.com/r/Eyebleach/comments/dmvrkl/smile_for_the_camera/" ],
  52. "like-of" : [ "https://www.reddit.com/r/Eyebleach/comments/dmvrkl/smile_for_the_camera/" ],
  53. "published" : [ "2019-10-25T19:03:02.264+02:00" ]
  54. }
  55. }
  56. ```
  57. I made it a JSON format because then it would be super easy to publish via an API, instead of having to create a mix of YAML/TOML and Markdown.
  58. > trigger the site build
  59. For this, because my site is using GitLab CI as its means to build/test/deploy the site, I didn't need to do anything special to get the site building. As soon as the commit arrived on `master`, it would start the process to build + deploy it.
  60. > auto commit to git.
  61. For this, I wanted to go API first and interact with GitLab.com's API (as I use GitLab.com for my repo hosting) rather than actually `git commit`ing locally, so I would recommend similar.
  62. In terms of other recommendations:
  63. - Read [the spec for Micropub](https://www.w3.org/TR/micropub/) and see if things make sense, if they don't, then [come and chat with the IndieWeb community](https://indieweb.org/discuss)
  64. - I would recommend having a "stub" server set up that will allow you to test your Micropub endpoint with a Micropub client, without publishing content, as it'll make it easier to verify things work without testing in production (but it may also be OK if you don't mind)!
  65. - Don't worry about implementing all the features, I've only got create functionality via forms, not via JSON, nor things like read/update/delete as I don't have a need yet for all the functionality
  66. - Also, don't feel like you need to implement a media endpoint for now, as you may not be publishing anything media-wise!