2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-08 11:00:14 +00:00
|
|
|
"context"
|
2018-10-25 13:51:47 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-07-08 11:00:14 +00:00
|
|
|
"os/signal"
|
2018-11-02 18:27:07 +00:00
|
|
|
"strings"
|
2020-07-08 11:00:14 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2018-10-25 13:51:47 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2019-06-26 11:23:23 +00:00
|
|
|
"github.com/knadh/koanf"
|
2019-10-01 10:49:40 +00:00
|
|
|
"github.com/knadh/koanf/providers/env"
|
2020-03-07 18:47:54 +00:00
|
|
|
"github.com/knadh/listmonk/internal/manager"
|
|
|
|
"github.com/knadh/listmonk/internal/media"
|
|
|
|
"github.com/knadh/listmonk/internal/messenger"
|
|
|
|
"github.com/knadh/listmonk/internal/subimporter"
|
2019-01-03 11:18:47 +00:00
|
|
|
"github.com/knadh/stuffbin"
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// App contains the "global" components that are
|
|
|
|
// passed around, especially through HTTP handlers.
|
|
|
|
type App struct {
|
2020-03-07 18:33:22 +00:00
|
|
|
fs stuffbin.FileSystem
|
|
|
|
db *sqlx.DB
|
|
|
|
queries *Queries
|
|
|
|
constants *constants
|
|
|
|
manager *manager.Manager
|
|
|
|
importer *subimporter.Importer
|
|
|
|
messenger messenger.Messenger
|
|
|
|
media media.Store
|
|
|
|
notifTpls *template.Template
|
|
|
|
log *log.Logger
|
2020-07-08 11:00:14 +00:00
|
|
|
|
|
|
|
// Channel for passing reload signals.
|
|
|
|
sigChan chan os.Signal
|
|
|
|
|
|
|
|
// Global variable that stores the state indicating that a restart is required
|
|
|
|
// after a settings update.
|
|
|
|
needsRestart bool
|
|
|
|
sync.Mutex
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 11:23:23 +00:00
|
|
|
var (
|
2020-03-07 18:33:22 +00:00
|
|
|
lo = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
|
2019-06-26 11:23:23 +00:00
|
|
|
ko = koanf.New(".")
|
2019-07-08 12:51:44 +00:00
|
|
|
|
2020-07-08 11:00:14 +00:00
|
|
|
fs stuffbin.FileSystem
|
|
|
|
db *sqlx.DB
|
|
|
|
queries *Queries
|
|
|
|
|
2019-07-08 12:51:44 +00:00
|
|
|
buildString string
|
2019-06-26 11:23:23 +00:00
|
|
|
)
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-07-08 12:51:44 +00:00
|
|
|
func init() {
|
2020-08-03 13:32:23 +00:00
|
|
|
lo.Println(buildString)
|
2020-07-08 11:00:14 +00:00
|
|
|
initFlags()
|
2019-06-26 11:23:23 +00:00
|
|
|
|
2019-07-08 12:51:44 +00:00
|
|
|
// Display version.
|
2020-07-08 11:00:14 +00:00
|
|
|
if ko.Bool("version") {
|
2019-07-08 12:51:44 +00:00
|
|
|
fmt.Println(buildString)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate new config.
|
2020-07-08 11:00:14 +00:00
|
|
|
if ko.Bool("new-config") {
|
2019-07-08 12:51:44 +00:00
|
|
|
if err := newConfigFile(); err != nil {
|
2020-03-07 18:33:22 +00:00
|
|
|
lo.Println(err)
|
2019-07-08 12:51:44 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-03-07 18:33:22 +00:00
|
|
|
lo.Println("generated config.toml. Edit and run --install")
|
2019-07-08 12:51:44 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:00:14 +00:00
|
|
|
// Load config files to pick up the database settings first.
|
|
|
|
initConfigFiles(ko.Strings("config"), ko)
|
|
|
|
|
|
|
|
// Connect to the database, load the filesystem to read SQL queries.
|
|
|
|
db = initDB()
|
|
|
|
fs = initFS(ko.String("static-dir"))
|
|
|
|
|
|
|
|
// Installer mode? This runs before the SQL queries are loaded and prepared
|
|
|
|
// as the installer needs to work on an empty DB.
|
|
|
|
if ko.Bool("install") {
|
2020-08-03 13:32:23 +00:00
|
|
|
// Save the version of the last listed migration.
|
|
|
|
install(migList[len(migList)-1].version, db, fs, !ko.Bool("yes"))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if ko.Bool("upgrade") {
|
|
|
|
upgrade(db, fs, !ko.Bool("yes"))
|
|
|
|
os.Exit(0)
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2020-03-07 18:33:22 +00:00
|
|
|
|
2020-08-03 13:32:23 +00:00
|
|
|
// Before the queries are prepared, see if there are pending upgrades.
|
|
|
|
checkUpgrade(db)
|
|
|
|
|
2020-07-08 11:00:14 +00:00
|
|
|
// Load the SQL queries from the filesystem.
|
|
|
|
_, queries := initQueries(queryFilePath, db, fs, true)
|
|
|
|
|
|
|
|
// Load settings from DB.
|
|
|
|
initSettings(queries)
|
|
|
|
|
2019-10-01 10:49:40 +00: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 {
|
2020-03-07 18:33:22 +00:00
|
|
|
lo.Fatalf("error loading config from env: %v", err)
|
2019-10-01 10:49:40 +00:00
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 18:33:22 +00:00
|
|
|
func main() {
|
|
|
|
// Initialize the main app controller that wraps all of the app's
|
|
|
|
// components. This is passed around HTTP handlers.
|
|
|
|
app := &App{
|
|
|
|
fs: fs,
|
|
|
|
db: db,
|
|
|
|
constants: initConstants(),
|
|
|
|
media: initMediaStore(),
|
|
|
|
log: lo,
|
|
|
|
}
|
|
|
|
_, app.queries = initQueries(queryFilePath, db, fs, true)
|
2020-03-08 10:08:47 +00:00
|
|
|
app.manager = initCampaignManager(app.queries, app.constants, app)
|
|
|
|
app.importer = initImporter(app.queries, db, app)
|
2020-03-07 18:33:22 +00:00
|
|
|
app.messenger = initMessengers(app.manager)
|
|
|
|
app.notifTpls = initNotifTemplates("/email-templates/*.html", fs, app.constants)
|
|
|
|
|
2020-03-08 06:57:41 +00:00
|
|
|
// Start the campaign workers. The campaign batches (fetch from DB, push out
|
|
|
|
// messages) get processed at the specified interval.
|
2020-03-07 18:33:22 +00:00
|
|
|
go app.manager.Run(time.Second * 5)
|
|
|
|
|
2020-07-08 11:00:14 +00:00
|
|
|
// Start the app server.
|
|
|
|
srv := initHTTPServer(app)
|
|
|
|
|
|
|
|
// Wait for the reload signal with a callback to gracefully shut down resources.
|
|
|
|
// The `wait` channel is passed to awaitReload to wait for the callback to finish
|
|
|
|
// within N seconds, or do a force reload.
|
|
|
|
app.sigChan = make(chan os.Signal)
|
|
|
|
signal.Notify(app.sigChan, syscall.SIGHUP)
|
|
|
|
|
|
|
|
closerWait := make(chan bool)
|
|
|
|
<-awaitReload(app.sigChan, closerWait, func() {
|
|
|
|
// Stop the HTTP server.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
srv.Shutdown(ctx)
|
|
|
|
|
|
|
|
// Close the campaign manager.
|
|
|
|
app.manager.Close()
|
|
|
|
|
|
|
|
// Close the DB pool.
|
|
|
|
app.db.DB.Close()
|
|
|
|
|
|
|
|
// Close the messenger pool.
|
|
|
|
app.messenger.Close()
|
|
|
|
|
|
|
|
// Signal the close.
|
|
|
|
closerWait <- true
|
|
|
|
})
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|