using chi routing
This commit is contained in:
parent
407d7ea9a5
commit
a88a18fe0e
4 changed files with 119 additions and 50 deletions
75
api.go
Normal file
75
api.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
//"io/ioutil"
|
||||
|
||||
//"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/ggicci/httpin"
|
||||
)
|
||||
|
||||
const DEFAULT_RESULT_COUNT = 50;
|
||||
|
||||
type GetTransactionPaginationInput struct {
|
||||
ResultCount int `in:"query=result_count"`
|
||||
PageNum int `in:"query=page_num"`
|
||||
}
|
||||
|
||||
func apiRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
//r.Use(ApiLoginRequired)
|
||||
r.With(
|
||||
httpin.NewInput(GetTransactionPaginationInput{}),
|
||||
).Get("/get_transactions", getTransactions)
|
||||
r.Post("/new_transaction", newTransaction)
|
||||
return r
|
||||
}
|
||||
|
||||
func getTransactions(w http.ResponseWriter, req *http.Request) {
|
||||
db, err := sqlx.Connect("postgres", "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
input := req.Context().Value(httpin.Input).(*GetTransactionPaginationInput)
|
||||
|
||||
if input.ResultCount == 0 {
|
||||
input.ResultCount = DEFAULT_RESULT_COUNT
|
||||
}
|
||||
|
||||
transactions := []Transaction{}
|
||||
|
||||
err = db.Select(&transactions,
|
||||
"SELECT trns_id, trns_amount, trns_description, " +
|
||||
"trns_account, trns_bucket, trns_date " +
|
||||
"FROM rcnt.transactions ORDER BY trns_id DESC " +
|
||||
fmt.Sprintf("OFFSET %d ROWS FETCH NEXT %d ROWS ONLY",
|
||||
input.PageNum, input.ResultCount))
|
||||
|
||||
for _, trns := range transactions {
|
||||
//bytes, err := json.Marshal(trns)
|
||||
bytes, err := json.MarshalIndent(trns, "", "\t")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Fprintf(w, string(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
func newTransaction(w http.ResponseWriter, req *http.Request) {
|
||||
fmt.Fprintf(w, "ding")
|
||||
/*
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
*/
|
||||
}
|
4
go.mod
4
go.mod
|
@ -3,7 +3,11 @@ module nickiel.net/recount_server
|
|||
go 1.21.5
|
||||
|
||||
require (
|
||||
github.com/ggicci/httpin v0.14.2
|
||||
github.com/go-chi/chi/v5 v5.0.11
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/shopspring/decimal v1.3.1
|
||||
)
|
||||
|
||||
require github.com/ggicci/owl v0.4.0 // indirect
|
||||
|
|
20
go.sum
20
go.sum
|
@ -1,9 +1,29 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/ggicci/httpin v0.14.2 h1:rGrG/OgXg3XZbRBBcqf8TSeu1nW9XX6L9F42XGhwgRQ=
|
||||
github.com/ggicci/httpin v0.14.2/go.mod h1:m/RhY5rRPkNQs4VMPK66LxBX4ZMPxPyXQvrnmBEo2Y8=
|
||||
github.com/ggicci/owl v0.4.0 h1:1cwRlynLe6P5ylLyjNWtOP5qVO1bYUmziPYVbOphGHQ=
|
||||
github.com/ggicci/owl v0.4.0/go.mod h1:TRPWshRwYej6uES//YW5aNgLB370URwyta1Ytfs7KXs=
|
||||
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
|
||||
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
|
||||
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
|
||||
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
|
||||
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
|
||||
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
|
||||
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -2,18 +2,15 @@ package main
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
// "json:"json_code_name,omitempty"" (omit empty)
|
||||
|
@ -43,43 +40,6 @@ func main() {
|
|||
log.SetPrefix("RecountServer: ")
|
||||
log.SetFlags(0)
|
||||
|
||||
//
|
||||
db, err := sqlx.Connect("postgres", "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Print("Database connected")
|
||||
defer db.Close()
|
||||
|
||||
// Confirm a successful connection.
|
||||
if err := db.Ping(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Print("Database successfully pinged")
|
||||
|
||||
transactions := []Transaction{}
|
||||
err = db.Select(&transactions, "SELECT trns_id, trns_amount, trns_description, trns_account, trns_bucket, trns_date FROM rcnt.transactions ORDER BY trns_id DESC")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Print(fmt.Sprintf("%+v\n", transactions))
|
||||
log.Print("Select has been run")
|
||||
|
||||
for _, trns := range transactions {
|
||||
if trns.Description.Valid {
|
||||
//bytes, err := json.Marshal(trns)
|
||||
bytes, err := json.MarshalIndent(trns, "", "\t")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println(string(bytes))
|
||||
} else {
|
||||
log.Print("amount invalid")
|
||||
}
|
||||
}
|
||||
|
||||
log.Print("select all run is done")
|
||||
|
||||
/*
|
||||
jsonExample := `{
|
||||
"Id": 3,
|
||||
|
@ -105,17 +65,27 @@ func main() {
|
|||
log.Println(trns.Amount)
|
||||
}
|
||||
*/
|
||||
r := chi.NewRouter()
|
||||
|
||||
http.HandleFunc("/hello", hello)
|
||||
//http.HandleFunc("/headers", headers)
|
||||
// A good base middleware stack
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.Logger)
|
||||
|
||||
http.ListenAndServe(":8090", nil)
|
||||
// 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))
|
||||
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
fmt.Printf("server closed\n")
|
||||
} else if err != nil {
|
||||
fmt.Printf("error starting server: %s\n", err)
|
||||
os.Exit(1)
|
||||
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")
|
||||
}
|
Loading…
Reference in a new issue