caddy -version)?Caddy 0.9.2
Extending Caddy by creating a new plugin.
0.0.0.0:2222
header / X-Frame-Options "SAMEORIGIN"
header / X-XSS-Protection "1; mode=block"
header / X-Content-Type-Options "nosniff"
#errors /var/log/caddy-errors.log
root /home/user/html
httpstatic
I run Caddy using Golang.
This is my main.go file:
package main
import (
"io/ioutil"
"log"
_ "./caddy/httpstatic"
"github.com/mholt/caddy"
_ "github.com/mholt/caddy/caddyhttp" // Initialize http server
)
func main() {
// Read Caddyfile in the current path
cdyFileBytes, err := ioutil.ReadFile("Caddyfile")
if err != nil {
log.Println(err)
return
}
caddyfile := caddy.CaddyfileInput{
Contents: cdyFileBytes,
Filepath: "./Caddyfile",
ServerTypeName: "http",
}
// Start caddy
caddyInstance, err := caddy.Start(caddyfile)
if err != nil {
log.Fatalln("could not start caddy", err)
}
caddyInstance.Wait()
}
My typical plugin code:
package httpstatic
import (
"fmt"
"net/http"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
type CaddyHandler struct {
NextMiddleware httpserver.Handler
}
func init() {
caddy.RegisterPlugin("httpstatic", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
func (h *CaddyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
return h.NextMiddleware.ServeHTTP(w, r)
}
func setup(c *caddy.Controller) error {
cfg := httpserver.GetConfig(c)
mid := func(next httpserver.Handler) httpserver.Handler {
return &CaddyHandler{
NextMiddleware: next,
}
}
cfg.AddMiddleware(mid)
return nil
}
I expect to have my plugin httpstatic added into directives list, as I think this is part of RegisterPlugin function.
./Caddyfile:11 - Parse error: Unknown directive 'httpstatic'
Run the code that I pasted.
Thanks !
Here are the outputs of DescribePlugins and ValidDirectives functions, as you will see below, the plugin httpstatic is added into the plugins list but it's not added into directives list.
Output of caddy.DescribePlugins():
2016/09/22 15:40:34 Server types:
http
Caddyfile loaders:
short
Other plugins:
http.basicauth
http.bind
http.browse
http.errors
http.expvar
http.ext
http.fastcgi
http.gzip
http.header
http.httpstatic
http.internal
http.log
http.markdown
http.mime
http.pprof
http.proxy
http.redir
http.rewrite
http.root
http.status
http.templates
http.websocket
shutdown
startup
tls
tls.storage.file
Output of caddy.ValidDirectives("http"):
[root bind tls startup shutdown realip git locale log rewrite ext gzip errors minify ipfilter ratelimit search header redir status cors mime basicauth jwt jsonp upload multipass internal pprof expvar prometheus proxy fastcgi websocket markdown templates browse filemanager hugo mailout awslambda]
I expect to have my plugin httpstatic added into directives list, as I think this is part of RegisterPlugin function.
You have to add your directive to directives list in mholt/caddy/caddyhttp/httpserver/plugin.go See wiki for info.
@evvvvr This should be supported automatically by RegisterPlugin, shouldn't be ?
The caddy package will not be a part of the project that I'm working on, the user should be able to launch the project just by cloning caddy, otherwise I need to pack Caddy with the software.
When moving the software to production, I still need to go to the package folder and change the directives list. doesn't seem mature to me.
Note: If you guys liked the idea of including it as a part of RegisterPlugin, I can work on it as a contribution.
You also need to add httpserver.RegisterDevDirective("httpstatic") as well or edit the plugins.go file and add to the list of directives.
As far as I understand, what you're trying to achieve is to have directive (plugin) added to Caddy w/o modifying Caddy code. Unfortunately Caddy is not designed and doesn't work this way — it doesn't support dynamic plugin loading and there are no plans to add it. There were a number of discussions regarding this decision, the latest is #1111.
What you could do with Caddy IMO is either publish your directive as an addon (see here) or (in case you don't want to publish your code) fork Caddy, add your plugin to it and let your users use this custom fork.
@YahiaSweid Thanks for your question! Abiola and Volodymyr are right.
as I think this is part of RegisterPlugin function.
That's not quite the case. The wiki has a step that explains you need to put your directive in the list in the proper order.
This should be supported automatically by RegisterPlugin, shouldn't be ?
No. This results in non-deterministic builds. See this forum post and the godoc for httpserver.RegisterDevDirective. But do not use RegisterDevDirective() outside of development purposes.
Also, not all plugins are associated with a directive.
I expect to have my plugin httpstatic added into directives list, as I think this is part of RegisterPlugin function.
You have to add your directive to directives list in mholt/caddy/caddyhttp/httpserver/plugin.go See wiki for info.
Thank you ,it solved my problem perfectly
Most helpful comment
You have to add your directive to directives list in mholt/caddy/caddyhttp/httpserver/plugin.go See wiki for info.