diff --git a/go.mod b/go.mod index 53025db..6686461 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,8 @@ module nickiel.net/recount_server go 1.21.5 -require github.com/jmoiron/sqlx v1.3.5 // indirect +require ( + github.com/jmoiron/sqlx v1.3.5 + github.com/lib/pq v1.10.9 + github.com/shopspring/decimal v1.3.1 +) diff --git a/go.sum b/go.sum index bc1991f..9b95f85 100644 --- a/go.sum +++ b/go.sum @@ -2,4 +2,8 @@ github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= +github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= diff --git a/recount-server.go b/recount-server.go index 3c938ff..d1bf2ae 100644 --- a/recount-server.go +++ b/recount-server.go @@ -1,13 +1,26 @@ package main import ( + //"database/sql" "fmt" "log" + "time" "net/http" - //"github.com/jmoiron/sqlx" + "github.com/shopspring/decimal" + _ "github.com/lib/pq" + "github.com/jmoiron/sqlx" ) +type Transaction struct { + id int + amount decimal.Decimal + description string + account int + bucket int + date time.Time +} + func hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "hello\n") } @@ -24,6 +37,33 @@ func main() { log.SetPrefix("RecountServer: ") log.SetFlags(0) + // + db, err := sqlx.Connect("postgres", "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable") + if err != nil { + log.Fatal(err) + } + log.Print("Database connected") + + // Confirm a successful connection. + if err := db.Ping(); err != nil { + log.Fatal(err) + } + log.Print("Database successfully pinged") + + transactions := []Transaction{} + err = db.Select(&transactions, "SELECT * FROM rcnt.transactions ORDER BY trns_id DESC") + if err != nil { + log.Fatal(err) + } + fmt.Print(fmt.Sprintf("%+v\n", transactions)) + log.Print("Select has been run") + + for _, trns := range transactions { + log.Print(fmt.Sprintf("%#v : %#v", trns.amount, trns.id)) + } + + log.Print("select all run is done") + http.HandleFunc("/hello", hello) http.HandleFunc("/headers", headers)