2023-12-19 20:13:32 -08:00
package main
2023-12-17 21:21:29 -08:00
2023-12-19 20:13:32 -08:00
import (
2023-12-21 16:44:34 -08:00
"database/sql"
2023-12-30 10:35:51 -08:00
"encoding/json"
"net/http"
"errors"
2023-12-19 20:13:32 -08:00
"fmt"
"log"
2023-12-30 10:35:51 -08:00
"os"
2023-12-19 22:03:09 -08:00
"time"
2023-12-19 20:51:52 -08:00
2023-12-19 22:03:09 -08:00
"github.com/shopspring/decimal"
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"
2023-12-19 20:13:32 -08:00
)
2023-12-17 21:21:29 -08:00
2023-12-30 10:35:51 -08:00
// "json:"json_code_name,omitempty"" (omit empty)
// if you use `json:"-"` it doesn't encode it
2023-12-19 22:03:09 -08:00
type Transaction struct {
2023-12-30 10:35:51 -08:00
Id int ` db:"trns_id" json:"Id" `
Amount decimal . Decimal ` db:"trns_amount" json:"Amount" `
Description sql . NullString ` db:"trns_description" json:"Description" `
Account int ` db:"trns_account" json:"Account" `
Bucket sql . NullInt64 ` db:"trns_bucket" json:"Bucket" `
Date time . Time ` db:"trns_date" json:"TransactionDate" `
2023-12-19 22:03:09 -08:00
}
2023-12-19 20:51:52 -08:00
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 )
}
}
}
2023-12-17 21:21:29 -08:00
func main ( ) {
2023-12-19 20:13:32 -08:00
log . SetPrefix ( "RecountServer: " )
log . SetFlags ( 0 )
2023-12-19 22:03:09 -08:00
//
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" )
2023-12-30 09:20:38 -08:00
defer db . Close ( )
2023-12-19 22:03:09 -08:00
// Confirm a successful connection.
if err := db . Ping ( ) ; err != nil {
log . Fatal ( err )
}
log . Print ( "Database successfully pinged" )
transactions := [ ] Transaction { }
2023-12-21 16:44:34 -08:00
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" )
2023-12-19 22:03:09 -08:00
if err != nil {
log . Fatal ( err )
}
fmt . Print ( fmt . Sprintf ( "%+v\n" , transactions ) )
log . Print ( "Select has been run" )
for _ , trns := range transactions {
2023-12-21 16:44:34 -08:00
if trns . Description . Valid {
2023-12-30 10:35:51 -08:00
//bytes, err := json.Marshal(trns)
bytes , err := json . MarshalIndent ( trns , "" , "\t" )
if err != nil {
log . Fatal ( err )
}
log . Println ( string ( bytes ) )
2023-12-21 16:44:34 -08:00
} else {
log . Print ( "amount invalid" )
}
2023-12-19 22:03:09 -08:00
}
log . Print ( "select all run is done" )
2023-12-30 10:35:51 -08:00
/ *
jsonExample := ` {
"Id" : 3 ,
"Amount" : "100" ,
"Description" : {
"String" : "Transaction 3" ,
"Valid" : true
} ,
"Account" : 1 ,
"Bucket" : {
"Int64" : 1 ,
"Valid" : true
} ,
"TransactionDate" : "2023-11-11T00:00:00Z"
} `
var trns Transaction = Transaction { }
err = json . Unmarshal ( [ ] byte ( jsonExample ) , & trns )
if err != nil {
log . Println ( err )
} else {
log . Println ( trns . Amount )
}
* /
2023-12-19 20:51:52 -08:00
http . HandleFunc ( "/hello" , hello )
2023-12-30 10:35:51 -08:00
//http.HandleFunc("/headers", headers)
2023-12-19 20:51:52 -08:00
http . ListenAndServe ( ":8090" , nil )
2023-12-30 10:35:51 -08:00
if errors . Is ( err , http . ErrServerClosed ) {
fmt . Printf ( "server closed\n" )
} else if err != nil {
fmt . Printf ( "error starting server: %s\n" , err )
os . Exit ( 1 )
}
2023-12-19 20:51:52 -08:00
//fmt.Println("Hello World")
2023-12-17 21:21:29 -08:00
}