Exemple #1
0
Fichier : ape.go Projet : 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
}
Exemple #2
0
func main() {
	var opts api.ServerOptions
	// add jwt auth
	if *flJWT {
		jwtOpt := api.JWTAuth(*flJWTSecret)
		opts = append(opts, jwtOpt)
	}
	// add basic auth
	if *flBasic {
		opts = append(opts, api.BasicAuth())
	}
	// configure repo
	repo := api.SimpleRepo(*flRepo)
	opts = append(opts, repo)
	// create handler
	apiHandler := api.NewServer(opts...)
	http.Handle("/", apiHandler)
	// serve http or https
	serve()
}