2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-02 18:27:07 +00:00
|
|
|
"strings"
|
2018-10-25 13:51:47 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/knadh/goyesql"
|
2019-06-26 11:23:23 +00:00
|
|
|
"github.com/knadh/koanf"
|
|
|
|
"github.com/knadh/koanf/parsers/toml"
|
|
|
|
"github.com/knadh/koanf/providers/file"
|
|
|
|
"github.com/knadh/koanf/providers/posflag"
|
2018-12-19 06:33:13 +00:00
|
|
|
"github.com/knadh/listmonk/manager"
|
2018-10-25 13:51:47 +00:00
|
|
|
"github.com/knadh/listmonk/messenger"
|
|
|
|
"github.com/knadh/listmonk/subimporter"
|
2019-01-03 11:18:47 +00:00
|
|
|
"github.com/knadh/stuffbin"
|
2018-10-25 13:51:47 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
flag "github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
type constants struct {
|
2018-11-28 07:59:57 +00:00
|
|
|
RootURL string `mapstructure:"root"`
|
2019-01-03 11:18:47 +00:00
|
|
|
LogoURL string `mapstructure:"logo_url"`
|
|
|
|
FaviconURL string `mapstructure:"favicon_url"`
|
2018-11-28 07:59:57 +00:00
|
|
|
UploadPath string `mapstructure:"upload_path"`
|
|
|
|
UploadURI string `mapstructure:"upload_uri"`
|
|
|
|
FromEmail string `mapstructure:"from_email"`
|
|
|
|
NotifyEmails []string `mapstructure:"notify_emails"`
|
2018-10-25 13:51:47 +00: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
|
2018-12-19 06:33:13 +00:00
|
|
|
Manager *manager.Manager
|
2019-01-03 11:18:47 +00:00
|
|
|
FS stuffbin.FileSystem
|
2018-10-25 13:51:47 +00:00
|
|
|
Logger *log.Logger
|
2018-11-28 07:59:57 +00:00
|
|
|
NotifTpls *template.Template
|
2018-10-29 09:50:49 +00:00
|
|
|
Messenger messenger.Messenger
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 11:23:23 +00:00
|
|
|
var (
|
|
|
|
// Global logger.
|
|
|
|
logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
|
2018-11-28 07:59:57 +00:00
|
|
|
|
2019-06-26 11:23:23 +00:00
|
|
|
// Global configuration reader.
|
|
|
|
ko = koanf.New(".")
|
|
|
|
)
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-06-26 11:23:23 +00:00
|
|
|
func initConfig(ko *koanf.Koanf) error {
|
2018-10-25 13:51:47 +00:00
|
|
|
// Register --help handler.
|
2019-06-26 11:23:23 +00:00
|
|
|
f := flag.NewFlagSet("config", flag.ContinueOnError)
|
|
|
|
f.Usage = func() {
|
|
|
|
fmt.Println(f.FlagUsages())
|
2018-10-25 13:51:47 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the default configuration.
|
2019-06-26 11:23:23 +00:00
|
|
|
f.StringSlice("config", []string{"config.toml"},
|
2018-10-25 13:51:47 +00:00
|
|
|
"Path to one or more config files (will be merged in order)")
|
2019-06-26 11:23:23 +00:00
|
|
|
f.Bool("install", false, "Run first time installation")
|
|
|
|
f.Bool("version", false, "Current version of the build")
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
// Process flags.
|
2019-06-26 11:23:23 +00:00
|
|
|
f.Parse(os.Args[1:])
|
|
|
|
|
|
|
|
// Load config files.
|
|
|
|
cFiles, _ := f.GetStringSlice("config")
|
|
|
|
for _, f := range cFiles {
|
|
|
|
log.Printf("reading config: %s", f)
|
|
|
|
if err := ko.Load(file.Provider(f), toml.Parser()); err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-26 11:23:23 +00:00
|
|
|
ko.Load(posflag.Provider(f, ".", ko), nil)
|
|
|
|
|
|
|
|
return nil
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 11:18:47 +00: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("./listmonk")
|
|
|
|
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/ ...
|
2019-05-27 11:47:26 +00:00
|
|
|
"frontend/build:/frontend",
|
2019-01-03 11:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initMessengers initializes various messaging backends.
|
2018-12-19 06:33:13 +00:00
|
|
|
func initMessengers(r *manager.Manager) messenger.Messenger {
|
2018-10-25 13:51:47 +00:00
|
|
|
// Load SMTP configurations for the default e-mail Messenger.
|
|
|
|
var srv []messenger.Server
|
2019-06-26 11:23:23 +00:00
|
|
|
for _, name := range ko.MapKeys("smtp") {
|
|
|
|
if !ko.Bool(fmt.Sprintf("smtp.%s.enabled", name)) {
|
2019-01-03 11:18:47 +00:00
|
|
|
logger.Printf("skipped SMTP: %s", name)
|
2018-10-25 13:51:47 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var s messenger.Server
|
2019-06-26 11:23:23 +00:00
|
|
|
ko.Unmarshal("smtp."+name, &s)
|
2018-10-25 13:51:47 +00:00
|
|
|
s.Name = name
|
|
|
|
s.SendTimeout = s.SendTimeout * time.Millisecond
|
|
|
|
srv = append(srv, s)
|
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
logger.Printf("loaded SMTP: %s (%s@%s)", s.Name, s.Username, s.Host)
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 09:50:49 +00:00
|
|
|
msgr, err := messenger.NewEmailer(srv...)
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("error loading e-mail messenger: %v", err)
|
|
|
|
}
|
2018-10-29 09:50:49 +00:00
|
|
|
if err := r.AddMessenger(msgr); err != nil {
|
2018-10-25 13:51:47 +00:00
|
|
|
logger.Printf("error registering messenger %s", err)
|
|
|
|
}
|
2018-10-29 09:50:49 +00:00
|
|
|
|
|
|
|
return msgr
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-06-26 11:23:23 +00:00
|
|
|
// Load config into the global conf.
|
|
|
|
if err := initConfig(ko); err != nil {
|
|
|
|
logger.Printf("error reading config: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// Connect to the DB.
|
2019-06-26 11:23:23 +00:00
|
|
|
db, err := connectDB(ko.String("db.host"),
|
|
|
|
ko.Int("db.port"),
|
|
|
|
ko.String("db.user"),
|
|
|
|
ko.String("db.password"),
|
|
|
|
ko.String("db.database"),
|
|
|
|
ko.String("db.ssl_mode"))
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("error connecting to DB: %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
var c constants
|
2019-06-26 11:23:23 +00:00
|
|
|
ko.Unmarshal("app", &c)
|
2018-11-02 18:27:07 +00:00
|
|
|
c.RootURL = strings.TrimRight(c.RootURL, "/")
|
|
|
|
c.UploadURI = filepath.Clean(c.UploadURI)
|
2019-01-03 11:18:47 +00:00
|
|
|
c.UploadPath = filepath.Clean(c.UploadPath)
|
|
|
|
|
|
|
|
// 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 13:51:47 +00:00
|
|
|
|
|
|
|
// Initialize the app context that's passed around.
|
|
|
|
app := &App{
|
|
|
|
Constants: &c,
|
|
|
|
DB: db,
|
|
|
|
Logger: logger,
|
2019-01-03 11:18:47 +00:00
|
|
|
FS: fs,
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load SQL queries.
|
2019-01-03 11:18:47 +00:00
|
|
|
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 13:51:47 +00:00
|
|
|
if err != nil {
|
2019-01-03 11:18:47 +00:00
|
|
|
logger.Fatalf("error parsing SQL queries: %v", err)
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
// Run the first time installation.
|
2019-06-26 11:23:23 +00:00
|
|
|
if ko.Bool("install") {
|
2018-10-25 13:51:47 +00:00
|
|
|
install(app, qMap)
|
|
|
|
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
|
2018-11-28 07:59:57 +00:00
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
// Initialize the bulk subscriber importer.
|
2018-11-28 07:59:57 +00:00
|
|
|
importNotifCB := func(subject string, data map[string]interface{}) error {
|
2019-05-14 16:48:18 +00:00
|
|
|
go sendNotification(notifTplImport, subject, data, app)
|
|
|
|
return nil
|
2018-11-28 07:59:57 +00:00
|
|
|
}
|
|
|
|
app.Importer = subimporter.New(q.UpsertSubscriber.Stmt,
|
2018-12-18 05:24:55 +00:00
|
|
|
q.UpsertBlacklistSubscriber.Stmt,
|
2019-05-14 16:36:14 +00:00
|
|
|
q.UpdateListsDate.Stmt,
|
2018-11-28 07:59:57 +00:00
|
|
|
db.DB,
|
|
|
|
importNotifCB)
|
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
// Read system e-mail templates.
|
|
|
|
notifTpls, err := stuffbin.ParseTemplatesGlob(fs, "/email-templates/*.html")
|
2018-11-28 07:59:57 +00:00
|
|
|
if err != nil {
|
2019-01-03 11:18:47 +00:00
|
|
|
logger.Fatalf("error loading system e-mail templates: %v", err)
|
2018-11-28 07:59:57 +00:00
|
|
|
}
|
|
|
|
app.NotifTpls = notifTpls
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
// Initialize the campaign manager.
|
2018-11-28 07:59:57 +00:00
|
|
|
campNotifCB := func(subject string, data map[string]interface{}) error {
|
|
|
|
return sendNotification(notifTplCampaign, subject, data, app)
|
|
|
|
}
|
2018-12-19 06:33:13 +00:00
|
|
|
m := manager.New(manager.Config{
|
2019-06-26 11:23:23 +00:00
|
|
|
Concurrency: ko.Int("app.concurrency"),
|
|
|
|
MaxSendErrors: ko.Int("app.max_send_errors"),
|
2018-11-28 07:59:57 +00:00
|
|
|
FromEmail: app.Constants.FromEmail,
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
// url.com/unsubscribe/{campaign_uuid}/{subscriber_uuid}
|
|
|
|
UnsubscribeURL: fmt.Sprintf("%s/unsubscribe/%%s/%%s", app.Constants.RootURL),
|
2018-11-02 07:50:32 +00:00
|
|
|
|
2018-10-31 12:54:21 +00:00
|
|
|
// url.com/link/{campaign_uuid}/{subscriber_uuid}/{link_uuid}
|
|
|
|
LinkTrackURL: fmt.Sprintf("%s/link/%%s/%%s/%%s", app.Constants.RootURL),
|
2018-11-02 07:50:32 +00:00
|
|
|
|
|
|
|
// url.com/campaign/{campaign_uuid}/{subscriber_uuid}/px.png
|
|
|
|
ViewTrackURL: fmt.Sprintf("%s/campaign/%%s/%%s/px.png", app.Constants.RootURL),
|
2018-12-19 06:33:13 +00:00
|
|
|
}, newManagerDB(q), campNotifCB, logger)
|
|
|
|
app.Manager = m
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
// Add messengers.
|
2018-12-19 06:33:13 +00:00
|
|
|
app.Messenger = initMessengers(app.Manager)
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
// Initialize the workers that push out messages.
|
2018-12-19 06:33:13 +00:00
|
|
|
go m.Run(time.Duration(time.Second * 5))
|
|
|
|
m.SpawnWorkers()
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
// Initialize the HTTP server.
|
2018-10-25 13:51:47 +00:00
|
|
|
var srv = echo.New()
|
2019-01-03 11:18:47 +00:00
|
|
|
srv.HideBanner = true
|
|
|
|
|
|
|
|
// Register app (*App) to be injected into all HTTP handlers.
|
2018-10-25 13:51:47 +00:00
|
|
|
srv.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
c.Set("app", app)
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-01-03 11:18:47 +00:00
|
|
|
// Parse user facing templates.
|
|
|
|
tpl, err := stuffbin.ParseTemplatesGlob(fs, "/public/templates/*.html")
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("error parsing public templates: %v", err)
|
|
|
|
}
|
2019-01-03 11:18:47 +00:00
|
|
|
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))
|
|
|
|
srv.Static(c.UploadURI, c.UploadURI)
|
2018-10-25 13:51:47 +00:00
|
|
|
registerHandlers(srv)
|
2019-06-26 11:23:23 +00:00
|
|
|
srv.Logger.Fatal(srv.Start(ko.String("app.address")))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|