2018-10-25 15:51:47 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-08 14:51:44 +02:00
|
|
|
"errors"
|
2018-10-25 15:51:47 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-07-08 14:51:44 +02:00
|
|
|
"os"
|
2019-06-26 12:52:47 +02:00
|
|
|
"strings"
|
2018-10-25 15:51:47 +02:00
|
|
|
|
2020-03-07 16:07:48 +01:00
|
|
|
"github.com/gofrs/uuid"
|
2018-10-25 15:51:47 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-03-07 19:33:22 +01:00
|
|
|
goyesqlx "github.com/knadh/goyesql/v2/sqlx"
|
2018-10-25 15:51:47 +02:00
|
|
|
"github.com/knadh/listmonk/models"
|
2020-03-07 19:33:22 +01:00
|
|
|
"github.com/knadh/stuffbin"
|
2019-06-26 12:52:47 +02:00
|
|
|
"github.com/lib/pq"
|
2018-10-25 15:51:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// install runs the first time setup of creating and
|
|
|
|
// migrating the database and creating the super user.
|
2020-08-03 15:32:23 +02:00
|
|
|
func install(lastVer string, db *sqlx.DB, fs stuffbin.FileSystem, prompt bool) {
|
2020-03-07 19:33:22 +01:00
|
|
|
qMap, _ := initQueries(queryFilePath, db, fs, false)
|
|
|
|
|
2019-01-03 12:18:47 +01:00
|
|
|
fmt.Println("")
|
2020-08-03 15:32:23 +02:00
|
|
|
fmt.Println("** first time installation **")
|
2019-06-26 12:52:47 +02:00
|
|
|
fmt.Printf("** IMPORTANT: This will wipe existing listmonk tables and types in the DB '%s' **",
|
2019-06-26 13:23:23 +02:00
|
|
|
ko.String("db.database"))
|
2019-01-03 12:18:47 +01:00
|
|
|
fmt.Println("")
|
2018-10-25 15:51:47 +02:00
|
|
|
|
2019-11-30 12:25:14 +01:00
|
|
|
if prompt {
|
|
|
|
var ok string
|
2020-08-03 15:32:23 +02:00
|
|
|
fmt.Print("continue (y/n)? ")
|
2019-11-30 12:25:14 +01:00
|
|
|
if _, err := fmt.Scanf("%s", &ok); err != nil {
|
2020-08-03 15:32:23 +02:00
|
|
|
lo.Fatalf("error reading value from terminal: %v", err)
|
2019-11-30 12:25:14 +01:00
|
|
|
}
|
|
|
|
if strings.ToLower(ok) != "y" {
|
2020-08-03 15:32:23 +02:00
|
|
|
fmt.Println("install cancelled.")
|
2019-11-30 12:25:14 +01:00
|
|
|
return
|
|
|
|
}
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate the tables.
|
2020-08-03 15:32:23 +02:00
|
|
|
err := installSchema(lastVer, db, fs)
|
2018-10-25 15:51:47 +02:00
|
|
|
if err != nil {
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Fatalf("Error migrating DB schema: %v", err)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the queries.
|
|
|
|
var q Queries
|
2020-03-07 19:33:22 +01:00
|
|
|
if err := goyesqlx.ScanToStruct(&q, qMap, db.Unsafe()); err != nil {
|
|
|
|
lo.Fatalf("error loading SQL queries: %v", err)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sample list.
|
2020-02-09 07:15:01 +01:00
|
|
|
var (
|
|
|
|
defList int
|
|
|
|
optinList int
|
|
|
|
)
|
|
|
|
if err := q.CreateList.Get(&defList,
|
2020-03-07 16:07:48 +01:00
|
|
|
uuid.Must(uuid.NewV4()),
|
2018-10-25 15:51:47 +02:00
|
|
|
"Default list",
|
2020-02-09 07:15:01 +01:00
|
|
|
models.ListTypePrivate,
|
2019-12-01 13:18:36 +01:00
|
|
|
models.ListOptinSingle,
|
2018-10-25 15:51:47 +02:00
|
|
|
pq.StringArray{"test"},
|
|
|
|
); err != nil {
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Fatalf("Error creating list: %v", err)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 16:07:48 +01:00
|
|
|
if err := q.CreateList.Get(&optinList, uuid.Must(uuid.NewV4()),
|
2020-02-09 07:15:01 +01:00
|
|
|
"Opt-in list",
|
|
|
|
models.ListTypePublic,
|
|
|
|
models.ListOptinDouble,
|
|
|
|
pq.StringArray{"test"},
|
|
|
|
); err != nil {
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Fatalf("Error creating list: %v", err)
|
2020-02-09 07:15:01 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 15:51:47 +02:00
|
|
|
// Sample subscriber.
|
|
|
|
if _, err := q.UpsertSubscriber.Exec(
|
2020-03-07 16:07:48 +01:00
|
|
|
uuid.Must(uuid.NewV4()),
|
2019-07-04 11:17:33 +02:00
|
|
|
"john@example.com",
|
|
|
|
"John Doe",
|
|
|
|
`{"type": "known", "good": true, "city": "Bengaluru"}`,
|
2020-02-09 07:15:01 +01:00
|
|
|
pq.Int64Array{int64(defList)},
|
2020-07-05 18:05:17 +02:00
|
|
|
true); err != nil {
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Fatalf("Error creating subscriber: %v", err)
|
2020-02-09 07:15:01 +01:00
|
|
|
}
|
|
|
|
if _, err := q.UpsertSubscriber.Exec(
|
2020-03-07 16:07:48 +01:00
|
|
|
uuid.Must(uuid.NewV4()),
|
2020-02-09 07:15:01 +01:00
|
|
|
"anon@example.com",
|
|
|
|
"Anon Doe",
|
|
|
|
`{"type": "unknown", "good": true, "city": "Bengaluru"}`,
|
|
|
|
pq.Int64Array{int64(optinList)},
|
2020-07-05 18:05:17 +02:00
|
|
|
true); err != nil {
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Fatalf("Error creating subscriber: %v", err)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default template.
|
2020-03-08 06:34:34 +01:00
|
|
|
tplBody, err := ioutil.ReadFile("static/email-templates/default.tpl")
|
2018-10-25 15:51:47 +02:00
|
|
|
if err != nil {
|
2018-10-29 10:50:11 +01:00
|
|
|
tplBody = []byte(tplTag)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var tplID int
|
|
|
|
if err := q.CreateTemplate.Get(&tplID,
|
|
|
|
"Default template",
|
|
|
|
string(tplBody),
|
|
|
|
); err != nil {
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Fatalf("error creating default template: %v", err)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
if _, err := q.SetDefaultTemplate.Exec(tplID); err != nil {
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Fatalf("error setting default template: %v", err)
|
2019-07-04 11:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sample campaign.
|
2020-03-07 16:07:48 +01:00
|
|
|
if _, err := q.CreateCampaign.Exec(uuid.Must(uuid.NewV4()),
|
2020-02-09 07:15:01 +01:00
|
|
|
models.CampaignTypeRegular,
|
2019-07-04 11:17:33 +02:00
|
|
|
"Test campaign",
|
|
|
|
"Welcome to listmonk",
|
2019-07-16 16:21:41 +02:00
|
|
|
"No Reply <noreply@yoursite.com>",
|
2019-07-04 11:17:33 +02:00
|
|
|
`<h3>Hi {{ .Subscriber.FirstName }}!</h3>
|
|
|
|
This is a test e-mail campaign. Your second name is {{ .Subscriber.LastName }} and you are from {{ .Subscriber.Attribs.city }}.`,
|
|
|
|
"richtext",
|
2020-08-01 13:15:29 +02:00
|
|
|
nil,
|
2019-07-04 11:17:33 +02:00
|
|
|
pq.StringArray{"test-campaign"},
|
|
|
|
"email",
|
|
|
|
1,
|
|
|
|
pq.Int64Array{1},
|
|
|
|
); err != nil {
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Fatalf("error creating sample campaign: %v", err)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 19:33:22 +01:00
|
|
|
lo.Printf("Setup complete")
|
|
|
|
lo.Printf(`Run the program and access the dashboard at %s`, ko.MustString("app.address"))
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 15:32:23 +02:00
|
|
|
// installSchema executes the SQL schema and creates the necessary tables and types.
|
|
|
|
func installSchema(curVer string, db *sqlx.DB, fs stuffbin.FileSystem) error {
|
2020-03-07 19:33:22 +01:00
|
|
|
q, err := fs.Read("/schema.sql")
|
2018-10-25 15:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-03 15:32:23 +02:00
|
|
|
if _, err := db.Exec(string(q)); err != nil {
|
2018-10-25 15:51:47 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-03 15:32:23 +02:00
|
|
|
// Insert the current migration version.
|
|
|
|
return recordMigrationVersion(curVer, db)
|
|
|
|
}
|
|
|
|
|
|
|
|
// recordMigrationVersion inserts the given version (of DB migration) into the
|
|
|
|
// `migrations` array in the settings table.
|
|
|
|
func recordMigrationVersion(ver string, db *sqlx.DB) error {
|
|
|
|
_, err := db.Exec(fmt.Sprintf(`INSERT INTO settings (key, value)
|
|
|
|
VALUES('migrations', '["%s"]'::JSONB)
|
|
|
|
ON CONFLICT (key) DO UPDATE SET value = settings.value || EXCLUDED.value`, ver))
|
|
|
|
return err
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
2019-07-08 14:51:44 +02:00
|
|
|
|
|
|
|
func newConfigFile() error {
|
|
|
|
if _, err := os.Stat("config.toml"); !os.IsNotExist(err) {
|
|
|
|
return errors.New("config.toml exists. Remove it to generate a new one")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the static file system into which all
|
|
|
|
// required static assets (.sql, .js files etc.) are loaded.
|
2020-03-14 16:37:14 +01:00
|
|
|
fs := initFS("")
|
2019-07-08 14:51:44 +02:00
|
|
|
b, err := fs.Read("config.toml.sample")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading sample config (is binary stuffed?): %v", err)
|
|
|
|
}
|
|
|
|
|
2019-10-25 07:41:47 +02:00
|
|
|
return ioutil.WriteFile("config.toml", b, 0644)
|
2019-07-08 14:51:44 +02:00
|
|
|
}
|