104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"nickiel.net/recount_server/tests"
|
|
|
|
"database/sql"
|
|
"net/http"
|
|
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
//"github.com/shopspring/decimal"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
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() {
|
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
var debugFlag = flag.Bool("d", false, "whether to enable debug mode")
|
|
var traceFlag = flag.Bool("t", false, "whether to trace logging")
|
|
|
|
flag.Parse()
|
|
|
|
if *traceFlag {
|
|
zerolog.SetGlobalLevel(zerolog.TraceLevel)
|
|
log.Debug().Msg("Enabling trace level debugging")
|
|
}
|
|
|
|
if *debugFlag {
|
|
if !*traceFlag {
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
}
|
|
log.Debug().Msg("Is debugging")
|
|
DB_TYPE = "sqlite3"
|
|
DB_SCHEMA = ""
|
|
DB_CONNECTION_STRING = "test.db"
|
|
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
|
|
}
|
|
|
|
|
|
debug_mode.SetLogLevel(zerolog.GlobalLevel())
|
|
|
|
log.Info().Msg("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(err).
|
|
Msg("Could not open server connection")
|
|
}
|
|
|
|
//fmt.Println("Hello World")
|
|
}
|