Compare commits
3 Commits
0397253ba7
...
86036afa4e
| Author | SHA1 | Date | |
|---|---|---|---|
| 86036afa4e | |||
| 1aa6e28b69 | |||
| b2451fed66 |
42
cmd/api-server/main.go
Normal file
42
cmd/api-server/main.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.rosemyrtle.work/personal-finance/server/internal/api"
|
||||||
|
|
||||||
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := godotenv.Load(); err != nil {
|
||||||
|
log.Fatal("error loading .env file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
database := fmt.Sprintf("postgres://%s:%s@%s/%s",
|
||||||
|
os.Getenv("PG_USER"),
|
||||||
|
os.Getenv("PG_PASSWORD"),
|
||||||
|
os.Getenv("PG_URL"),
|
||||||
|
os.Getenv("PG_DB"))
|
||||||
|
|
||||||
|
db, err := sql.Open("pgx", database)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("unable to connect to database: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Ping(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e := echo.New()
|
||||||
|
handlers := api.ServerImpl{Db: db}
|
||||||
|
api.RegisterHandlers(e, &handlers)
|
||||||
|
|
||||||
|
e.Logger.Fatal(e.Start(":9000"))
|
||||||
|
}
|
||||||
39
go.mod
39
go.mod
@ -1,3 +1,42 @@
|
|||||||
module git.rosemyrtle.work/personal-finance/server
|
module git.rosemyrtle.work/personal-finance/server
|
||||||
|
|
||||||
go 1.21.1
|
go 1.21.1
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||||
|
github.com/jackc/pgx/v5 v5.5.4
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
|
||||||
|
github.com/deepmap/oapi-codegen/v2 v2.1.0 // indirect
|
||||||
|
github.com/getkin/kin-openapi v0.123.0 // indirect
|
||||||
|
github.com/go-openapi/jsonpointer v0.20.2 // indirect
|
||||||
|
github.com/go-openapi/swag v0.22.8 // indirect
|
||||||
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
|
github.com/invopop/yaml v0.2.0 // indirect
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||||
|
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||||
|
github.com/joho/godotenv v1.5.1
|
||||||
|
github.com/josharian/intern v1.0.0 // indirect
|
||||||
|
github.com/labstack/echo/v4 v4.11.4 // indirect
|
||||||
|
github.com/labstack/gommon v0.4.2 // indirect
|
||||||
|
github.com/mailru/easyjson v0.7.7 // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
||||||
|
github.com/oapi-codegen/runtime v1.1.1 // indirect
|
||||||
|
github.com/perimeterx/marshmallow v1.1.5 // indirect
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
|
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||||
|
golang.org/x/crypto v0.17.0 // indirect
|
||||||
|
golang.org/x/mod v0.12.0 // indirect
|
||||||
|
golang.org/x/net v0.19.0 // indirect
|
||||||
|
golang.org/x/sync v0.3.0 // indirect
|
||||||
|
golang.org/x/sys v0.15.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
golang.org/x/tools v0.12.0 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
|
|||||||
82
go.sum
Normal file
82
go.sum
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||||
|
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||||
|
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
|
||||||
|
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
|
||||||
|
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
|
||||||
|
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/deepmap/oapi-codegen/v2 v2.1.0 h1:I/NMVhJCtuvL9x+S2QzZKpSjGi33oDZwPRdemvOZWyQ=
|
||||||
|
github.com/deepmap/oapi-codegen/v2 v2.1.0/go.mod h1:R1wL226vc5VmCNJUvMyYr3hJMm5reyv25j952zAVXZ8=
|
||||||
|
github.com/getkin/kin-openapi v0.123.0 h1:zIik0mRwFNLyvtXK274Q6ut+dPh6nlxBp0x7mNrPhs8=
|
||||||
|
github.com/getkin/kin-openapi v0.123.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
|
||||||
|
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
|
||||||
|
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs=
|
||||||
|
github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw=
|
||||||
|
github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI=
|
||||||
|
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||||
|
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
|
||||||
|
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||||
|
github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
|
||||||
|
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
|
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||||
|
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||||
|
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
|
||||||
|
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
|
||||||
|
github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
|
||||||
|
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
|
||||||
|
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||||
|
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
|
||||||
|
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||||
|
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
|
||||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
|
||||||
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||||
|
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
|
||||||
|
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
|
||||||
|
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
|
||||||
|
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
|
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||||
|
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||||
|
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
|
||||||
|
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
|
||||||
|
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
||||||
|
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
|
||||||
|
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
|
||||||
|
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
||||||
|
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||||
|
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||||
|
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
30
internal/api/converter.go
Normal file
30
internal/api/converter.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func convert[T any](rows *sql.Rows) []T {
|
||||||
|
var ans []T
|
||||||
|
for rows.Next() {
|
||||||
|
var r T
|
||||||
|
s := reflect.ValueOf(&r).Elem()
|
||||||
|
|
||||||
|
numCols := s.NumField()
|
||||||
|
columns := make([]interface{}, numCols)
|
||||||
|
|
||||||
|
for i := 0; i < numCols; i++ {
|
||||||
|
field := s.Field(i)
|
||||||
|
columns[i] = field.Addr().Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Scan(columns...); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ans = append(ans, r)
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
36
internal/api/impl.go
Normal file
36
internal/api/impl.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=api.cfg.yaml ../../docs/openapi.yaml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServerImpl struct {
|
||||||
|
Db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*ServerImpl) GetBanks(ctx echo.Context) error {
|
||||||
|
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*ServerImpl) GetBanksById(ctx echo.Context, bankId int64) error {
|
||||||
|
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pf *ServerImpl) GetTransactions(ctx echo.Context, params GetTransactionsParams) error {
|
||||||
|
rows, err := pf.Db.Query("SELECT tc.name, t.date, t.description, t.id, t.amount FROM pfbudget.transactions t LEFT JOIN pfbudget.transactions_categorized tc ON t.id = tc.id")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(http.StatusOK, convert[Transaction](rows))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*ServerImpl) GetTransactionsById(ctx echo.Context, transactionId int64) error {
|
||||||
|
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||||
|
}
|
||||||
65
internal/api/impl_test.go
Normal file
65
internal/api/impl_test.go
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetTransactions(t *testing.T) {
|
||||||
|
db, mock, err := sqlmock.New()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e := echo.New()
|
||||||
|
handlers := ServerImpl{db}
|
||||||
|
RegisterHandlers(e, &handlers)
|
||||||
|
|
||||||
|
t.Run("when successful", func(t *testing.T) {
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/transactions", nil)
|
||||||
|
|
||||||
|
date := time.Now()
|
||||||
|
rows := mock.NewRows([]string{"category", "date", "description", "id", "amount"}).
|
||||||
|
AddRow(nil, date, "#1", 1, 1000).
|
||||||
|
AddRow("expense", date, "#2", 2, -1000)
|
||||||
|
mock.ExpectQuery("SELECT t.category, t.date, t.description, t.id, tc.amount FROM pfbudget.transactions t LEFT JOIN pfbudget.transactions_categorized tc ON t.id = tc.id").WillReturnRows(rows)
|
||||||
|
|
||||||
|
ctx := e.NewContext(req, rec)
|
||||||
|
err := handlers.GetTransactions(ctx, GetTransactionsParams{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Error(rec.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `[
|
||||||
|
{
|
||||||
|
"date":"` + date.Format(time.DateOnly) + `",
|
||||||
|
"description": "#1",
|
||||||
|
"id":1,
|
||||||
|
"value":1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"category": "expense",
|
||||||
|
"date":"` + date.Format(time.DateOnly) + `",
|
||||||
|
"description": "#2",
|
||||||
|
"id":2,
|
||||||
|
"value":-1000
|
||||||
|
}
|
||||||
|
]`
|
||||||
|
expected = strings.Join(strings.Fields(expected), "")
|
||||||
|
log.Println(expected)
|
||||||
|
if ret := strings.TrimRight(rec.Body.String(), "\n"); ret != expected {
|
||||||
|
t.Error(ret, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
312
internal/api/server.gen.go
Normal file
312
internal/api/server.gen.go
Normal file
@ -0,0 +1,312 @@
|
|||||||
|
// Package api provides primitives to interact with the openapi HTTP API.
|
||||||
|
//
|
||||||
|
// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT.
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/getkin/kin-openapi/openapi3"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/oapi-codegen/runtime"
|
||||||
|
openapi_types "github.com/oapi-codegen/runtime/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Bank defines model for Bank.
|
||||||
|
type Bank struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
NordigenId *openapi_types.UUID `json:"nordigenId,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Banks defines model for Banks.
|
||||||
|
type Banks = []Bank
|
||||||
|
|
||||||
|
// Manually added date time that implements the Scanner interface
|
||||||
|
type ScannableDate struct {
|
||||||
|
openapi_types.Date
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ScannableDate) Scan(src any) error {
|
||||||
|
switch s := src.(type) {
|
||||||
|
case time.Time:
|
||||||
|
d.Time = s
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("column is not convertible to date")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transaction defines model for Transaction.
|
||||||
|
type Transaction struct {
|
||||||
|
Category *string `json:"category,omitempty"`
|
||||||
|
Date ScannableDate `json:"date"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Id int64 `json:"id"`
|
||||||
|
Value float32 `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transactions defines model for Transactions.
|
||||||
|
type Transactions = []Transaction
|
||||||
|
|
||||||
|
// GetTransactionsParams defines parameters for GetTransactions.
|
||||||
|
type GetTransactionsParams struct {
|
||||||
|
// Category filter by transaction category
|
||||||
|
Category *string `form:"category,omitempty" json:"category,omitempty"`
|
||||||
|
|
||||||
|
// Limit number of transactions to return
|
||||||
|
Limit *int32 `form:"limit,omitempty" json:"limit,omitempty"`
|
||||||
|
|
||||||
|
// Offset offset from where to retrieve transactions
|
||||||
|
Offset *int32 `form:"offset,omitempty" json:"offset,omitempty"`
|
||||||
|
|
||||||
|
// Bank ID of the bank
|
||||||
|
Bank *string `form:"bank,omitempty" json:"bank,omitempty"`
|
||||||
|
|
||||||
|
// Sort field name by which to sort
|
||||||
|
Sort *string `form:"sort,omitempty" json:"sort,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServerInterface represents all server handlers.
|
||||||
|
type ServerInterface interface {
|
||||||
|
// Retrieve existing banks
|
||||||
|
// (GET /banks)
|
||||||
|
GetBanks(ctx echo.Context) error
|
||||||
|
// Find bank by ID
|
||||||
|
// (GET /banks/{bankId})
|
||||||
|
GetBanksById(ctx echo.Context, bankId int64) error
|
||||||
|
// Retrieve existing transactions
|
||||||
|
// (GET /transactions)
|
||||||
|
GetTransactions(ctx echo.Context, params GetTransactionsParams) error
|
||||||
|
// Find transaction by ID
|
||||||
|
// (GET /transactions/{transactionId})
|
||||||
|
GetTransactionsById(ctx echo.Context, transactionId int64) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServerInterfaceWrapper converts echo contexts to parameters.
|
||||||
|
type ServerInterfaceWrapper struct {
|
||||||
|
Handler ServerInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBanks converts echo context to params.
|
||||||
|
func (w *ServerInterfaceWrapper) GetBanks(ctx echo.Context) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// Invoke the callback with all the unmarshaled arguments
|
||||||
|
err = w.Handler.GetBanks(ctx)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBanksById converts echo context to params.
|
||||||
|
func (w *ServerInterfaceWrapper) GetBanksById(ctx echo.Context) error {
|
||||||
|
var err error
|
||||||
|
// ------------- Path parameter "bankId" -------------
|
||||||
|
var bankId int64
|
||||||
|
|
||||||
|
err = runtime.BindStyledParameterWithOptions("simple", "bankId", ctx.Param("bankId"), &bankId, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter bankId: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invoke the callback with all the unmarshaled arguments
|
||||||
|
err = w.Handler.GetBanksById(ctx, bankId)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTransactions converts echo context to params.
|
||||||
|
func (w *ServerInterfaceWrapper) GetTransactions(ctx echo.Context) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// Parameter object where we will unmarshal all parameters from the context
|
||||||
|
var params GetTransactionsParams
|
||||||
|
// ------------- Optional query parameter "category" -------------
|
||||||
|
|
||||||
|
err = runtime.BindQueryParameter("form", true, false, "category", ctx.QueryParams(), ¶ms.Category)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter category: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------- Optional query parameter "limit" -------------
|
||||||
|
|
||||||
|
err = runtime.BindQueryParameter("form", true, false, "limit", ctx.QueryParams(), ¶ms.Limit)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter limit: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------- Optional query parameter "offset" -------------
|
||||||
|
|
||||||
|
err = runtime.BindQueryParameter("form", true, false, "offset", ctx.QueryParams(), ¶ms.Offset)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter offset: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------- Optional query parameter "bank" -------------
|
||||||
|
|
||||||
|
err = runtime.BindQueryParameter("form", true, false, "bank", ctx.QueryParams(), ¶ms.Bank)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter bank: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------- Optional query parameter "sort" -------------
|
||||||
|
|
||||||
|
err = runtime.BindQueryParameter("form", true, false, "sort", ctx.QueryParams(), ¶ms.Sort)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter sort: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invoke the callback with all the unmarshaled arguments
|
||||||
|
err = w.Handler.GetTransactions(ctx, params)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTransactionsById converts echo context to params.
|
||||||
|
func (w *ServerInterfaceWrapper) GetTransactionsById(ctx echo.Context) error {
|
||||||
|
var err error
|
||||||
|
// ------------- Path parameter "transactionId" -------------
|
||||||
|
var transactionId int64
|
||||||
|
|
||||||
|
err = runtime.BindStyledParameterWithOptions("simple", "transactionId", ctx.Param("transactionId"), &transactionId, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter transactionId: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invoke the callback with all the unmarshaled arguments
|
||||||
|
err = w.Handler.GetTransactionsById(ctx, transactionId)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a simple interface which specifies echo.Route addition functions which
|
||||||
|
// are present on both echo.Echo and echo.Group, since we want to allow using
|
||||||
|
// either of them for path registration
|
||||||
|
type EchoRouter interface {
|
||||||
|
CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterHandlers adds each server route to the EchoRouter.
|
||||||
|
func RegisterHandlers(router EchoRouter, si ServerInterface) {
|
||||||
|
RegisterHandlersWithBaseURL(router, si, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Registers handlers, and prepends BaseURL to the paths, so that the paths
|
||||||
|
// can be served under a prefix.
|
||||||
|
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {
|
||||||
|
|
||||||
|
wrapper := ServerInterfaceWrapper{
|
||||||
|
Handler: si,
|
||||||
|
}
|
||||||
|
|
||||||
|
router.GET(baseURL+"/banks", wrapper.GetBanks)
|
||||||
|
router.GET(baseURL+"/banks/:bankId", wrapper.GetBanksById)
|
||||||
|
router.GET(baseURL+"/transactions", wrapper.GetTransactions)
|
||||||
|
router.GET(baseURL+"/transactions/:transactionId", wrapper.GetTransactionsById)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Base64 encoded, gzipped, json marshaled Swagger object
|
||||||
|
var swaggerSpec = []string{
|
||||||
|
|
||||||
|
"H4sIAAAAAAAC/8xWX2/bNhD/KgS3R0VS6mAPetqKoIGBoQvW7anrAy2dZHb8o5JHZ0bg7z4cKTuSJdcZ",
|
||||||
|
"sA57iSXzePf7c3fOM6+t7q0Bg55Xz9zXW9AiPr4V5k/67J3twaGE+K1s6C/ue+AV9+ik6fgh40ZoWD6w",
|
||||||
|
"rpEdmHW811qnBfKKhyAbnp2HHzLu4EuQDhpefeQxJGb+dAq1m89QI2UmfAkSgo4P3ztoecW/K144FQOh",
|
||||||
|
"IrI5nNII58Se3n9zwnhRo7RmTrYWCJ11+0VmjUCYcIpfZAuB4Gsn+2OJ2bmcSiMN/nDHM66lkTpoXpWn",
|
||||||
|
"nNIgdODo0k6oMC3fKivwpb4JekOhS5oOSMfAjhmXlB5p9HrBx8LOdCdU0rQ2imwNihrpEbSQiqhII0wN",
|
||||||
|
"P+rgUOQN7GYy8kdw3hqh2LsUyz6A24H7g5goWYPxUZ3Ul/zh/e/sAQw4odhj2ChZs59TENut8pJZx5RA",
|
||||||
|
"cDzjwRGCLWLvq6J4enrKOxNy67piSOuLrlc3q7y88ShMI5Q1kG9Rq0hTooIleDfslx7MT49rtspLUhuc",
|
||||||
|
"T0zKvMxv6a7twYhe8oqv8jJf8Yz3ArdR6GJzbPYOolLUpYKkoLniD4BpGshr31tCSVFvyvKoMJh4T/S9",
|
||||||
|
"knW8WXz2qSGTY68ZIJ+cmzrxIdQ1eN8GxU6oiM5deUc5p8HvLdsMiTLug9aCpov/Cugk7IDBX9KjNN0o",
|
||||||
|
"KnEvnulj3RyuivB2v26idk5oQHCeVx/PYazvmW1jDYaWOcDgqHEknZHox71T8VSVj4cIXYBspNpscs+n",
|
||||||
|
"9fDpG/vyz2wp57aszU4o2bD1PfOBoEBz0UIqyIxF1tpgmjMj30nTJF03e7a+Twbi2f64ZN9kz1xxsJUK",
|
||||||
|
"wVGVUXZ2WtiDl18CxJfBzNHxi7gmKCU2NLXJ2Nlv0nnptFmpgcbE5o10VlxJLXFSuYFWBIW8ui3LbNJG",
|
||||||
|
"qzdXfgDmqGzbekDWOqvZ0xYcDIDSXOFU2iV8KcEywJvbBXzXMaUxwy3ElrhQdzh6qXrVgFaCahhdJ/+f",
|
||||||
|
"trLeEllvHV6oMRxdrvEtJ3TS1v/CAsVpvq/v0VnwZBqL59HbleU6pvH6HTueziurdgLlf7xxJ//Z/IeL",
|
||||||
|
"d1T36/t3rPlxDR8OfwcAAP//XTQsSusLAAA=",
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSwagger returns the content of the embedded swagger specification file
|
||||||
|
// or error if failed to decode
|
||||||
|
func decodeSpec() ([]byte, error) {
|
||||||
|
zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, ""))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error base64 decoding spec: %w", err)
|
||||||
|
}
|
||||||
|
zr, err := gzip.NewReader(bytes.NewReader(zipped))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error decompressing spec: %w", err)
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, err = buf.ReadFrom(zr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error decompressing spec: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawSpec = decodeSpecCached()
|
||||||
|
|
||||||
|
// a naive cached of a decoded swagger spec
|
||||||
|
func decodeSpecCached() func() ([]byte, error) {
|
||||||
|
data, err := decodeSpec()
|
||||||
|
return func() ([]byte, error) {
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructs a synthetic filesystem for resolving external references when loading openapi specifications.
|
||||||
|
func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) {
|
||||||
|
res := make(map[string]func() ([]byte, error))
|
||||||
|
if len(pathToFile) > 0 {
|
||||||
|
res[pathToFile] = rawSpec
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSwagger returns the Swagger specification corresponding to the generated code
|
||||||
|
// in this file. The external references of Swagger specification are resolved.
|
||||||
|
// The logic of resolving external references is tightly connected to "import-mapping" feature.
|
||||||
|
// Externally referenced files must be embedded in the corresponding golang packages.
|
||||||
|
// Urls can be supported but this task was out of the scope.
|
||||||
|
func GetSwagger() (swagger *openapi3.T, err error) {
|
||||||
|
resolvePath := PathToRawSpec("")
|
||||||
|
|
||||||
|
loader := openapi3.NewLoader()
|
||||||
|
loader.IsExternalRefsAllowed = true
|
||||||
|
loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) {
|
||||||
|
pathToFile := url.String()
|
||||||
|
pathToFile = path.Clean(pathToFile)
|
||||||
|
getSpec, ok := resolvePath[pathToFile]
|
||||||
|
if !ok {
|
||||||
|
err1 := fmt.Errorf("path not found: %s", pathToFile)
|
||||||
|
return nil, err1
|
||||||
|
}
|
||||||
|
return getSpec()
|
||||||
|
}
|
||||||
|
var specData []byte
|
||||||
|
specData, err = rawSpec()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
swagger, err = loader.LoadFromData(specData)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user