示例#1
0
// setup used internally by Caddy to set up this middleware
func setup(c *caddy.Controller) error {
	mc, err := parse(c)
	if err != nil {
		return err
	}

	if c.ServerBlockKeyIndex == 0 {
		// only run when the first hostname has been loaded.
		if mc.maillog, err = mc.maillog.Init(c.ServerBlockKeys...); err != nil {
			return err
		}
		if err = mc.loadFromEnv(); err != nil {
			return err
		}
		if err = mc.loadPGPKeys(); err != nil {
			return err
		}
		if err = mc.loadTemplate(); err != nil {
			return err
		}
		if err = mc.pingSMTP(); err != nil {
			return err
		}

		c.ServerBlockStorage = newHandler(mc, startMailDaemon(mc))
	}

	c.OnShutdown(func() error {
		if moh, ok := c.ServerBlockStorage.(*handler); ok {
			if moh.reqPipe != nil {
				close(moh.reqPipe)
				moh.reqPipe = nil
			}
		}
		return nil
	})

	fmt.Printf("%#v\n\n", c)

	if moh, ok := c.ServerBlockStorage.(*handler); ok { // moh = mailOutHandler ;-)
		httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
			moh.Next = next
			return moh
		})
		return nil
	}
	return errors.New("[mailout] Could not create the middleware handler")
}