Exemplo n.º 1
0
Arquivo: ape.go Projeto: groob/ape
// Setup creates a caddy middleware
func Setup(c *setup.Controller) (middleware.Middleware, error) {
	repoPath, err := parseRepo(c)
	if err != nil {
		return nil, err
	}
	paths := []string{"/api"}

	// Runs on Caddy startup, useful for services or other setups.
	c.Startup = append(c.Startup, func() error {
		fmt.Println("api middleware is initiated")
		return nil
	})

	// Runs on Caddy shutdown, useful for cleanups.
	c.Shutdown = append(c.Shutdown, func() error {
		fmt.Println("api middleware is cleaning up")
		return nil
	})

	return func(next middleware.Handler) middleware.Handler {

		h := &handler{
			Paths: paths,
			Next:  next,
		}
		repo := api.SimpleRepo(repoPath)
		server := api.NewServer(repo)
		h.apihandler = server
		return h
	}, nil
}