Commit 7ed4b4ab authored by Leonard Techel's avatar Leonard Techel
Browse files

Move the provider switch into the provider package

parent 4f948770
......@@ -25,6 +25,11 @@ type DBConfig struct {
Args string `json:"args"`
}
type ProviderConfig struct {
Provider string `json:"provider"`
Config json.RawMessage `json:"conf"`
}
type ML struct {
Id string `json:"id"`
Title map[string]string `json:"title"`
......@@ -32,9 +37,8 @@ type ML struct {
}
type MLConfig struct {
Provider string `json:"provider"`
Config json.RawMessage `json:"conf"`
Lists []ML `json:"lists"`
ProviderConfig
Lists []ML `json:"lists"`
}
type AppConfig struct {
......@@ -95,26 +99,10 @@ func CreateApp(cfg AppConfig) (app *App) {
ctx.Data["db"] = db
ctx.Data["app"] = app
})
m.Use(func(ctx *macaron.Context) {
var ml mailinglistprovider.MailinglistProvider
if app.cfg.Mailinglists.Provider == "mailman2" {
var cfg mailinglistprovider.Mailman2ProviderConfig
err := json.Unmarshal(app.cfg.Mailinglists.Config, &cfg)
if err != nil {
panic(err)
}
ml, err = mailinglistprovider.CreateMailman2Provider(cfg)
if err != nil {
panic(err)
}
}
ctx.MapTo(ml, (*mailinglistprovider.MailinglistProvider)(nil))
})
m.Use(mailinglistprovider.Mount(app.cfg.Mailinglists.Provider, app.cfg.Mailinglists.Config))
auth := remoteauthprovider.CreateOAuth2AuthProvider(m, db, app.cfg.OAuth2)
m.Group("/user", func() {
m.Combo("/login").
Get(auth.GetLogin).
......
package mailinglistprovider
import (
"encoding/json"
"gopkg.in/macaron.v1"
)
type MailinglistProvider interface {
IsSubscribed(email string) bool
Subscribe(email string)
Unsubscribe(email string)
}
// Use this function using app.Use() on your macaron app to mount auth providers
func Mount(provider string, config json.RawMessage) func(*macaron.Context) {
return func(ctx *macaron.Context) {
var ml MailinglistProvider
switch provider {
case "mailman2":
var cfg Mailman2ProviderConfig
err := json.Unmarshal(config, &cfg)
if err != nil {
panic(err)
}
ml, err = CreateMailman2Provider(cfg)
if err != nil {
panic(err)
}
break
default:
panic("unknown mailinglist provider")
}
ctx.MapTo(ml, (*MailinglistProvider)(nil))
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment