-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
95 lines (76 loc) · 2.21 KB
/
main.go
File metadata and controls
95 lines (76 loc) · 2.21 KB
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
package main
import (
"database/sql"
"goldwatcher/repository"
"log"
"net/http"
"os"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
_ "github.com/glebarez/go-sqlite"
)
type Config struct {
App fyne.App
InfoLog *log.Logger
ErrorLog *log.Logger
DB repository.Repository
MainWindow fyne.Window
PriceContainer *fyne.Container
Toolbar *widget.Toolbar
PriceChartContainer *fyne.Container
Holdings [][]interface{}
HoldingsTable *widget.Table
HTTPClient *http.Client
AddHoldingsPurchaseAmountEntry *widget.Entry
AddHoldingsPurchaseDateEntry *widget.Entry
AddHoldingsPurchasePriceEntry *widget.Entry
}
func main() {
var myApp Config
// create a fyne application
fyneApp := app.NewWithID("dev.thomascode92.goldwatcher")
myApp.App = fyneApp
myApp.HTTPClient = &http.Client{}
// create loggers
myApp.InfoLog = log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
myApp.ErrorLog = log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
// open a connection to the database
sqlDB, err := myApp.connectSQL()
if err != nil {
log.Panic(err)
}
// create a database repository
myApp.setupDB(sqlDB)
currency = fyneApp.Preferences().StringWithFallback("currency", "USD")
// create and size a fyne window
myApp.MainWindow = fyneApp.NewWindow("GoldWatcher")
myApp.MainWindow.Resize(fyne.NewSize(770, 410))
myApp.MainWindow.SetFixedSize(true)
myApp.MainWindow.SetMaster()
myApp.makeUI()
// show and run the application
myApp.MainWindow.ShowAndRun()
}
func (app *Config) connectSQL() (*sql.DB, error) {
path := ""
if os.Getenv("DB_PATH") != "" {
path = os.Getenv("DB_PATH")
} else {
path = app.App.Storage().RootURI().Path() + "/sqlite.db"
app.InfoLog.Println("db in:", path)
}
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, err
}
return db, nil
}
func (app *Config) setupDB(sqlDB *sql.DB) {
app.DB = repository.NewSQLiteRepository(sqlDB)
err := app.DB.Migrate()
if err != nil {
app.ErrorLog.Println(err)
log.Panic()
}
}