示例#1
0
func GetHandlers(cfg *periwinkle.Cfg) locale.Error {
	cfg.DomainHandlers = map[string]periwinkle.DomainHandler{
		"sms.gateway":   HandleSMS,
		"mms.gateway":   HandleMMS,
		cfg.GroupDomain: HandleEmail,
	}
	return nil
}
示例#2
0
func CreateTempDB() *periwinkle.Cfg {
	conf := periwinkle.Cfg{
		Mailstore:      "./Maildir",
		WebUIDir:       "./www",
		Debug:          true,
		TrustForwarded: true,
		GroupDomain:    "localhost",
		WebRoot:        "http://locahost:8080",
		DB:             nil, // the default DB is set later
	}

	db, err := cfg.OpenDB("sqlite3", "file:temp.sqlite?mode=memory&_txlock=exclusive", false)
	if err != nil {
		periwinkle.Logf("Error loading sqlite3 database")
	}
	conf.DB = db

	conf.DB.Do(func(tx *periwinkle.Tx) {
		DbSchema(tx)
	})

	return &conf
}
示例#3
0
func Parse(in io.Reader) (cfgptr *periwinkle.Cfg, e locale.Error) {
	defer func() {
		if r := recover(); r != nil {
			cfgptr = nil
			switch err := r.(type) {
			case locale.Error:
				e = err
			default:
				panic(r)
			}
		}
	}()

	// these are the defaults
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "localhost"
	}
	cfg := periwinkle.Cfg{
		Mailstore:            "./Maildir",
		WebUIDir:             "./www",
		Debug:                true,
		TrustForwarded:       true,
		TwilioAccountID:      os.Getenv("TWILIO_ACCOUNTID"),
		TwilioAuthToken:      os.Getenv("TWILIO_TOKEN"),
		GroupDomain:          "localhost",
		WebRoot:              "http://" + hostname + ":8080",
		DB:                   nil, // the default DB is set later
		DefaultDomainHandler: bounceNoHost,
	}

	datstr, err := ioutil.ReadAll(in)
	if err != nil {
		gotoError(locale.UntranslatedError(err))
	}

	var datint interface{}
	err = yaml.Unmarshal(datstr, &datint)
	if err != nil {
		gotoError(locale.UntranslatedError(err))
	}

	datmap, ok := datint.(map[interface{}]interface{})
	if !ok {
		gotoError(locale.Errorf("root element is not a map"))
	}

	var dbdriver string
	var dbsource string

	for key, val := range datmap {
		switch key {
		case "Mailstore":
			cfg.Mailstore = maildir.Maildir(getString(key.(string), val))
		case "WebUIDir":
			cfg.WebUIDir = http.Dir(getString(key.(string), val))
		case "Debug":
			cfg.Debug = getBool(key.(string), val)
		case "TrustForwarded":
			cfg.TrustForwarded = getBool(key.(string), val)
		case "TwilioAccountID":
			cfg.TwilioAccountID = getString(key.(string), val)
		case "TwilioAuthToken":
			cfg.TwilioAuthToken = getString(key.(string), val)
		case "GroupDomain":
			cfg.GroupDomain = getString(key.(string), val)
		case "WebRoot":
			cfg.WebRoot = getString(key.(string), val)
		case "DB":
			m, ok := val.(map[interface{}]interface{})
			if !ok {
				gotoError(locale.Errorf("value for %q is not a map", key.(string)))
			}
			for key, val := range m {
				switch key {
				case "driver":
					dbdriver = getString("DB."+key.(string), val)
				case "source":
					dbsource = getString("DB."+key.(string), val)
				default:
					gotoError(locale.Errorf("unknown field: %v", "DB."+key.(string)))
				}
			}
		default:
			gotoError(locale.Errorf("unknown field: %v", key))
		}
	}

	if dbdriver != "" && dbsource != "" {
		db, err := OpenDB(dbdriver, dbsource, cfg.Debug)
		if err != nil {
			gotoError(err)
		}
		cfg.DB = db
	}

	// Set the default database
	if cfg.DB == nil {
		periwinkle.Logf("DB not configured, trying MySQL periwinkle:periwinkle@localhost/periwinkle ...")
		db, err := OpenDB("mysql", "periwinkle:periwinkle@/periwinkle?charset=utf8&parseTime=True", cfg.Debug)
		if err != nil {
			periwinkle.Logf("Could not connect to MySQL: %v", locale.UntranslatedError(err))
			periwinkle.Logf("No MySQL, trying SQLite3 file:periwinkle.sqlite ...")
			db, err = OpenDB("sqlite3", "file:periwinkle.sqlite?mode=rwc&_txlock=exclusive", cfg.Debug)
			if err != nil {
				periwinkle.Logf("Could not open SQLite3 DB: %v", locale.UntranslatedError(err))
				gotoError(locale.Errorf("Could not connect to database"))
			}
		}
		cfg.DB = db
	}

	domain_handlers.GetHandlers(&cfg)

	return &cfg, nil
}