Caddy: Directive isn't being added by calling RegisterPlugin function

Created on 22 Sep 2016  Â·  7Comments  Â·  Source: caddyserver/caddy

1. What version of Caddy are you running (caddy -version)?

Caddy 0.9.2

2. What are you trying to do?

Extending Caddy by creating a new plugin.

3. What is your entire Caddyfile?

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

4. How did you run Caddy (give the full command and describe the execution environment)?

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
}

5. What did you expect to see?

I expect to have my plugin httpstatic added into directives list, as I think this is part of RegisterPlugin function.

6. What did you see instead (give full error messages and/or log)?

./Caddyfile:11 - Parse error: Unknown directive 'httpstatic'

7. How can someone who is starting from scratch reproduce this behavior as minimally as possible?

Run the code that I pasted.

Thanks !

Most helpful comment

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.

All 7 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dafanasiev picture dafanasiev  Â·  3Comments

muhammadmuzzammil1998 picture muhammadmuzzammil1998  Â·  3Comments

lorddaedra picture lorddaedra  Â·  3Comments

ericmdantas picture ericmdantas  Â·  3Comments

mschneider82 picture mschneider82  Â·  3Comments