How to read and send static html file to browser

func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

I want to read and send a static file to the client side, based on the’root 'configuration. What should be done. For example:x.readAndSend (x.get ("root ") . "/ static.html")

That’s what the file_server module does already – can you just use that?

How to read that config item?

file_server { root .... }
root /a /data/a
root /b/* /data/b

emm. in the custom module

I don’t understand the question.

Please elaborate on what you’re trying to do. Please be as detailed as possible.

I’m learning to develop a request flow limiting extension module for caddy.

The configuration file for the design is mymodule { req_per_second int, respond xx.html }

I want to return xx.html as response output when the request reaches req_per_seond.

But I want to read the file based on the root path of the current server’s configuration, and I don’t know how I can read the root configuration, or how to determine multiple root configurations

1 Like

There’s already this rate limiting plugin FYI: GitHub - mholt/caddy-ratelimit: HTTP rate limiting module for Caddy 2

You can use handle_errors to write a custom response when rate limits are hit.

That said, root directive sets a var which is stored in the request context. Read the source for the file_server handler to see how it’s grabbed.

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.