Sample HTTP Middleware Plugin?


(Jim Mendenhall) #1

Is there a repo out there somewhere with a very basic http middleware plugin? I’ve been reading the docs here, but honestly, with just code snippets and not full files, it’s kind of hard to follow. Any examples or links that you could share would be very helpful!


(Matt Holt) #2

I should probably put up a repo with a middleware stub that people can use as a template. I’ll try to do that this week sometime!

Whatcha building ? :grin:


(Jim Mendenhall) #3

I want to build a layout middleware (for lack of a better word) that takes the source files or proxied backend output and inserts it into a layout. I really don’t even understand if that’s possible but really want to play around with this and figure it out.


(Jim Mendenhall) #4

@matt In my attempt to get my plugging working (I can’t get anything to work) I started reading the source code for the Markdown plugin. I guess I missed somehow when reading the documentation that you have templates included in the Markdown functionality!

Honestly, this probably solves my use case! The inclusion of front matter even solves the inevitable problem of needing to pass more than one block of text/code from the backend server to the template. So, I guess I don’t need to write a plugin after all … although, I’d really like to try my hand at it sometime.


(Jim Mendenhall) #5

Except it doesn’t seem possible to use the markdown directive and the proxy directive together …


(Matt Holt) #6

Either one or the other will handle the request; either it is proxied to a backend, or markdown renders the response.

I think there is a WIP implementation of using templates and proxy together, where templates can render a proxied page, but I don’t think it’s been extended to markdown.


(Jim Mendenhall) #7

@matt Interesting that you mention templates. I’ve been using http.templates with http.proxy for a project since last year and it works great!

What I actually want to do is avoid the need to include the template syntax in the proxied app. Right now, the app (ex. Rails) needs to return template syntax like so.

...
{{.Include "caddy-templates/rails/head/index.html"}} 
...
html from rails app ...

It would be very nice to have a layout/template that proxied content is render into, allowing you to make minimal changes to your backend app and just output the very minimal content that is needed to be plugged into your site template.

Looking at the Caddy markdown code gave me the idea of running the proxied content through something very similar, with templates and front matter. This would be very powerful!


(Jim Mendenhall) #8

This is the relevant part of my Caddyfile where I proxy requests to a Rails app that includes template syntax which is then rendered correctly by http.template.

http://0.0.0.0:8080 {
    root /somepath
    gzip
    internal /caddy-templates
    templates / .html
    proxy /reporter/ http://localhost:3000 {
      transparent
   }
}

(Jim Mendenhall) #9

Looks like that WIP made it into the code with this commit.


(Jim Mendenhall) #10

For those who find this. It would seem that your plugin also needs to be listed in https://github.com/mholt/caddy/blob/master/caddyhttp/httpserver/plugin.go to work.


(system) #11

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.