ran gofmt
This commit is contained in:
parent
d5fc3d8d23
commit
da14c059c1
5 changed files with 219 additions and 204 deletions
6
api.go
6
api.go
|
@ -1,18 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
//"context"
|
//"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
"github.com/ggicci/httpin"
|
"github.com/ggicci/httpin"
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DEFAULT_RESULT_COUNT = 50;
|
const DEFAULT_RESULT_COUNT = 50
|
||||||
|
|
||||||
type GetTransactionPaginationInput struct {
|
type GetTransactionPaginationInput struct {
|
||||||
ResultCount int `in:"query=result_count"`
|
ResultCount int `in:"query=result_count"`
|
||||||
|
|
18
db.go
18
db.go
|
@ -3,13 +3,13 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func db_get_transactions(transactions *[]Transaction,r *GetTransactionPaginationInput) (error) {
|
func db_get_transactions(transactions *[]Transaction, r *GetTransactionPaginationInput) error {
|
||||||
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
|
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().
|
log.Fatal().
|
||||||
|
@ -20,18 +20,18 @@ func db_get_transactions(transactions *[]Transaction,r *GetTransactionPagination
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
err = db.Select(transactions,
|
err = db.Select(transactions,
|
||||||
"SELECT trns_id, trns_amount, trns_description, " +
|
"SELECT trns_id, trns_amount, trns_description, "+
|
||||||
"trns_account, trns_bucket, trns_date " +
|
"trns_account, trns_bucket, trns_date "+
|
||||||
fmt.Sprintf("FROM %stransactions ORDER BY trns_id DESC ", DB_SCHEMA) +
|
fmt.Sprintf("FROM %stransactions ORDER BY trns_id DESC ", DB_SCHEMA)+
|
||||||
fmt.Sprintf("LIMIT %d OFFSET %d",
|
fmt.Sprintf("LIMIT %d OFFSET %d",
|
||||||
r.ResultCount, r.PageNum * r.ResultCount ))
|
r.ResultCount, r.PageNum*r.ResultCount))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func db_new_transaction(transaction Transaction) (error) {
|
func db_new_transaction(transaction Transaction) error {
|
||||||
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
|
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info()
|
log.Info()
|
||||||
|
@ -45,8 +45,8 @@ func db_new_transaction(transaction Transaction) (error) {
|
||||||
log.Debug().Msgf("%#v", transaction)
|
log.Debug().Msgf("%#v", transaction)
|
||||||
|
|
||||||
_, err = db.NamedExec(
|
_, err = db.NamedExec(
|
||||||
fmt.Sprintf("INSERT INTO %stransactions", DB_SCHEMA) +
|
fmt.Sprintf("INSERT INTO %stransactions", DB_SCHEMA)+
|
||||||
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" +
|
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)"+
|
||||||
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",
|
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",
|
||||||
transaction)
|
transaction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
4
main.go
4
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"nickiel.net/recount_server/tests"
|
"nickiel.net/recount_server/tests"
|
||||||
|
"nickiel.net/recount_server/web"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -71,7 +72,6 @@ func main() {
|
||||||
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
|
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
debug_mode.SetLogLevel(zerolog.GlobalLevel())
|
debug_mode.SetLogLevel(zerolog.GlobalLevel())
|
||||||
|
|
||||||
log.Info().Msg("starting server")
|
log.Info().Msg("starting server")
|
||||||
|
@ -89,8 +89,8 @@ func main() {
|
||||||
// processing should be stopped.
|
// processing should be stopped.
|
||||||
//r.Use(middleware.Timeout(60 * time.Second))
|
//r.Use(middleware.Timeout(60 * time.Second))
|
||||||
|
|
||||||
r.Get("/", hello)
|
|
||||||
r.Get("/headers", headers)
|
r.Get("/headers", headers)
|
||||||
|
r.Mount("/", web.WebRouter())
|
||||||
r.Mount("/api", apiRouter())
|
r.Mount("/api", apiRouter())
|
||||||
|
|
||||||
err := http.ListenAndServe(":8090", r)
|
err := http.ListenAndServe(":8090", r)
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package debug_mode
|
package debug_mode
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"encoding/json"
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
@ -149,13 +149,11 @@ INSERT INTO transactions (trns_amount, trns_description, trns_account, trns_buck
|
||||||
log.Fatal().Err(err).Msg("could not unmarshal")
|
log.Fatal().Err(err).Msg("could not unmarshal")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.NamedExec("INSERT INTO transactions" +
|
_, err = db.NamedExec("INSERT INTO transactions"+
|
||||||
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" +
|
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)"+
|
||||||
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",
|
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",
|
||||||
trns)
|
trns)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
log.Debug().Msg("Test database initialized")
|
log.Debug().Msg("Test database initialized")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
17
web/router.go
Normal file
17
web/router.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WebRouter() http.Handler {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", getIndex)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIndex(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue