Recount-Server/main.go

92 lines
2.2 KiB
Go
Raw Normal View History

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
"net/http"
2023-12-19 20:13:32 -08:00
"fmt"
"log"
2023-12-19 22:03:09 -08:00
"time"
2023-12-19 20:51:52 -08:00
2023-12-30 14:10:41 -08:00
//"github.com/shopspring/decimal"
2023-12-30 13:51:59 -08:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
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"`
2023-12-30 14:10:41 -08:00
Amount string `db:"trns_amount" json:"Amount"`
2023-12-30 10:35:51 -08:00
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-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-30 13:51:59 -08:00
r := chi.NewRouter()
2023-12-30 10:35:51 -08:00
2023-12-30 13:51:59 -08:00
// A good base middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
r.Use(middleware.Logger)
2023-12-19 20:51:52 -08:00
2023-12-30 13:51:59 -08:00
// 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))
2023-12-19 20:51:52 -08:00
2023-12-30 13:51:59 -08:00
r.Get("/", hello)
r.Get("/headers", headers)
r.Mount("/api", apiRouter())
err := http.ListenAndServe(":8090", r)
if err != nil {
log.Fatal(err)
2023-12-30 10:35:51 -08:00
}
2023-12-30 13:51:59 -08:00
2023-12-19 20:51:52 -08:00
//fmt.Println("Hello World")
2023-12-17 21:21:29 -08:00
}