Recount-Server/main.go

105 lines
2.6 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 (
2024-01-01 20:47:34 -08:00
"nickiel.net/recount_server/tests"
2024-01-11 20:07:47 -08:00
"nickiel.net/recount_server/web"
2023-12-31 17:21:09 -08:00
2024-01-01 20:47:34 -08:00
"database/sql"
"net/http"
2023-12-30 10:35:51 -08:00
2024-01-01 20:47:34 -08:00
"flag"
"fmt"
2024-01-01 22:18:43 -08:00
"os"
2024-01-01 20:47:34 -08:00
"time"
2023-12-19 20:51:52 -08:00
2024-01-01 20:47:34 -08:00
//"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"
2023-12-19 20:13:32 -08:00
)
2023-12-17 21:21:29 -08:00
2023-12-31 17:21:09 -08:00
var DB_TYPE = "postgres"
var DB_SCHEMA = "rcnt."
2023-12-30 15:16:26 -08:00
var DB_CONNECTION_STRING string = "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable"
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 {
2024-01-11 20:07:47 -08:00
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"`
2023-12-19 22:03:09 -08:00
}
2023-12-19 20:51:52 -08:00
func hello(w http.ResponseWriter, req *http.Request) {
2024-01-11 20:07:47 -08:00
fmt.Fprintf(w, "hello\n")
2023-12-19 20:51:52 -08:00
}
func headers(w http.ResponseWriter, req *http.Request) {
2024-01-11 20:07:47 -08:00
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
2023-12-19 20:51:52 -08:00
}
2023-12-17 21:21:29 -08:00
func main() {
2024-01-11 20:07:47 -08:00
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("/headers", headers)
r.Mount("/", web.WebRouter())
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")
2023-12-17 21:21:29 -08:00
}