First Go Project: A Jam-stack Commenting API

First Go Project: A Jam-stack Commenting API

go indiego project tiim.ch web-api

by
published on last updated on

I recently have been looking around for a simple commenting system to integrate into my website. Since my website is a pre-rendered static Html site hosted on Github Pages, there is no way for it to directly store comments because it does not have a database. The only option for dynamic content to be stored is with an external service.

I kept my eyes open for a service that I liked, but I did not want to just integrate any old service into my website, I did have some requirements:

  • The service should not cost anything. I would rather host something myself than sign up for another subscription (because I'm already paying for a VPS anyway).
  • I want to control how the comments on my website are displayed. I quite like my website design and I don't want a generic comment box below my posts.
  • The service should respect the privacy of the people using my website.
  • There should be an option to comment without setting up an account with the service.

While looking around for how other people integrated comments into their static websites, I found a nice blog post from Average Linux User which compares a few popular commenting systems. Unfortunately, most systems either are not very privacy-friendly, cost money or store the comments as comments on Github issues..? After looking through the options I decided to use this opportunity to write my own commenting system and dabble with the Go programming language.

Writing a commenting API in Go

First thing first, if you want to take a look at the code, check out the Github repo.

I decided to write the commenting system in Go because I have been looking for an excuse to practice Go for a while, and this seemed like the perfect fit. It is a small CRUD app, consisting of a storage component, an API component and a small event component in the middle to easily compose the functionality I want.

Currently, it supports the following functionality:

  • Listing all comments (optionally since a specified timestamp)
  • Listing all comments for a specified page (optionally since a specified timestamp)
  • Posting comments through the API
  • A simple admin dashboard that lists all comments and allows the admin to delete them
  • Email notifications when someone comments
  • Email notifications when someone replies to your comment
  • SQLite storage for comments

The code is built in a way to make it easy to customise the features. For example to disable features like the email reply notifications you can just comment out the line in the main.go file that registers that hook.

To write custom hooks that get executed when a new comment gets submitted or one gets deleted, just implement the Handler interface and register it in the main method.

You can also easily add other storage options like databases or file storage by implementing the Store and SubscribtionStore interfaces.

Can it be used in production? πŸš—πŸ’¨

I currently use it on this website! Go test it out (I might delete the comments if they are rude though πŸ€”).

In all seriousness, I would not use it for a website where the comments are critical. But for a personal blog or similar, I don't see why not.

If you want to host your own version, there is a Dockerfile available. If you decide to integrate this into your website, please comment below, ping me @TiimB or shoot me an email hey@tiim.ch, I would love to check it out.

You found an error in this post? Open a pull request.
Photo of Tim Bachmann

Hi, my name is Tim Bachmann! I'm a master graduate in computer science at University of Basel, swimmer and swim coach.

I am passionate about all things web development, swimming, personal knowledge management and much more. If you liked this or any of my posts, feel free to follow me.

11 Comments and Interactions

Leave a comment or interact with this page via WebMention
  • Tim Bachmann

    2023-08-02 9:10:04 AM

    Mentioned this post

    I blogged about creating a comment system for my website a while ago, and later how I implemented webmentions into that same project. Since then this little go program has grown quite a bit, and it has turned into a modular platform that supports quite a few technologies:...

  • Anonymous

    2023-07-09 7:25:02 PM

    Heya just saw your post on Reddit about this comment feature, didn't want to leave without using it ^^. Nicely done!

  • In reply to undefined

    You are right, there is not any documentation in the readme yet. Although hopefully, I will work on that soon. I'm in the middle of refactoring the project. To query the comments there are two rest endpoints ["/comment"](https://github.com/Tiim/IndieGo/blob/044b58e96dae112ceaca509f8541c84db3ef50f3/api/comment.go#L41-L71) and ["/comment/:page"](https://github.com/Tiim/IndieGo/blob/044b58e96dae112ceaca509f8541c84db3ef50f3/api/comment.go#L73-L107) which return all comments or the comments for a specific page. The comments are loaded from this API endpoint when the site is generated. To display the comments without rebuilding the site, new comments are fetched in the browser with the "?since=<time-of-last-build>" query parameter.

  • Good stuff. Always nice to see a site supporting comments and/or Webmentions. Maybe I missed it in the readme but I am curious as to how one queries the API for comments. Do you pull in the comments from the database when you generate the site?

  • Golang Bot

    2022-11-27 10:31:58 PM

    Mentioned this post

    I published a new blog post: First Go Project: A jam-stack Commenting API tiim.ch/blog/2022-07-1… #golang #jamstack #API

  • Tim Bachmann

    2022-11-21 10:19:23 PM

    Mentioned this post

    This site now supports sending and receiving webmentions and surfacing structured data using microformats2.

  • hola

  • In reply to undefined

    And a polite reply

  • Pretty cool dudez

  • This is a rude comment ;)

  • Good job dude!

Built with SvelteKit and hosted on GitHub Pages.

View this website on GitHub!

Other pages