86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"nickiel.net/recount_server/tests"
|
|
|
|
"database/sql"
|
|
"net/http"
|
|
|
|
"fmt"
|
|
"flag"
|
|
"log"
|
|
"time"
|
|
|
|
//"github.com/shopspring/decimal"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
)
|
|
|
|
var DB_TYPE = "postgres"
|
|
var DB_SCHEMA = "rcnt."
|
|
var DB_CONNECTION_STRING string = "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable"
|
|
|
|
// "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)
|
|
|
|
var nFlag = flag.Bool("d", false, "whether to enable debug mode")
|
|
flag.Parse()
|
|
|
|
if *nFlag {
|
|
log.Println("Is debugging")
|
|
DB_TYPE = "sqlite3"
|
|
DB_SCHEMA = ""
|
|
DB_CONNECTION_STRING = "test.db"
|
|
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
|
|
}
|
|
|
|
log.Print("starting server")
|
|
|
|
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")
|
|
}
|