2020-07-16 01:20:08 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-21 01:11:24 +02:00
|
|
|
"net/http"
|
2020-09-13 01:03:17 +02:00
|
|
|
"strings"
|
2020-08-21 01:11:24 +02:00
|
|
|
"time"
|
|
|
|
|
2020-08-13 05:17:42 +02:00
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/postgres"
|
|
|
|
"github.com/golang-migrate/migrate/v4/source/httpfs"
|
2020-09-13 01:03:17 +02:00
|
|
|
"github.com/google/uuid"
|
2020-07-16 01:20:08 +02:00
|
|
|
"github.com/spf13/cobra"
|
2020-08-13 05:17:42 +02:00
|
|
|
"github.com/spf13/viper"
|
2020-07-16 01:20:08 +02:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2020-08-07 03:50:35 +02:00
|
|
|
"github.com/jordanknott/taskcafe/internal/route"
|
2020-12-23 23:44:13 +01:00
|
|
|
"github.com/jordanknott/taskcafe/internal/utils"
|
2020-07-16 01:20:08 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-08-13 05:17:42 +02:00
|
|
|
var autoMigrate bool
|
|
|
|
|
2020-07-16 01:20:08 +02:00
|
|
|
func newWebCmd() *cobra.Command {
|
2020-08-13 05:17:42 +02:00
|
|
|
cc := &cobra.Command{
|
2020-07-16 01:20:08 +02:00
|
|
|
Use: "web",
|
|
|
|
Short: "Run the web server",
|
|
|
|
Long: "Run the web & api server",
|
2020-08-13 05:17:42 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-07-16 01:20:08 +02:00
|
|
|
Formatter := new(log.TextFormatter)
|
|
|
|
Formatter.TimestampFormat = "02-01-2006 15:04:05"
|
|
|
|
Formatter.FullTimestamp = true
|
|
|
|
log.SetFormatter(Formatter)
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
|
2020-10-21 01:52:09 +02:00
|
|
|
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s port=%s sslmode=disable",
|
2020-08-13 05:17:42 +02:00
|
|
|
viper.GetString("database.user"),
|
|
|
|
viper.GetString("database.password"),
|
|
|
|
viper.GetString("database.host"),
|
|
|
|
viper.GetString("database.name"),
|
2020-10-21 01:52:09 +02:00
|
|
|
viper.GetString("database.port"),
|
2020-07-16 01:20:08 +02:00
|
|
|
)
|
2020-08-28 21:22:24 +02:00
|
|
|
var db *sqlx.DB
|
|
|
|
var err error
|
2020-09-13 01:23:23 +02:00
|
|
|
var retryDuration time.Duration
|
|
|
|
maxRetryNumber := 4
|
|
|
|
for i := 0; i < maxRetryNumber; i++ {
|
2020-08-28 21:22:24 +02:00
|
|
|
db, err = sqlx.Connect("postgres", connection)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2020-09-13 01:23:23 +02:00
|
|
|
retryDuration = time.Duration(i*2) * time.Second
|
|
|
|
log.WithFields(log.Fields{"retryNumber": i, "retryDuration": retryDuration}).WithError(err).Error("issue connecting to database, retrying")
|
|
|
|
if i != maxRetryNumber-1 {
|
|
|
|
time.Sleep(retryDuration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-07-16 01:20:08 +02:00
|
|
|
}
|
|
|
|
db.SetMaxOpenConns(25)
|
|
|
|
db.SetMaxIdleConns(25)
|
|
|
|
db.SetConnMaxLifetime(5 * time.Minute)
|
|
|
|
defer db.Close()
|
2020-08-13 03:54:14 +02:00
|
|
|
|
2020-08-13 05:17:42 +02:00
|
|
|
if viper.GetBool("migrate") {
|
|
|
|
log.Info("running auto schema migrations")
|
|
|
|
if err = runMigration(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{"url": viper.GetString("server.hostname")}).Info("starting server")
|
2020-09-13 01:03:17 +02:00
|
|
|
secret := viper.GetString("server.secret")
|
|
|
|
if strings.TrimSpace(secret) == "" {
|
|
|
|
log.Warn("server.secret is not set, generating a random secret")
|
|
|
|
secret = uuid.New().String()
|
|
|
|
}
|
2020-12-23 23:44:13 +01:00
|
|
|
r, _ := route.NewRouter(db, utils.EmailConfig{
|
|
|
|
From: viper.GetString("smtp.from"),
|
|
|
|
Host: viper.GetString("smtp.host"),
|
|
|
|
Port: viper.GetInt("smtp.port"),
|
|
|
|
Username: viper.GetString("smtp.username"),
|
|
|
|
Password: viper.GetString("smtp.password"),
|
|
|
|
InsecureSkipVerify: viper.GetBool("smtp.skip_verify"),
|
|
|
|
}, []byte(secret))
|
2020-09-19 03:19:14 +02:00
|
|
|
return http.ListenAndServe(viper.GetString("server.hostname"), r)
|
2020-07-16 01:20:08 +02:00
|
|
|
},
|
|
|
|
}
|
2020-10-21 01:52:09 +02:00
|
|
|
|
2020-12-23 23:44:13 +01:00
|
|
|
viper.SetDefault("smtp.from", "no-reply@example.com")
|
|
|
|
viper.SetDefault("smtp.host", "localhost")
|
|
|
|
viper.SetDefault("smtp.port", 587)
|
|
|
|
viper.SetDefault("smtp.username", "")
|
|
|
|
viper.SetDefault("smtp.password", "")
|
|
|
|
viper.SetDefault("smtp.skip_verify", false)
|
|
|
|
|
2020-08-13 05:17:42 +02:00
|
|
|
cc.Flags().Bool("migrate", false, "if true, auto run's schema migrations before starting the web server")
|
2020-10-21 01:52:09 +02:00
|
|
|
|
2020-08-13 05:17:42 +02:00
|
|
|
viper.BindPFlag("migrate", cc.Flags().Lookup("migrate"))
|
2020-10-21 01:52:09 +02:00
|
|
|
|
2020-08-13 05:17:42 +02:00
|
|
|
viper.SetDefault("migrate", false)
|
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
func runMigration(db *sqlx.DB) error {
|
|
|
|
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := httpfs.New(migration, "./")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m, err := migrate.NewWithInstance("httpfs", src, "postgres", driver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger := &MigrateLog{}
|
|
|
|
m.Log = logger
|
|
|
|
err = m.Up()
|
|
|
|
if err != nil && err != migrate.ErrNoChange {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-16 01:20:08 +02:00
|
|
|
}
|