Recount-Server/main.go

87 lines
2.3 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-31 17:21:09 -08:00
"nickiel.net/recount_server/tests"
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"
2023-12-30 15:16:26 -08:00
"flag"
2023-12-19 20:13:32 -08:00
"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-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 {
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 15:16:26 -08:00
var nFlag = flag.Bool("d", false, "whether to enable debug mode")
flag.Parse()
2023-12-30 10:35:51 -08:00
2023-12-30 15:16:26 -08:00
if *nFlag {
log.Println("Is debugging")
2023-12-31 17:21:09 -08:00
DB_TYPE = "sqlite3"
DB_SCHEMA = ""
DB_CONNECTION_STRING = "test.db"
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
2023-12-30 10:35:51 -08:00
}
2023-12-30 15:16:26 -08:00
2023-12-31 17:21:09 -08:00
log.Print("starting server")
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
}