listmonk/install.go

171 lines
4.1 KiB
Go
Raw Normal View History

2018-10-25 15:51:47 +02:00
package main
import (
"errors"
2018-10-25 15:51:47 +02:00
"fmt"
"io/ioutil"
"os"
"strings"
"time"
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"
"github.com/knadh/goyesql"
"github.com/knadh/listmonk/models"
"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.
2019-11-30 12:25:14 +01:00
func install(app *App, qMap goyesql.Queries, prompt bool) {
fmt.Println("")
fmt.Println("** First time installation **")
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"))
fmt.Println("")
2018-10-25 15:51:47 +02:00
2019-11-30 12:25:14 +01:00
if prompt {
var ok string
fmt.Print("Continue (y/n)? ")
if _, err := fmt.Scanf("%s", &ok); err != nil {
logger.Fatalf("Error reading value from terminal: %v", err)
}
if strings.ToLower(ok) != "y" {
fmt.Println("Installation cancelled.")
return
}
2018-10-25 15:51:47 +02:00
}
// Migrate the tables.
err := installMigrate(app.DB, app)
2018-10-25 15:51:47 +02:00
if err != nil {
logger.Fatalf("Error migrating DB schema: %v", err)
}
// Load the queries.
var q Queries
if err := scanQueriesToStruct(&q, qMap, app.DB.Unsafe()); err != nil {
logger.Fatalf("error loading SQL queries: %v", err)
}
// Sample list.
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",
models.ListTypePrivate,
models.ListOptinSingle,
2018-10-25 15:51:47 +02:00
pq.StringArray{"test"},
); err != nil {
logger.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()),
"Opt-in list",
models.ListTypePublic,
models.ListOptinDouble,
pq.StringArray{"test"},
); err != nil {
logger.Fatalf("Error creating list: %v", err)
}
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()),
"john@example.com",
"John Doe",
`{"type": "known", "good": true, "city": "Bengaluru"}`,
pq.Int64Array{int64(defList)},
); err != nil {
logger.Fatalf("Error creating subscriber: %v", err)
}
if _, err := q.UpsertSubscriber.Exec(
2020-03-07 16:07:48 +01:00
uuid.Must(uuid.NewV4()),
"anon@example.com",
"Anon Doe",
`{"type": "unknown", "good": true, "city": "Bengaluru"}`,
pq.Int64Array{int64(optinList)},
2018-10-25 15:51:47 +02:00
); err != nil {
logger.Fatalf("Error creating subscriber: %v", err)
}
// Default template.
tplBody, err := ioutil.ReadFile("email-templates/default.tpl")
2018-10-25 15:51:47 +02:00
if err != nil {
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 {
logger.Fatalf("error creating default template: %v", err)
2018-10-25 15:51:47 +02:00
}
if _, err := q.SetDefaultTemplate.Exec(tplID); err != nil {
logger.Fatalf("error setting default template: %v", err)
}
// Sample campaign.
sendAt := time.Now()
sendAt.Add(time.Minute * 43200)
2020-03-07 16:07:48 +01:00
if _, err := q.CreateCampaign.Exec(uuid.Must(uuid.NewV4()),
models.CampaignTypeRegular,
"Test campaign",
"Welcome to listmonk",
"No Reply <noreply@yoursite.com>",
`<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",
sendAt,
pq.StringArray{"test-campaign"},
"email",
1,
pq.Int64Array{1},
); err != nil {
logger.Fatalf("error creating sample campaign: %v", err)
2018-10-25 15:51:47 +02:00
}
logger.Printf("Setup complete")
2019-07-15 17:06:08 +02:00
logger.Printf(`Run the program and access the dashboard at %s`, ko.String("app.address"))
2018-10-25 15:51:47 +02:00
}
// installMigrate executes the SQL schema and creates the necessary tables and types.
func installMigrate(db *sqlx.DB, app *App) error {
q, err := app.FS.Read("/schema.sql")
2018-10-25 15:51:47 +02:00
if err != nil {
return err
}
_, err = db.Query(string(q))
if err != nil {
return err
}
return nil
}
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.
fs, err := initFileSystem(os.Args[0])
if err != nil {
return err
}
b, err := fs.Read("config.toml.sample")
if err != nil {
return fmt.Errorf("error reading sample config (is binary stuffed?): %v", err)
}
return ioutil.WriteFile("config.toml", b, 0644)
}