2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-08 12:51:44 +00:00
|
|
|
"errors"
|
2018-10-25 13:51:47 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-07-08 12:51:44 +00:00
|
|
|
"os"
|
2019-06-26 10:52:47 +00:00
|
|
|
"strings"
|
2019-07-04 09:17:33 +00:00
|
|
|
"time"
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/knadh/goyesql"
|
|
|
|
"github.com/knadh/listmonk/models"
|
2019-06-26 10:52:47 +00:00
|
|
|
"github.com/lib/pq"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// install runs the first time setup of creating and
|
|
|
|
// migrating the database and creating the super user.
|
|
|
|
func install(app *App, qMap goyesql.Queries) {
|
2019-01-03 11:18:47 +00:00
|
|
|
fmt.Println("")
|
2019-06-26 10:52:47 +00:00
|
|
|
fmt.Println("** First time installation **")
|
|
|
|
fmt.Printf("** IMPORTANT: This will wipe existing listmonk tables and types in the DB '%s' **",
|
2019-06-26 11:23:23 +00:00
|
|
|
ko.String("db.database"))
|
2019-01-03 11:18:47 +00:00
|
|
|
fmt.Println("")
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-06-26 10:52:47 +00:00
|
|
|
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)
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2019-06-26 10:52:47 +00:00
|
|
|
if strings.ToLower(ok) != "y" {
|
|
|
|
fmt.Println("Installation cancelled.")
|
|
|
|
return
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate the tables.
|
2019-06-26 10:52:47 +00:00
|
|
|
err := installMigrate(app.DB, app)
|
2018-10-25 13:51:47 +00: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 listID int
|
|
|
|
if err := q.CreateList.Get(&listID,
|
|
|
|
uuid.NewV4().String(),
|
|
|
|
"Default list",
|
|
|
|
models.ListTypePublic,
|
|
|
|
pq.StringArray{"test"},
|
|
|
|
); err != nil {
|
|
|
|
logger.Fatalf("Error creating superadmin user: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sample subscriber.
|
|
|
|
if _, err := q.UpsertSubscriber.Exec(
|
|
|
|
uuid.NewV4(),
|
2019-07-04 09:17:33 +00:00
|
|
|
"john@example.com",
|
|
|
|
"John Doe",
|
|
|
|
`{"type": "known", "good": true, "city": "Bengaluru"}`,
|
2018-10-25 13:51:47 +00:00
|
|
|
pq.Int64Array{int64(listID)},
|
|
|
|
); err != nil {
|
|
|
|
logger.Fatalf("Error creating subscriber: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default template.
|
2019-07-04 09:03:03 +00:00
|
|
|
tplBody, err := ioutil.ReadFile("email-templates/default.tpl")
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
2018-10-29 09:50:11 +00:00
|
|
|
tplBody = []byte(tplTag)
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tplID int
|
|
|
|
if err := q.CreateTemplate.Get(&tplID,
|
|
|
|
"Default template",
|
|
|
|
string(tplBody),
|
|
|
|
); err != nil {
|
2019-07-04 09:17:33 +00:00
|
|
|
logger.Fatalf("error creating default template: %v", err)
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
if _, err := q.SetDefaultTemplate.Exec(tplID); err != nil {
|
2019-07-04 09:17:33 +00:00
|
|
|
logger.Fatalf("error setting default template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sample campaign.
|
|
|
|
sendAt := time.Now()
|
|
|
|
sendAt.Add(time.Minute * 43200)
|
|
|
|
if _, err := q.CreateCampaign.Exec(uuid.NewV4(),
|
|
|
|
"Test campaign",
|
|
|
|
"Welcome to listmonk",
|
|
|
|
"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 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Printf("Setup complete")
|
2019-06-26 11:23:23 +00:00
|
|
|
logger.Printf(`Run the program view it at %s`, ko.String("app.address"))
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// installMigrate executes the SQL schema and creates the necessary tables and types.
|
2019-01-03 11:18:47 +00:00
|
|
|
func installMigrate(db *sqlx.DB, app *App) error {
|
|
|
|
q, err := app.FS.Read("/schema.sql")
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Query(string(q))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-08 12:51:44 +00: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.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
ioutil.WriteFile("config.toml", b, 0644)
|
|
|
|
return nil
|
|
|
|
}
|