listmonk/main.go

381 lines
11 KiB
Go
Raw Normal View History

2018-10-25 15:51:47 +02:00
package main
import (
"fmt"
"html/template"
"log"
"os"
"path/filepath"
2018-11-02 19:27:07 +01:00
"strings"
2018-10-25 15:51:47 +02:00
"time"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/jmoiron/sqlx"
"github.com/knadh/goyesql"
2019-06-26 13:23:23 +02:00
"github.com/knadh/koanf"
"github.com/knadh/koanf/maps"
2019-06-26 13:23:23 +02:00
"github.com/knadh/koanf/parsers/toml"
2019-10-01 12:49:40 +02:00
"github.com/knadh/koanf/providers/env"
2019-06-26 13:23:23 +02:00
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/listmonk/manager"
"github.com/knadh/listmonk/media"
"github.com/knadh/listmonk/media/providers/filesystem"
"github.com/knadh/listmonk/media/providers/s3"
2018-10-25 15:51:47 +02:00
"github.com/knadh/listmonk/messenger"
"github.com/knadh/listmonk/subimporter"
"github.com/knadh/stuffbin"
2018-10-25 15:51:47 +02:00
"github.com/labstack/echo"
flag "github.com/spf13/pflag"
)
type constants struct {
RootURL string `koanf:"root"`
LogoURL string `koanf:"logo_url"`
FaviconURL string `koanf:"favicon_url"`
UnsubURL string
LinkTrackURL string
ViewTrackURL string
OptinURL string
FromEmail string `koanf:"from_email"`
NotifyEmails []string `koanf:"notify_emails"`
Privacy privacyOptions `koanf:"privacy"`
}
type privacyOptions struct {
AllowBlacklist bool `koanf:"allow_blacklist"`
AllowExport bool `koanf:"allow_export"`
AllowWipe bool `koanf:"allow_wipe"`
Exportable map[string]bool `koanf:"-"`
2018-10-25 15:51:47 +02:00
}
// App contains the "global" components that are
// passed around, especially through HTTP handlers.
type App struct {
Constants *constants
DB *sqlx.DB
Queries *Queries
Importer *subimporter.Importer
Manager *manager.Manager
FS stuffbin.FileSystem
2018-10-25 15:51:47 +02:00
Logger *log.Logger
NotifTpls *template.Template
2018-10-29 10:50:49 +01:00
Messenger messenger.Messenger
Media media.Store
2018-10-25 15:51:47 +02:00
}
2019-06-26 13:23:23 +02:00
var (
// Global logger.
logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
2019-06-26 13:23:23 +02:00
// Global configuration reader.
ko = koanf.New(".")
buildString string
2019-06-26 13:23:23 +02:00
)
2018-10-25 15:51:47 +02:00
func init() {
2018-10-25 15:51:47 +02:00
// Register --help handler.
2019-06-26 13:23:23 +02:00
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.Usage = func() {
fmt.Println(f.FlagUsages())
2018-10-25 15:51:47 +02:00
os.Exit(0)
}
// Setup the default configuration.
2019-06-26 13:23:23 +02:00
f.StringSlice("config", []string{"config.toml"},
2018-10-25 15:51:47 +02:00
"Path to one or more config files (will be merged in order)")
2019-06-26 13:23:23 +02:00
f.Bool("install", false, "Run first time installation")
f.Bool("version", false, "Current version of the build")
f.Bool("new-config", false, "Generate sample config file")
2019-11-30 12:25:14 +01:00
f.Bool("yes", false, "Assume 'yes' to prompts, eg: during --install")
2018-10-25 15:51:47 +02:00
// Process flags.
if err := f.Parse(os.Args[1:]); err != nil {
logger.Fatalf("error loading flags: %v", err)
}
2019-06-26 13:23:23 +02:00
// Display version.
if v, _ := f.GetBool("version"); v {
fmt.Println(buildString)
os.Exit(0)
}
// Generate new config.
if ok, _ := f.GetBool("new-config"); ok {
if err := newConfigFile(); err != nil {
2019-07-12 13:49:26 +02:00
logger.Println(err)
os.Exit(1)
}
2019-07-12 13:49:26 +02:00
logger.Println("generated config.toml. Edit and run --install")
os.Exit(0)
}
2019-06-26 13:23:23 +02:00
// Load config files.
cFiles, _ := f.GetStringSlice("config")
for _, f := range cFiles {
2019-07-12 13:49:26 +02:00
logger.Printf("reading config: %s", f)
2019-06-26 13:23:23 +02:00
if err := ko.Load(file.Provider(f), toml.Parser()); err != nil {
2019-07-12 13:49:26 +02:00
if os.IsNotExist(err) {
logger.Fatal("config file not found. If there isn't one yet, run --new-config to generate one.")
}
2019-10-01 12:49:40 +02:00
logger.Fatalf("error loadng config from file: %v.", err)
2018-10-25 15:51:47 +02:00
}
}
2019-10-01 12:49:40 +02:00
// Load environment variables and merge into the loaded config.
if err := ko.Load(env.Provider("LISTMONK_", ".", func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, "LISTMONK_")), "__", ".", -1)
}), nil); err != nil {
logger.Fatalf("error loading config from env: %v", err)
}
if err := ko.Load(posflag.Provider(f, ".", ko), nil); err != nil {
logger.Fatalf("error loading config: %v", err)
}
2018-10-25 15:51:47 +02:00
}
// initFileSystem initializes the stuffbin FileSystem to provide
// access to bunded static assets to the app.
func initFileSystem(binPath string) (stuffbin.FileSystem, error) {
fs, err := stuffbin.UnStuff(binPath)
if err == nil {
return fs, nil
}
// Running in local mode. Load the required static assets into
// the in-memory stuffbin.FileSystem.
logger.Printf("unable to initialize embedded filesystem: %v", err)
logger.Printf("using local filesystem for static assets")
files := []string{
"config.toml.sample",
"queries.sql",
"schema.sql",
"email-templates",
"public",
// The frontend app's static assets are aliased to /frontend
// so that they are accessible at localhost:port/frontend/static/ ...
"frontend/build:/frontend",
}
fs, err = stuffbin.NewLocalFS("/", files...)
if err != nil {
return nil, fmt.Errorf("failed to initialize local file for assets: %v", err)
}
return fs, nil
2018-10-25 15:51:47 +02:00
}
// initMessengers initializes various messaging backends.
func initMessengers(r *manager.Manager) messenger.Messenger {
2018-10-25 15:51:47 +02:00
// Load SMTP configurations for the default e-mail Messenger.
var (
mapKeys = ko.MapKeys("smtp")
srv = make([]messenger.Server, 0, len(mapKeys))
)
for _, name := range mapKeys {
2019-06-26 13:23:23 +02:00
if !ko.Bool(fmt.Sprintf("smtp.%s.enabled", name)) {
logger.Printf("skipped SMTP: %s", name)
2018-10-25 15:51:47 +02:00
continue
}
var s messenger.Server
if err := ko.Unmarshal("smtp."+name, &s); err != nil {
logger.Fatalf("error loading SMTP: %v", err)
}
2018-10-25 15:51:47 +02:00
s.Name = name
s.SendTimeout *= time.Millisecond
2018-10-25 15:51:47 +02:00
srv = append(srv, s)
logger.Printf("loaded SMTP: %s (%s@%s)", s.Name, s.Username, s.Host)
2018-10-25 15:51:47 +02:00
}
2018-10-29 10:50:49 +01:00
msgr, err := messenger.NewEmailer(srv...)
2018-10-25 15:51:47 +02:00
if err != nil {
logger.Fatalf("error loading e-mail messenger: %v", err)
}
2018-10-29 10:50:49 +01:00
if err := r.AddMessenger(msgr); err != nil {
2018-10-25 15:51:47 +02:00
logger.Printf("error registering messenger %s", err)
}
2018-10-29 10:50:49 +01:00
return msgr
2018-10-25 15:51:47 +02:00
}
// initMediaStore initializes Upload manager with a custom backend.
func initMediaStore() media.Store {
switch provider := ko.String("upload.provider"); provider {
case "s3":
var opts s3.Opts
ko.Unmarshal("upload.s3", &opts)
uplder, err := s3.NewS3Store(opts)
if err != nil {
logger.Fatalf("error initializing s3 upload provider %s", err)
}
return uplder
case "filesystem":
var opts filesystem.Opts
ko.Unmarshal("upload.filesystem", &opts)
opts.UploadPath = filepath.Clean(opts.UploadPath)
opts.UploadURI = filepath.Clean(opts.UploadURI)
uplder, err := filesystem.NewDiskStore(opts)
if err != nil {
logger.Fatalf("error initializing filesystem upload provider %s", err)
}
return uplder
default:
logger.Fatalf("unknown provider. please select one of either filesystem or s3")
}
return nil
}
2018-10-25 15:51:47 +02:00
func main() {
// Connect to the DB.
2019-12-05 12:58:22 +01:00
var dbCfg dbConf
if err := ko.Unmarshal("db", &dbCfg); err != nil {
log.Fatalf("error loading db config: %v", err)
}
db, err := connectDB(dbCfg)
2018-10-25 15:51:47 +02:00
if err != nil {
logger.Fatalf("error connecting to DB: %v", err)
}
defer db.Close()
var c constants
if err := ko.Unmarshal("app", &c); err != nil {
log.Fatalf("error loading app config: %v", err)
}
if err := ko.Unmarshal("privacy", &c.Privacy); err != nil {
log.Fatalf("error loading app config: %v", err)
}
2018-11-02 19:27:07 +01:00
c.RootURL = strings.TrimRight(c.RootURL, "/")
c.Privacy.Exportable = maps.StringSliceToLookupMap(ko.Strings("privacy.exportable"))
// Initialize the static file system into which all
// required static assets (.sql, .js files etc.) are loaded.
fs, err := initFileSystem(os.Args[0])
if err != nil {
logger.Fatal(err)
}
2018-10-25 15:51:47 +02:00
// Initialize the app context that's passed around.
app := &App{
Constants: &c,
DB: db,
Logger: logger,
FS: fs,
2018-10-25 15:51:47 +02:00
}
// Load SQL queries.
qB, err := fs.Read("/queries.sql")
if err != nil {
logger.Fatalf("error reading queries.sql: %v", err)
}
qMap, err := goyesql.ParseBytes(qB)
2018-10-25 15:51:47 +02:00
if err != nil {
logger.Fatalf("error parsing SQL queries: %v", err)
2018-10-25 15:51:47 +02:00
}
// Run the first time installation.
2019-06-26 13:23:23 +02:00
if ko.Bool("install") {
2019-11-30 12:25:14 +01:00
install(app, qMap, !ko.Bool("yes"))
2018-10-25 15:51:47 +02:00
return
}
// Map queries to the query container.
q := &Queries{}
if err := scanQueriesToStruct(q, qMap, db.Unsafe()); err != nil {
logger.Fatalf("no SQL queries loaded: %v", err)
}
app.Queries = q
// Initialize the bulk subscriber importer.
importNotifCB := func(subject string, data interface{}) error {
go sendNotification(app.Constants.NotifyEmails, subject, notifTplImport, data, app)
return nil
}
app.Importer = subimporter.New(q.UpsertSubscriber.Stmt,
q.UpsertBlacklistSubscriber.Stmt,
q.UpdateListsDate.Stmt,
db.DB,
importNotifCB)
// Prepare notification e-mail templates.
notifTpls, err := compileNotifTpls("/email-templates/*.html", fs, app)
if err != nil {
logger.Fatalf("error loading e-mail notification templates: %v", err)
}
app.NotifTpls = notifTpls
2018-10-25 15:51:47 +02:00
// Static URLS.
// url.com/subscription/{campaign_uuid}/{subscriber_uuid}
c.UnsubURL = fmt.Sprintf("%s/subscription/%%s/%%s", app.Constants.RootURL)
// url.com/subscription/optin/{subscriber_uuid}
c.OptinURL = fmt.Sprintf("%s/subscription/optin/%%s?%%s", app.Constants.RootURL)
// url.com/link/{campaign_uuid}/{subscriber_uuid}/{link_uuid}
c.LinkTrackURL = fmt.Sprintf("%s/link/%%s/%%s/%%s", app.Constants.RootURL)
// url.com/campaign/{campaign_uuid}/{subscriber_uuid}/px.png
c.ViewTrackURL = fmt.Sprintf("%s/campaign/%%s/%%s/px.png", app.Constants.RootURL)
// Initialize the campaign manager.
campNotifCB := func(subject string, data interface{}) error {
return sendNotification(app.Constants.NotifyEmails, subject, notifTplCampaign, data, app)
}
m := manager.New(manager.Config{
2019-06-26 13:23:23 +02:00
Concurrency: ko.Int("app.concurrency"),
MaxSendErrors: ko.Int("app.max_send_errors"),
FromEmail: app.Constants.FromEmail,
UnsubURL: c.UnsubURL,
OptinURL: c.OptinURL,
LinkTrackURL: c.LinkTrackURL,
ViewTrackURL: c.ViewTrackURL,
}, newManagerDB(q), campNotifCB, logger)
app.Manager = m
2018-10-25 15:51:47 +02:00
// Add messengers.
app.Messenger = initMessengers(app.Manager)
2018-10-25 15:51:47 +02:00
// Add uploader
app.Media = initMediaStore()
// Initialize the workers that push out messages.
go m.Run(time.Second * 5)
m.SpawnWorkers()
2018-10-25 15:51:47 +02:00
// Initialize the HTTP server.
2018-10-25 15:51:47 +02:00
var srv = echo.New()
srv.HideBanner = true
// Register app (*App) to be injected into all HTTP handlers.
2018-10-25 15:51:47 +02:00
srv.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set("app", app)
return next(c)
}
})
// Parse user facing templates.
2019-07-05 10:38:48 +02:00
tpl, err := stuffbin.ParseTemplatesGlob(nil, fs, "/public/templates/*.html")
2018-10-25 15:51:47 +02:00
if err != nil {
logger.Fatalf("error parsing public templates: %v", err)
}
srv.Renderer = &tplRenderer{
templates: tpl,
RootURL: c.RootURL,
LogoURL: c.LogoURL,
FaviconURL: c.FaviconURL}
// Register HTTP handlers and static file servers.
fSrv := app.FS.FileServer()
srv.GET("/public/*", echo.WrapHandler(fSrv))
srv.GET("/frontend/*", echo.WrapHandler(fSrv))
if ko.String("upload.provider") == "filesystem" {
srv.Static(ko.String("upload.filesystem.upload_uri"), ko.String("upload.filesystem.upload_path"))
}
2018-10-25 15:51:47 +02:00
registerHandlers(srv)
2019-06-26 13:23:23 +02:00
srv.Logger.Fatal(srv.Start(ko.String("app.address")))
2018-10-25 15:51:47 +02:00
}