2020-04-10 04:40:22 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-05 01:02:57 +02:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2020-06-23 22:20:53 +02:00
|
|
|
"github.com/jordan-wright/email"
|
2020-08-07 03:50:35 +02:00
|
|
|
"github.com/jordanknott/taskcafe/pg"
|
|
|
|
"github.com/jordanknott/taskcafe/router"
|
2020-05-27 23:18:50 +02:00
|
|
|
_ "github.com/lib/pq"
|
2020-07-05 01:02:57 +02:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"io/ioutil"
|
2020-06-23 22:20:53 +02:00
|
|
|
"net/smtp"
|
2020-07-05 01:02:57 +02:00
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/postgres"
|
|
|
|
_ "github.com/golang-migrate/migrate/v4/source/file"
|
2020-04-10 04:40:22 +02:00
|
|
|
)
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
type Database struct {
|
|
|
|
Host string
|
|
|
|
Name string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
type AppConfig struct {
|
|
|
|
Database Database
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserRegistration struct {
|
|
|
|
Username string
|
|
|
|
Year string
|
|
|
|
AppName string
|
|
|
|
AppURL string
|
|
|
|
}
|
|
|
|
|
2020-05-27 23:18:50 +02:00
|
|
|
type color struct {
|
|
|
|
Name string
|
|
|
|
Color string
|
|
|
|
Position int
|
|
|
|
}
|
|
|
|
|
|
|
|
type colors struct {
|
|
|
|
Color []color
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func SendEmail() {
|
|
|
|
emailTmpl, err := ioutil.ReadFile("templates/mail/user/registered.tmpl")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-08-07 03:50:35 +02:00
|
|
|
user := UserRegistration{Username: "jordanthedev", AppName: "Taskcafe", AppURL: "http://localhost:3000/", Year: "2020"}
|
2020-07-05 01:02:57 +02:00
|
|
|
tmpl, err := template.New("registered").Parse(string(emailTmpl))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var tpl bytes.Buffer
|
|
|
|
if err := tmpl.Execute(&tpl, &user); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := tpl.String()
|
2020-06-23 22:20:53 +02:00
|
|
|
|
|
|
|
e := email.NewEmail()
|
2020-08-07 03:50:35 +02:00
|
|
|
e.From = "Jordan Knott <no-reply@taskcafe.com>"
|
2020-06-23 22:20:53 +02:00
|
|
|
e.To = []string{"jordan@jordanthedev.com"}
|
2020-08-07 03:50:35 +02:00
|
|
|
e.Subject = "Jordan Knott (@jordanthedev) invited you to join the team \"Paradox\" on Taskcafe"
|
2020-06-23 22:20:53 +02:00
|
|
|
e.Text = []byte("Text Body is, of course, supported!")
|
2020-07-05 01:02:57 +02:00
|
|
|
e.HTML = []byte(result)
|
2020-06-23 22:20:53 +02:00
|
|
|
e.Send("localhost:1025", smtp.PlainAuth("", "test@gmail.com", "password123", "localhost"))
|
2020-07-05 01:02:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Seed() {
|
|
|
|
dur := time.Hour * 24 * 7 * 30
|
|
|
|
token, err := router.NewAccessTokenCustomExpiration("21345076-6423-4a00-a6bd-cd9f830e2764", dur)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println(token)
|
|
|
|
|
|
|
|
fmt.Println("seeding database...")
|
|
|
|
|
|
|
|
dat, err := ioutil.ReadFile("data/dark_colors.toml")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var labelColors colors
|
|
|
|
_, err = toml.Decode(string(dat), &labelColors)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-07 03:50:35 +02:00
|
|
|
db, err := sqlx.Connect("postgres", "user=postgres password=test host=0.0.0.0 dbname=taskcafe sslmode=disable")
|
2020-07-05 01:02:57 +02:00
|
|
|
repository := pg.NewRepository(db)
|
|
|
|
for _, color := range labelColors.Color {
|
|
|
|
fmt.Printf("%v\n", color)
|
|
|
|
repository.CreateLabelColor(context.Background(), pg.CreateLabelColorParams{color.Name, color.Color, float64(color.Position)})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func Migrate() {
|
|
|
|
dat, err := ioutil.ReadFile("conf/app.toml")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var appConfig AppConfig
|
|
|
|
_, err = toml.Decode(string(dat), &appConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
|
|
|
|
appConfig.Database.User,
|
|
|
|
appConfig.Database.Password,
|
|
|
|
appConfig.Database.Host,
|
|
|
|
appConfig.Database.Name,
|
|
|
|
)
|
|
|
|
fmt.Println(connection)
|
|
|
|
db, err := sqlx.Connect("postgres", connection)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
m, err := migrate.NewWithDatabaseInstance(
|
|
|
|
"file://migrations",
|
|
|
|
"postgres", driver)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = m.Up()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
dat, err := ioutil.ReadFile("conf/app.toml")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var appConfig AppConfig
|
|
|
|
_, err = toml.Decode(string(dat), &appConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
|
|
|
|
appConfig.Database.User,
|
|
|
|
appConfig.Database.Password,
|
|
|
|
appConfig.Database.Host,
|
|
|
|
appConfig.Database.Name,
|
|
|
|
)
|
|
|
|
fmt.Println(connection)
|
|
|
|
db, err := sqlx.Connect("postgres", connection)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-05-27 23:18:50 +02:00
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
}
|
|
|
|
createdAt := time.Now().UTC()
|
|
|
|
hashedPwd, err := bcrypt.GenerateFromPassword([]byte("test"), 14)
|
|
|
|
repo := pg.NewRepository(db)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = repo.CreateUserAccount(context.Background(), pg.CreateUserAccountParams{
|
|
|
|
Username: "jordan",
|
|
|
|
Initials: "JK",
|
|
|
|
Email: "jordan@jordanthedev.com",
|
|
|
|
PasswordHash: string(hashedPwd),
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
RoleCode: "admin",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|