Recount-Server/recount-server.go
2023-12-21 16:44:34 -08:00

77 lines
2 KiB
Go

package main
import (
"database/sql"
"fmt"
"log"
"time"
"net/http"
"github.com/shopspring/decimal"
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"
)
type Transaction struct {
Id int `db:"trns_id"`
Amount decimal.Decimal `db:"trns_amount"`
Description sql.NullString `db:"trns_description"`
Account int `db:"trns_account"`
Bucket sql.NullInt64 `db:"trns_bucket"`
Date time.Time `db:"trns_date"`
}
func hello(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "hello\n")
}
func headers(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}
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 trns_id, trns_amount, trns_description, trns_account, trns_bucket, trns_date 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 {
if trns.Description.Valid {
log.Print(fmt.Sprintf("%v : %#v", trns.Amount, trns.Id))
} else {
log.Print("amount invalid")
}
}
log.Print("select all run is done")
http.HandleFunc("/hello", hello)
http.HandleFunc("/headers", headers)
http.ListenAndServe(":8090", nil)
//fmt.Println("Hello World")
}