Recount-Server/main.go

91 lines
2.2 KiB
Go

package main
import (
"database/sql"
"net/http"
"fmt"
"log"
"time"
//"github.com/shopspring/decimal"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
// "json:"json_code_name,omitempty"" (omit empty)
// if you use `json:"-"` it doesn't encode it
type Transaction struct {
Id int `db:"trns_id" json:"Id"`
Amount string `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"`
}
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)
/*
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)
}
*/
r := chi.NewRouter()
// A good base middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
r.Use(middleware.Logger)
// Set a timeout value on the request context (ctx), that will signal
// through ctx.Done() that the request has timed out and further
// processing should be stopped.
//r.Use(middleware.Timeout(60 * time.Second))
r.Get("/", hello)
r.Get("/headers", headers)
r.Mount("/api", apiRouter())
err := http.ListenAndServe(":8090", r)
if err != nil {
log.Fatal(err)
}
//fmt.Println("Hello World")
}