Add db and types
This commit is contained in:
51
.air.toml
Normal file
51
.air.toml
Normal file
@@ -0,0 +1,51 @@
|
||||
root = "."
|
||||
testdata_dir = "testdata"
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
args_bin = []
|
||||
bin = "tmp\\main.exe"
|
||||
cmd = "clear && go run ."
|
||||
delay = 1000
|
||||
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
|
||||
exclude_file = []
|
||||
exclude_regex = ["_test.go"]
|
||||
exclude_unchanged = false
|
||||
follow_symlink = false
|
||||
full_bin = ""
|
||||
include_dir = []
|
||||
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||
include_file = []
|
||||
kill_delay = "0s"
|
||||
log = "build-errors.log"
|
||||
poll = false
|
||||
poll_interval = 0
|
||||
post_cmd = []
|
||||
pre_cmd = []
|
||||
rerun = false
|
||||
rerun_delay = 500
|
||||
send_interrupt = false
|
||||
stop_on_error = false
|
||||
|
||||
[color]
|
||||
app = ""
|
||||
build = "yellow"
|
||||
main = "magenta"
|
||||
runner = "green"
|
||||
watcher = "cyan"
|
||||
|
||||
[log]
|
||||
main_only = false
|
||||
time = false
|
||||
|
||||
[misc]
|
||||
clean_on_exit = false
|
||||
|
||||
[proxy]
|
||||
app_port = 0
|
||||
enabled = false
|
||||
proxy_port = 0
|
||||
|
||||
[screen]
|
||||
clear_on_rebuild = false
|
||||
keep_scroll = true
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
build/bin
|
||||
node_modules
|
||||
frontend/dist
|
||||
tmp
|
||||
food.db
|
||||
|
57
db.go
Normal file
57
db.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
path string
|
||||
readConn *sql.DB
|
||||
writeConn *sql.DB
|
||||
}
|
||||
|
||||
func (db *DB) Open() error {
|
||||
if db.path == "" {
|
||||
return fmt.Errorf("database path not set")
|
||||
}
|
||||
|
||||
writeConn, err := sql.Open("sqlite3", db.path+"?_journal=WAL&_synchronous=NORMAL")
|
||||
if err != nil {
|
||||
Error.Printf("%++v", err)
|
||||
return err
|
||||
}
|
||||
writeConn.SetMaxOpenConns(1)
|
||||
writeConn.SetConnMaxIdleTime(30 * time.Second)
|
||||
writeConn.SetConnMaxLifetime(30 * time.Second)
|
||||
db.writeConn = writeConn
|
||||
|
||||
readConn, err := sql.Open("sqlite3", db.path+"?mode=ro&_journal=WAL&_synchronous=NORMAL&_mode=ro")
|
||||
if err != nil {
|
||||
Error.Printf("%++v", err)
|
||||
return err
|
||||
}
|
||||
readConn.SetMaxOpenConns(4)
|
||||
readConn.SetConnMaxIdleTime(30 * time.Second)
|
||||
readConn.SetConnMaxLifetime(30 * time.Second)
|
||||
db.readConn = readConn
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) Close() error {
|
||||
err := db.writeConn.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = db.readConn.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
0
foodservice.go
Normal file
0
foodservice.go
Normal file
5
go.mod
5
go.mod
@@ -4,7 +4,10 @@ go 1.21
|
||||
|
||||
toolchain go1.22.4
|
||||
|
||||
require github.com/wailsapp/wails/v2 v2.9.1
|
||||
require (
|
||||
github.com/mattn/go-sqlite3 v1.14.22
|
||||
github.com/wailsapp/wails/v2 v2.9.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bep/debounce v1.2.1 // indirect
|
||||
|
2
go.sum
2
go.sum
@@ -35,6 +35,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
|
||||
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
|
40
main.go
40
main.go
@@ -2,6 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/wailsapp/wails/v2"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
@@ -11,12 +17,44 @@ import (
|
||||
//go:embed all:frontend/dist
|
||||
var assets embed.FS
|
||||
|
||||
var Error *log.Logger
|
||||
func init() {
|
||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||
logFile, err := os.Create("main.log")
|
||||
if err != nil {
|
||||
log.Printf("Error creating log file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logger := io.MultiWriter(os.Stdout, logFile)
|
||||
log.SetOutput(logger)
|
||||
|
||||
Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout),
|
||||
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
|
||||
log.Lmicroseconds|log.Lshortfile)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
dbpath := flag.String("db", "food.db", "Path to the database file")
|
||||
flag.Parse()
|
||||
|
||||
db := DB{path: *dbpath}
|
||||
err := db.Open()
|
||||
if err != nil {
|
||||
Error.Printf("%++v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.Println("done")
|
||||
wg.Wait()
|
||||
|
||||
// Create an instance of the app structure
|
||||
app := NewApp()
|
||||
|
||||
// Create application with options
|
||||
err := wails.Run(&options.App{
|
||||
err = wails.Run(&options.App{
|
||||
Title: "calorie-counter",
|
||||
Width: 1024,
|
||||
Height: 768,
|
||||
|
0
settingsservice.go
Normal file
0
settingsservice.go
Normal file
35
types.go
Normal file
35
types.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
Setting struct {
|
||||
key string
|
||||
value string
|
||||
}
|
||||
|
||||
Food struct {
|
||||
rowid int
|
||||
date time.Time
|
||||
food string
|
||||
descripton string
|
||||
amount float32
|
||||
per100 float32
|
||||
energy float32
|
||||
}
|
||||
AggregatedFood struct {
|
||||
period string
|
||||
amount float32
|
||||
avgPer100 float32
|
||||
energy float32
|
||||
}
|
||||
Weight struct {
|
||||
rowid int
|
||||
date time.Time
|
||||
weight float32
|
||||
}
|
||||
AggregatedWeight struct {
|
||||
period string
|
||||
amount float32
|
||||
}
|
||||
)
|
0
weightservice.go
Normal file
0
weightservice.go
Normal file
Reference in New Issue
Block a user