This is a memo to improve how templates
works. We could potentially extend this idea to markdown
as well.
Right now, the templates
directive loads a static file from disk, parses it as a template, and executes it. Unfortunately, there's no way in Go (that I know of) to render the contents of an io.Reader as a template; it has to be fully buffered in memory. So, that's how we do it.
It could be really useful if templates
would wrap the ResponseWriter with its own that buffers the body, then renders whatever is written to it as a template. The buffering _shouldn't_ be a new problem since we're doing that already anyway. But this would allow us to proxy to a backend that returns template HTML that Caddy then renders.
This idea could extend to markdown
too, because that middleware _also_ has to load the full input into memory anyway.
I'm going to open this to a "help wanted" if anyone would like to take a crack at it. REMEMBER: When wrapping the ResponseWriter, it must satisfy a few interfaces:
var _ http.Pusher = (*MyResponseRecorder)(nil)
var _ http.Flusher = (*MyResponseRecorder)(nil)
var _ http.CloseNotifier = (*MyResponseRecorder)(nil)
var _ http.Hijacker = (*MyResponseRecorder)(nil)
^ These lines can be used as "interface guards" to ensure the type that records the response satisfies the interfaces. See httpserver.ResponseRecorder for an example. (Maybe it would be worth making httpserver.limitWriter
an exported type that other middlewares could use?)
@mholt first off thanks for caddy
! I'd like to get involved. Would you mind if take a stab at this? I'll drop some notes here as I go along so I can check if I'm on the right track.
@mholt first off thanks for caddy! I'd like to get involved. Would you mind if take a stab at this? I'll drop some notes here as I go along so I can check if I'm on the right track.
Let's Go 馃槆
@eticzon Sure -- don't be afraid to submit a PR -- smaller ones are better. i.e. one for templates, one for markdown, for example. We'll review and iterate on it until it's ready to merge.
Most helpful comment
@mholt first off thanks for
caddy
! I'd like to get involved. Would you mind if take a stab at this? I'll drop some notes here as I go along so I can check if I'm on the right track.