app.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
package main
import (
"context"
"time"
"path/filepath"
"os"
"github.com/adrg/xdg"
"github.com/sanbornm/go-selfupdate/selfupdate"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
)
type AppConfig struct {
Title string
Version string
}
type App struct {
ctx context.Context
Config AppConfig
DB *gorm.DB
UserContact Contact
Updater *selfupdate.Updater
idleCtx context.Context
idleCancel context.CancelFunc
}
func NewApp(config AppConfig) *App {
app := &App{Config: config}
db, err := gorm.Open(sqlite.Open(app.GetDataFile()), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
panic(err)
}
app.DB = db
app.migrate()
return app
}
func (app *App) startup(ctx context.Context) {
app.Log("Running startup.").
Stdout()
defer app.Log("Startup finished.").
Stdout()
app.ctx = ctx
app.idleCtx, app.idleCancel = context.WithCancel(app.ctx)
updater := &selfupdate.Updater{
CurrentVersion: app.GetVersion(),
ApiURL: "https://updater.machinetele.com/",
BinURL: "https://updater.machinetele.com/",
Dir: "update/",
CmdName: app.GetTitle(),
ForceCheck: true,
CheckTime: 1,
RandomizeTime: 0,
OnPreFetch: func(u *selfupdate.Updater) {
app.Log(
"Searching for updates...",
).Stdout().StatusLine()
app.Log(
"On %s and found %s.",
u.Info.Version,
u.CurrentVersion,
).Stdout().StatusLine()
},
OnUpdateAttempt: func() {
app.Log("Found a new version! Attempting to update.").Stdout().StatusLine()
},
OnSuccessfulUpdate: func() {
app.Log("Successfully updated to the newest version of EMessage.").Stdout().StatusLine()
},
OnUpdateFailure: func(err error) {
app.Log("Update failed: %v.", err.Error()).Stdout().StatusLine()
},
OnFinally: func() {
app.Log("Finished updating.").Stdout().StatusLine()
},
}
app.Updater = updater
go updater.Update()
go func() {
ticker := time.NewTicker(1 * time.Hour)
defer ticker.Stop()
for range ticker.C {
if err := updater.Update(); err != nil {
app.Log("Update failed: %v", err).Stdout().StatusLine()
}
}
}()
app.EstablishMailConnections()
return
}
func (app *App) shutdown(ctx context.Context) {
app.Log("Running shutdown.").
Stdout()
defer app.Log("finished shutdown.").
Stdout()
CloseMailConnections()
return
}
func (app *App) migrate() {
app.Log("Starting migration...").
Stdout()
defer app.Log("finished migration.").
Stdout()
app.DB.AutoMigrate(&Contact{})
app.DB.AutoMigrate(&EmailAddress{})
app.DB.AutoMigrate(&PhoneNumber{})
app.DB.AutoMigrate(&URL{})
app.DB.AutoMigrate(&StreetAddress{})
app.DB.AutoMigrate(&Date{})
app.DB.AutoMigrate(&OtherField{})
app.DB.Preload(clause.Associations).
FirstOrCreate(
&app.UserContact,
Contact{Model: gorm.Model{ID: 1}},
)
app.Log("Ensuring contact.").
Stdout()
app.DB.AutoMigrate(&MailAccountConnection{})
app.DB.AutoMigrate(&Conversation{})
app.DB.AutoMigrate(&Message{})
app.DB.AutoMigrate(&MailHeader{})
app.DB.AutoMigrate(&LastSynced{})
app.DB.AutoMigrate(&MessageContent{})
app.DB.AutoMigrate(&DisplayImage{})
return
}
func (app *App) GetTitle() string {
return app.Config.Title
}
func (app *App) GetVersion() string {
return app.Config.Version
}
func (app *App) GetDataFile() string {
if file, err := xdg.DataFile(app.GetTitle() + "/data.db"); err != nil {
panic(err)
} else {
return file
}
}
func (app *App) GetMailPasswordKeyringService() string {
title := app.GetTitle()
return title + "-mail-password"
}
func (app *App) GetTmpLogFile() string {
return filepath.Join(os.TempDir(), app.GetTitle()+".log")
}
|