<commit_before>// Copyright 2016 <NAME>. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.

package multipass

// Add the multipass directive to the caddy source in order to build the plugin.
// source: github.com/mholt/caddy/caddyhttp/httpserver/plugin.go

import (
	"fmt"
	"time"

	"github.com/mholt/caddy"
	"github.com/mholt/caddy/caddyhttp/httpserver"
	"github.com/namsral/multipass"
	"github.com/namsral/multipass/services/email"
)

const directive = "multipass"

func init() {
	caddy.RegisterPlugin(directive, caddy.Plugin{
		ServerType: "http",
		Action:     setup,
	})
}

func setup(c *caddy.Controller) error {
	cfg := httpserver.GetConfig(c)
	c.OnStartup(func() error {
		fmt.Printf("%s for %s is initialized\n", directive, cfg.Addr.String())
		return nil
	})

	rules, err := parse(c)
	if err != nil || len(rules) != 1 {
		return c.Err("invalid directive")
	}
	rule := rules[0]

	m, err := multipass.New(cfg.Addr.String())
	if err != nil {
		return err
	}
	if p := rule.BasePath; len(p) > 0 {
		m.SetBasePath(rule.BasePath)
	}

	// Create an email UserService
	service, err := email.NewUserService(email.Options{
		SMTPAddr: rule.SMTPAddr,
		SMTPUser: rule.SMTPUser,
		SMTPPass: rule.SMTPPass,
		FromAddr: rule.MailFrom,
	})
	if err != nil {
		return err
	}
	for _, v := range rule.Handles {
		if err := service.AddHandle(v); err != nil {
			return err
		}
	}
	for _, v := range rule.Resources {
		if err := service.AddResource(v); err != nil {
			return err
		}
	}
	m.SetUserService(service)

	if rule.Expires > 0 {
		m.Expires = rule.Expires
	}

	mid := func(next httpserver.Handler) httpserver.Handler {
		return &Auth{
			Multipass: m,
			Next:      next,
		}
	}
	cfg.AddMiddleware(mid)

	c.OnShutdown(func() error {
		//TODO: Fix close method on private field
		// return multipass.UserService.Close()
		return nil
	})

	return nil<commit_msg>Fix unset default for the resources parameter<commit_after>// Copyright 2016 <NAME>. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.

package multipass

// Add the multipass directive to the caddy source in order to build the plugin.
// source: github.com/mholt/caddy/caddyhttp/httpserver/plugin.go

import (
	"fmt"
	"time"

	"github.com/mholt/caddy"
	"github.com/mholt/caddy/caddyhttp/httpserver"
	"github.com/namsral/multipass"
	"github.com/namsral/multipass/services/email"
)

const directive = "multipass"

func init() {
	caddy.RegisterPlugin(directive, caddy.Plugin{
		ServerType: "http",
		Action:     setup,
	})
}

func setup(c *caddy.Controller) error {
	cfg := httpserver.GetConfig(c)
	c.OnStartup(func() error {
		fmt.Printf("%s for %s is initialized\n", directive, cfg.Addr.String())
		return nil
	})

	rules, err := parse(c)
	if err != nil || len(rules) != 1 {
		return c.Err("invalid directive")
	}
	rule := rules[0]

	m, err := multipass.New(cfg.Addr.String())
	if err != nil {
		return err
	}
	if p := rule.BasePath; len(p) > 0 {
		m.SetBasePath(rule.BasePath)
	}

	// Create an email UserService
	service, err := email.NewUserService(email.Options{
		SMTPAddr: rule.SMTPAddr,
		SMTPUser: rule.SMTPUser,
		SMTPPass: rule.SMTPPass,
		FromAddr: rule.MailFrom,
	})
	if err != nil {
		return err
	}
	for _, v := range rule.Handles {
		if err := service.AddHandle(v); err != nil {
			return err
		}
	}
	if len(rule.Resources) > 0 {
		for _, v := range rule.Resources {
			if err := service.AddResource(v); err != nil {
				return err
			}
		}
	} else {
		service.AddResource("/")
	}
	m.SetUserService(service)

	if rule.Expires > 0 {
		m.Expires = rule.Expires
	}

	mid := func(next httpserver.Handler) httpserver.Handler {
		return &Auth{
			Multipass: m,
			Next:      next,
		}
	}
	cfg.AddMiddleware(mid)

	c.OnShutdown(func() error {
		//TODO: Fix close method on private field
		// return multipass.UserService.Close()
		return nil
	})

	return nil
{'fraction_non_alphanumeric': 0.13511469624401312, 'fraction_numerical': 0.004285354171918326, 'mean_word_length': 3.408888888888889, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2523213', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1441, 'n_words': 370}