Caddy: templates: Render whatever is written as a template, rather than loading a static file

Created on 22 Apr 2017  路  3Comments  路  Source: caddyserver/caddy

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?)

feature request

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.

All 3 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpoehls picture jpoehls  路  54Comments

miekg picture miekg  路  95Comments

pastjean picture pastjean  路  40Comments

hazcod picture hazcod  路  41Comments

wallacyyy picture wallacyyy  路  53Comments