From 0d261d2f7d080b610f50102576073e334a89e8cb Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Thu, 23 Oct 2025 01:15:26 +0700 Subject: feat: email linking --- config/config.go | 11 +++++++++++ config/config.local.yaml | 8 ++++++++ config/config.prod.yaml | 8 ++++++++ config/config.stage.yaml | 8 ++++++++ 4 files changed, 35 insertions(+) (limited to 'config') diff --git a/config/config.go b/config/config.go index f57b5cd..d5dab2d 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,7 @@ type Config struct { LocalePath string `json:"LocalePath" yaml:"localePath" validate:"required,filepath"` AvailableLanguages []AvailableLanguageConfig `json:"AvailableLanguages" yaml:"availableLanguages" validate:"required"` Auth AuthConfig `json:"Auth" yaml:"auth" validate:"required"` + Mail MailConfig `json:"Mail" yaml:"mail" validate:"required"` } type BlogPagesConfig struct { @@ -60,6 +61,16 @@ type Sqlite3Config struct { DSN string `json:"DSN" yaml:"dsn" validate:"required"` } +type MailConfig struct { + ClientHost string `json:"ClientHost" yaml:"clientHost" validate:"required"` + MailHost string `json:"MailHost" yaml:"mailHost" validate:"required"` + PublicName string `json:"PublicName" yaml:"publicName" validate:"required"` + MailAddress string `json:"MailAddress" yaml:"mailAddress" validate:"required"` + Username string `json:"Username" yaml:"username" validate:"required"` + Password string `json:"Password" yaml:"password" validate:"required"` + Salt string `json:"Salt" yaml:"salt" validate:"required"` +} + func LoadConfig(path string, config *Config) error { fileBytes, err := os.ReadFile(path) if err != nil { diff --git a/config/config.local.yaml b/config/config.local.yaml index 6d0abf3..008fa93 100644 --- a/config/config.local.yaml +++ b/config/config.local.yaml @@ -34,3 +34,11 @@ auth: config: dsn: 'file:/tmp/auth.db?cache=shared&mode=rwc' salt: '123' +mail: + clientHost: '127.0.0.1:3000' + mailHost: '${MAIL_HOST}' + publicName: 'LOCAL.SAYA.TODAY' + mailAddress: '${MAIL_ADDRESS}' + username: '${MAIL_USERNAME}' + password: '${MAIL_PASSWORD}' + salt: '${MAIL_SALT}' diff --git a/config/config.prod.yaml b/config/config.prod.yaml index 38981a8..4c0f425 100644 --- a/config/config.prod.yaml +++ b/config/config.prod.yaml @@ -34,3 +34,11 @@ auth: config: dsn: 'file:/data/auth.db?cache=private&mode=rwc&_locking_mode=EXCLUSIVE&_mutex=no&_auto_vacuum=2' salt: '${AUTH_SALT}' +mail: + clientHost: '${FQDN}' + mailHost: '${MAIL_HOST}' + publicName: 'SAYA.TODAY' + mailAddress: '${MAIL_ADDRESS}' + username: '${MAIL_USERNAME}' + password: '${MAIL_PASSWORD}' + salt: '${MAIL_SALT}' diff --git a/config/config.stage.yaml b/config/config.stage.yaml index b05edaf..003ec05 100644 --- a/config/config.stage.yaml +++ b/config/config.stage.yaml @@ -34,3 +34,11 @@ auth: config: dsn: 'file:/data/auth.db?cache=private&mode=rwc&_locking_mode=EXCLUSIVE&_mutex=no&_auto_vacuum=2' salt: '${AUTH_SALT}' +mail: + clientHost: '${FQDN}' + mailHost: '${MAIL_HOST}' + publicName: 'DEMO.SAYA.TODAY' + mailAddress: '${MAIL_ADDRESS}' + username: '${MAIL_USERNAME}' + password: '${MAIL_PASSWORD}' + salt: '${MAIL_SALT}' -- cgit v1.3.1+13 From 249f73a7eff43994ed91fb1e1bef8edf5ac6c86a Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Sat, 25 Oct 2025 18:57:48 +0700 Subject: feat: configurable cron for mail trigger --- config/config.go | 19 ++++++++++++------- config/config.local.yaml | 2 ++ config/config.prod.yaml | 2 ++ config/config.stage.yaml | 2 ++ internal/blogtrigger/blogtrigger.go | 4 ++-- main.go | 2 +- 6 files changed, 21 insertions(+), 10 deletions(-) (limited to 'config') diff --git a/config/config.go b/config/config.go index d5dab2d..1393dd8 100644 --- a/config/config.go +++ b/config/config.go @@ -62,13 +62,18 @@ type Sqlite3Config struct { } type MailConfig struct { - ClientHost string `json:"ClientHost" yaml:"clientHost" validate:"required"` - MailHost string `json:"MailHost" yaml:"mailHost" validate:"required"` - PublicName string `json:"PublicName" yaml:"publicName" validate:"required"` - MailAddress string `json:"MailAddress" yaml:"mailAddress" validate:"required"` - Username string `json:"Username" yaml:"username" validate:"required"` - Password string `json:"Password" yaml:"password" validate:"required"` - Salt string `json:"Salt" yaml:"salt" validate:"required"` + ClientHost string `json:"ClientHost" yaml:"clientHost" validate:"required"` + MailHost string `json:"MailHost" yaml:"mailHost" validate:"required"` + PublicName string `json:"PublicName" yaml:"publicName" validate:"required"` + MailAddress string `json:"MailAddress" yaml:"mailAddress" validate:"required"` + Username string `json:"Username" yaml:"username" validate:"required"` + Password string `json:"Password" yaml:"password" validate:"required"` + Salt string `json:"Salt" yaml:"salt" validate:"required"` + Trigger TriggerConfig `json:"Trigger" yaml:"trigger" validate:"required"` +} + +type TriggerConfig struct { + OnNewPost string `json:"OnNewPost" yaml:"onNewPost" validate:"cron,required"` } func LoadConfig(path string, config *Config) error { diff --git a/config/config.local.yaml b/config/config.local.yaml index 008fa93..d40e8e9 100644 --- a/config/config.local.yaml +++ b/config/config.local.yaml @@ -42,3 +42,5 @@ mail: username: '${MAIL_USERNAME}' password: '${MAIL_PASSWORD}' salt: '${MAIL_SALT}' + trigger: + onNewPost: "* * * * *" diff --git a/config/config.prod.yaml b/config/config.prod.yaml index 4c0f425..7a9e5ae 100644 --- a/config/config.prod.yaml +++ b/config/config.prod.yaml @@ -42,3 +42,5 @@ mail: username: '${MAIL_USERNAME}' password: '${MAIL_PASSWORD}' salt: '${MAIL_SALT}' + trigger: + onNewPost: "0/5 * * * *" diff --git a/config/config.stage.yaml b/config/config.stage.yaml index 003ec05..8a5fde1 100644 --- a/config/config.stage.yaml +++ b/config/config.stage.yaml @@ -42,3 +42,5 @@ mail: username: '${MAIL_USERNAME}' password: '${MAIL_PASSWORD}' salt: '${MAIL_SALT}' + trigger: + onNewPost: "0/3 * * * *" diff --git a/internal/blogtrigger/blogtrigger.go b/internal/blogtrigger/blogtrigger.go index ab66981..9b016e8 100644 --- a/internal/blogtrigger/blogtrigger.go +++ b/internal/blogtrigger/blogtrigger.go @@ -16,7 +16,7 @@ type BlogTriggerScheduler struct { onTrigger func([]*b2.BlogPage) error } -func NewBlogTriggerScheduler(b2Client *b2.B2Client, availableLanguages []config.AvailableLanguageConfig, onTrigger func([]*b2.BlogPage) error) (*BlogTriggerScheduler, error) { +func NewBlogTriggerScheduler(b2Client *b2.B2Client, availableLanguages []config.AvailableLanguageConfig, cron string, onTrigger func([]*b2.BlogPage) error) (*BlogTriggerScheduler, error) { s, err := gocron.NewScheduler() if err != nil { return nil, fmt.Errorf("failed to create new scheduler: %w", err) @@ -30,7 +30,7 @@ func NewBlogTriggerScheduler(b2Client *b2.B2Client, availableLanguages []config. bts := &BlogTriggerScheduler{s, knownBlogPages, b2Client, onTrigger} defer bts.s.Start() - bts.s.NewJob(gocron.CronJob("0/5 * * * *", false), gocron.NewTask(func(bts *BlogTriggerScheduler) { + bts.s.NewJob(gocron.CronJob(cron, false), gocron.NewTask(func(bts *BlogTriggerScheduler) { posts, err := bts.scan() if err != nil { slog.Error("failed to execute scanning new blog pages cron job", slog.String("error", err.Error())) diff --git a/main.go b/main.go index 9d80cee..bd552a1 100644 --- a/main.go +++ b/main.go @@ -153,7 +153,7 @@ func main() { os.Exit(1) } - router.BlogTrigger, err = blogtrigger.NewBlogTriggerScheduler(b2Client, cfg.AvailableLanguages, func(bp []*b2.BlogPage) error { + router.BlogTrigger, err = blogtrigger.NewBlogTriggerScheduler(b2Client, cfg.AvailableLanguages, cfg.Mail.Trigger.OnNewPost, func(bp []*b2.BlogPage) error { for _, post := range bp { if err := router.Mailer.NewPost(post); err != nil { return err -- cgit v1.3.1+13 From 634fa487aa91c1a12af0ddb0cb0744c27df12c04 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Sat, 25 Oct 2025 19:41:49 +0700 Subject: feat: enable WAL journal mode for db fix: green colors on unsubscribe page feat: properly shutdown blog trigger scheduler --- config/config.local.yaml | 2 +- config/config.prod.yaml | 2 +- config/config.stage.yaml | 2 +- internal/blogtrigger/blogtrigger.go | 4 ++++ internal/router/lang-user-unsubscribe.go | 2 +- main.go | 3 +++ 6 files changed, 11 insertions(+), 4 deletions(-) (limited to 'config') diff --git a/config/config.local.yaml b/config/config.local.yaml index d40e8e9..327bad4 100644 --- a/config/config.local.yaml +++ b/config/config.local.yaml @@ -32,7 +32,7 @@ auth: db: type: sqlite3 config: - dsn: 'file:/tmp/auth.db?cache=shared&mode=rwc' + dsn: 'file:/tmp/auth.db?cache=shared&mode=rwc&_journal_mode=WAL' salt: '123' mail: clientHost: '127.0.0.1:3000' diff --git a/config/config.prod.yaml b/config/config.prod.yaml index 7a9e5ae..0bee79d 100644 --- a/config/config.prod.yaml +++ b/config/config.prod.yaml @@ -32,7 +32,7 @@ auth: db: type: sqlite3 config: - dsn: 'file:/data/auth.db?cache=private&mode=rwc&_locking_mode=EXCLUSIVE&_mutex=no&_auto_vacuum=2' + dsn: 'file:/data/auth.db?cache=private&mode=rwc&_locking_mode=EXCLUSIVE&_mutex=no&_auto_vacuum=2&_journal_mode=WAL' salt: '${AUTH_SALT}' mail: clientHost: '${FQDN}' diff --git a/config/config.stage.yaml b/config/config.stage.yaml index 8a5fde1..1c15dae 100644 --- a/config/config.stage.yaml +++ b/config/config.stage.yaml @@ -32,7 +32,7 @@ auth: db: type: sqlite3 config: - dsn: 'file:/data/auth.db?cache=private&mode=rwc&_locking_mode=EXCLUSIVE&_mutex=no&_auto_vacuum=2' + dsn: 'file:/data/auth.db?cache=private&mode=rwc&_locking_mode=EXCLUSIVE&_mutex=no&_auto_vacuum=2&_journal_mode=WAL' salt: '${AUTH_SALT}' mail: clientHost: '${FQDN}' diff --git a/internal/blogtrigger/blogtrigger.go b/internal/blogtrigger/blogtrigger.go index 9b016e8..c5b3097 100644 --- a/internal/blogtrigger/blogtrigger.go +++ b/internal/blogtrigger/blogtrigger.go @@ -49,6 +49,10 @@ func NewBlogTriggerScheduler(b2Client *b2.B2Client, availableLanguages []config. return bts, nil } +func (bts *BlogTriggerScheduler) Close() error { + return bts.s.Shutdown() +} + func (bts *BlogTriggerScheduler) scan() (newPages []*b2.BlogPage, err error) { newPages = make([]*b2.BlogPage, 0) for lang := range bts.knownBlogPages { diff --git a/internal/router/lang-user-unsubscribe.go b/internal/router/lang-user-unsubscribe.go index a6ca89f..81d1bfb 100644 --- a/internal/router/lang-user-unsubscribe.go +++ b/internal/router/lang-user-unsubscribe.go @@ -45,7 +45,7 @@ func Lang_User_Unsubscribe(l map[string]*locale.LocaleConfig, langs []config.Ava statusText = l[lang].UnsubscribePage.OnServerError status = fiber.ErrInternalServerError.Code } else { - statusColor = "0, 255, 0," + statusColor = "0, 255, 0" statusEmoji = "♡⸜(˶˃ ᵕ ˂˶)⸝♡" statusText = l[lang].UnsubscribePage.Success status = fiber.StatusOK diff --git a/main.go b/main.go index bd552a1..174854d 100644 --- a/main.go +++ b/main.go @@ -202,6 +202,9 @@ func main() { <-sigChan slog.Info("gracefully shutting down...") + if err = router.BlogTrigger.Close(); err != nil { + slog.Error("fail to shutdown blog trigger scheduler", slog.String("error", err.Error())) + } if err = app.Shutdown(); err != nil { slog.Error("fail to shutdown fiber server", slog.String("error", err.Error())) } -- cgit v1.3.1+13 From e1dbbcf13f1a4c1f82a8f3eaeb54f67f7be2e480 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Sat, 25 Oct 2025 20:22:10 +0700 Subject: feat: add debug info about starting/ending db transactions --- config/config.local.yaml | 3 ++- internal/mailer/mailer.go | 22 ++++++++++++++++++++++ internal/router/client-cache.go | 12 ++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) (limited to 'config') diff --git a/config/config.local.yaml b/config/config.local.yaml index 327bad4..e23c808 100644 --- a/config/config.local.yaml +++ b/config/config.local.yaml @@ -32,7 +32,8 @@ auth: db: type: sqlite3 config: - dsn: 'file:/tmp/auth.db?cache=shared&mode=rwc&_journal_mode=WAL' + # dsn: 'file:/tmp/auth.db?cache=shared&mode=rwc&_journal_mode=WAL' + dsn: 'file:/tmp/auth.db?cache=private&mode=rwc&_locking_mode=EXCLUSIVE&_mutex=no&_auto_vacuum=2&_journal_mode=WAL' salt: '123' mail: clientHost: '127.0.0.1:3000' diff --git a/internal/mailer/mailer.go b/internal/mailer/mailer.go index eb86d56..847ebde 100644 --- a/internal/mailer/mailer.go +++ b/internal/mailer/mailer.go @@ -151,15 +151,19 @@ func (m *Mailer) MailIsTaken(email string) (bool, error) { if err != nil { return false, fmt.Errorf("failed to initialize transaction with db: %s", err) } + + slog.Debug("began db transaction", slog.String("method", "MailIsTaken")) defer func(tx *sql.Tx) { if err = tx.Commit(); err != nil { tx.Rollback() } + slog.Debug("ended db transaction", slog.String("method", "MailIsTaken")) }(tx) var rows *sql.Rows if rows, err = tx.Query(`SELECT email FROM user_email_table WHERE email=? LIMIT 1;`, email); err != nil { tx.Rollback() + return false, fmt.Errorf("failed to query user-email settings in db: %s", err) } defer rows.Close() @@ -173,10 +177,13 @@ func (m *Mailer) GetInfo(userIdHash []byte) (email string, lang string, err erro if err != nil { return "", "", fmt.Errorf("failed to initialize transaction with db: %s", err) } + + slog.Debug("began db transaction", slog.String("method", "GetInfo")) defer func(tx *sql.Tx) { if err = tx.Commit(); err != nil { tx.Rollback() } + slog.Debug("ended db transaction", slog.String("method", "GetInfo")) }(tx) var rows *sql.Rows @@ -310,18 +317,22 @@ func (m *Mailer) Verify(verificationCodeEncoded string, lang string) error { return fmt.Errorf("failed to initialize transaction with db: %s", err) } + slog.Debug("began db transaction", slog.String("method", "Verify")) if _, err = tx.Exec(`INSERT INTO user_email_table(user_id, email, lang) VALUES(?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET email=excluded.email, lang=excluded.lang;`, m.GetHash(string(userId)), address, lang); err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "Verify")) return fmt.Errorf("failed to configure user-email settings in db: %s", err) } if err = tx.Commit(); err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "Verify")) return fmt.Errorf("failed to commit transaction to db: %s", err) } + slog.Debug("ended db transaction", slog.String("method", "Verify")) m.verificationCodes.Del(verificationCode) delete(m.lostMailMap, verificationSegments[0]) @@ -333,9 +344,12 @@ func (m *Mailer) GetSubscriptions(userId string) (subscriptionType SubscriptionT if err != nil { return None, nil, fmt.Errorf("failed to initialize transaction with db: %s", err) } + + slog.Debug("began db transaction", slog.String("method", "GetSubscriptions")) defer func(tx *sql.Tx) { if err = tx.Commit(); err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "GetSubscriptions")) } }(tx) @@ -373,6 +387,8 @@ func (m *Mailer) Subscribe(userIdHash []byte, subscriptionType SubscriptionType, return fmt.Errorf("failed to initialize transaction with db: %s", err) } + slog.Debug("began db transaction", slog.String("method", "Subscribe")) + slices.Sort(tags) tagsOutput := "" switch subscriptionType { @@ -388,13 +404,16 @@ func (m *Mailer) Subscribe(userIdHash []byte, subscriptionType SubscriptionType, ON CONFLICT(user_id) DO UPDATE SET tags=excluded.tags;`, userIdHash, tagsOutput); err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "Subscribe")) return fmt.Errorf("failed to configure user-to-tags table in db for the user: %s", err) } if err = tx.Commit(); err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "Subscribe")) return fmt.Errorf("failed to commit transaction to db: %s", err) } + slog.Debug("ended db transaction", slog.String("method", "Subscribe")) return nil } @@ -405,9 +424,11 @@ func (m *Mailer) NewPost(post *b2.BlogPage) error { return fmt.Errorf("failed to initialize transaction with db: %s", err) } + slog.Debug("began db transaction", slog.String("method", "NewPost")) var rows *sql.Rows if rows, err = tx.Query(`SELECT user_id, tags FROM subscription_user_to_tags_table;`); err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "NewPost")) return fmt.Errorf("failed to query user-to-tags table in db: %s", err) } @@ -463,6 +484,7 @@ rowLoop: tx.Commit() rows.Close() + slog.Debug("ended db transaction", slog.String("method", "NewPost")) messages := make([]*mail.Msg, 0, len(usersToSend)) for _, user := range usersToSend { diff --git a/internal/router/client-cache.go b/internal/router/client-cache.go index 26557d0..e7cda30 100644 --- a/internal/router/client-cache.go +++ b/internal/router/client-cache.go @@ -31,10 +31,12 @@ func NewClientCache(db *sql.DB, salt []byte) (*ClientCache, error) { if err != nil { return nil, fmt.Errorf("fail to init transaction with db to fill cache: %w", err) } + slog.Debug("began db transaction", slog.String("method", "NewClientCache")) rows, err := tx.Query("select * from blog_likes;") if err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "NewClientCache")) return nil, fmt.Errorf("fail to query db for blog_likes to fill cache: %w", err) } @@ -62,6 +64,7 @@ func NewClientCache(db *sql.DB, salt []byte) (*ClientCache, error) { rows, err = tx.Query("select * from blog_views;") if err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "NewClientCache")) return nil, fmt.Errorf("fail to query db for blog_views to fill cache: %w", err) } @@ -81,8 +84,11 @@ func NewClientCache(db *sql.DB, salt []byte) (*ClientCache, error) { } if err = tx.Commit(); err != nil { + tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "NewClientCache")) return nil, fmt.Errorf("fail to commit transaction in db: %w", err) } + slog.Debug("ended db transaction", slog.String("method", "NewClientCache")) return &ClientCache{ hashMap: make(map[string]string), @@ -99,21 +105,27 @@ func (c *ClientCache) Close() error { if err != nil { return fmt.Errorf("fail to init transaction with db to dump cache: %w", err) } + slog.Debug("began db transaction in ClientCache.Close") if err = batchSave(tx, "blog_likes", c.likePageMap); err != nil { tx.Rollback() + slog.Debug("ended db transaction in ClientCache.Close") return fmt.Errorf("fail to save blog_likes: %s", err) } if err = batchSave(tx, "blog_views", c.viewPageMap); err != nil { tx.Rollback() + slog.Debug("ended db transaction in ClientCache.Close") return fmt.Errorf("fail to save blog_views: %s", err) } if err = tx.Commit(); err != nil { tx.Rollback() + slog.Debug("ended db transaction in ClientCache.Close") return fmt.Errorf("fail to commit all the changes related to cache: %s", err) } + + slog.Debug("ended db transaction in ClientCache.Close") return nil } -- cgit v1.3.1+13