Using Ogen with HTMX + Templ #1203
Replies: 1 comment 2 replies
-
I have two separate OpenAPI specifications for the same app - one for the JSON API and one for the HTML (hypermedia) API, which is using The same Go executable then embeds both servers (JSON and HTML) and I've added a mux which depending on the presence of the // jsonServer and htmlServer are results of ogen's NewServer function.
mux := http.NewServeMux()
mux.Handle("/api/", jsonServer)
mux.Handle("/", htmlServer) For returning HTML you can put the following into the configuration file for ogen ( generator:
content_type_aliases:
text/html: "text/plain" Then you can use that in a response like: "200":
description: OK
content:
text/html:
schema:
type: string Are you sure that you'd rather have one specification with support for both HTML and JSON responses instead of separate specifications? I haven't (yet) introduced HTMX into the equation, but if I did I'd probably try doing something like you've mentioned - reacting to the presence and content of headers HTMX is sending and eventually (optimisation) rendering only a subset of the template, initially letting HTMX do the extraction of the relevant part of the whole HTML response. I'd still keep the HTML and JSON APIs separate since they can be, I believe, (c)leaner that way, and there shouldn't be that much overhead if you keep the handler logic minimal. You've mentioned that you've already separated the implementation into multiple layers, which goes hand in hand with this 😄 |
Beta Was this translation helpful? Give feedback.
-
Hi folks, looking to get some help for a SaaS I am currently building.
Our stack is Go + HTMX with TailwindCSS and Templ.
We want to use an OpenAPI schema as the source of truth, with a schema first approach to ensure consistency in our APIs.
For this, we are using ogen, which we have set up correctly and tested, allowing us to hit endpoints. At the moment, this is all JSON, since this was mostly testing ogen as a proof of concept.
Lookign forward, we are trying to figure out the most idiomatic way to handle the fact that HTMX expects an HTML response, not JSON. With that being said, we still want to support JSON format so that we can open our API in the future, without maintaining 2 versions.
I know this may not be the best approach, but what are some ways you guys can think of doing this cleanly? I was thinking of using headers, since HTMX sets an HTXM specific header on every API requests. Using this header, we can use a middleware to parse the Response object into HTML/Templ.
I have seen this pattern mentioned before, but never with tools like ogen.
Can someone help me understand the best way to approach this, and some examples if possible?
If needed, I can also post our current structure, although we are in the early stages so its mostly a basic OpenAPI schema and a Go Web App with 3 layers (handlers, services, and repositories).
Thanks in advance for any help!
Beta Was this translation helpful? Give feedback.
All reactions