Skip to content

Commit 23f3fb1

Browse files
committed
fix: gin support and tests refactor
1 parent 2ba171a commit 23f3fb1

11 files changed

Lines changed: 576 additions & 256 deletions

File tree

ROADMAP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## Framework support
1414

15-
- [ ] Gin - https://github.com/gin-gonic/gin - 83k
15+
- [-] Gin - https://github.com/gin-gonic/gin - 83k
1616
- [-] Fiber - https://github.com/gofiber/fiber - 37k
1717
- [ ] Beego - https://github.com/beego/beego - 32k
1818
- [ ] Echo - https://github.com/labstack/echo - 31k

go.mod

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,47 @@ module github.com/go-simpl/simplapi
33
go 1.20
44

55
require (
6+
github.com/gin-gonic/gin v1.10.1
67
github.com/gofiber/fiber/v2 v2.52.8
78
github.com/stretchr/testify v1.10.0
89
)
910

1011
require (
1112
github.com/andybalholm/brotli v1.1.0 // indirect
13+
github.com/bytedance/sonic v1.11.6 // indirect
14+
github.com/bytedance/sonic/loader v0.1.1 // indirect
15+
github.com/cloudwego/base64x v0.1.4 // indirect
16+
github.com/cloudwego/iasm v0.2.0 // indirect
1217
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
19+
github.com/gin-contrib/sse v0.1.0 // indirect
20+
github.com/go-playground/locales v0.14.1 // indirect
21+
github.com/go-playground/universal-translator v0.18.1 // indirect
22+
github.com/go-playground/validator/v10 v10.20.0 // indirect
23+
github.com/goccy/go-json v0.10.2 // indirect
1324
github.com/google/uuid v1.6.0 // indirect
25+
github.com/json-iterator/go v1.1.12 // indirect
1426
github.com/klauspost/compress v1.17.9 // indirect
27+
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
28+
github.com/leodido/go-urn v1.4.0 // indirect
1529
github.com/mattn/go-colorable v0.1.13 // indirect
1630
github.com/mattn/go-isatty v0.0.20 // indirect
1731
github.com/mattn/go-runewidth v0.0.16 // indirect
32+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
33+
github.com/modern-go/reflect2 v1.0.2 // indirect
34+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
1835
github.com/pmezard/go-difflib v1.0.0 // indirect
1936
github.com/rivo/uniseg v0.2.0 // indirect
37+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
38+
github.com/ugorji/go/codec v1.2.12 // indirect
2039
github.com/valyala/bytebufferpool v1.0.0 // indirect
2140
github.com/valyala/fasthttp v1.51.0 // indirect
2241
github.com/valyala/tcplisten v1.0.0 // indirect
42+
golang.org/x/arch v0.8.0 // indirect
43+
golang.org/x/crypto v0.23.0 // indirect
44+
golang.org/x/net v0.25.0 // indirect
2345
golang.org/x/sys v0.28.0 // indirect
46+
golang.org/x/text v0.15.0 // indirect
47+
google.golang.org/protobuf v1.34.1 // indirect
2448
gopkg.in/yaml.v3 v3.0.1 // indirect
2549
)

go.sum

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,109 @@
11
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
22
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
3+
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
4+
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
5+
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
6+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
7+
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
8+
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
9+
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
10+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
11+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
312
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
413
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
15+
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
16+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
17+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
18+
github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
19+
github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
20+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
21+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
22+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
23+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
24+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
25+
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
26+
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
27+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
28+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
529
github.com/gofiber/fiber/v2 v2.52.8 h1:xl4jJQ0BV5EJTA2aWiKw/VddRpHrKeZLF0QPUxqn0x4=
630
github.com/gofiber/fiber/v2 v2.52.8/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
31+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
32+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
733
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
834
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
35+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
36+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
937
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
1038
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
39+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
40+
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
41+
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
42+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
43+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
44+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
1145
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
1246
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
1347
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
1448
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
1549
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
1650
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
1751
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
52+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
53+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
54+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
55+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
56+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
57+
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
58+
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
1859
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1960
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2061
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
2162
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
63+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
64+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
65+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
66+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
67+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
68+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
69+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
70+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
71+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
72+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
73+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2274
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
2375
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
76+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
77+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
78+
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
79+
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
2480
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
2581
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
2682
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
2783
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
2884
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
2985
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
86+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
87+
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
88+
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
89+
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
90+
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
91+
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
92+
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
3093
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
94+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3195
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3296
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
3397
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
98+
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
99+
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
100+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
101+
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
102+
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
34103
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
35104
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
105+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36106
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
37107
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
108+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
109+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package ginframework
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/go-simpl/simplapi/pkg/framework"
9+
)
10+
11+
type ginFramework struct {
12+
engine *gin.Engine
13+
}
14+
15+
func New(engine *gin.Engine) framework.Framework {
16+
return &ginFramework{
17+
engine: engine,
18+
}
19+
}
20+
21+
func (g *ginFramework) GET(path string, handler framework.FrameworkHandler) {
22+
g.engine.GET(path, func(c *gin.Context) {
23+
err := handler(NewRequest(c), NewResponse(c))
24+
if err != nil {
25+
c.AbortWithError(http.StatusInternalServerError, err)
26+
}
27+
})
28+
}
29+
30+
func (g *ginFramework) POST(path string, handler framework.FrameworkHandler) {
31+
g.engine.POST(path, func(c *gin.Context) {
32+
err := handler(NewRequest(c), NewResponse(c))
33+
if err != nil {
34+
c.AbortWithError(http.StatusInternalServerError, err)
35+
}
36+
})
37+
}
38+
39+
func (g *ginFramework) PUT(path string, handler framework.FrameworkHandler) {
40+
g.engine.PUT(path, func(c *gin.Context) {
41+
err := handler(NewRequest(c), NewResponse(c))
42+
if err != nil {
43+
c.AbortWithError(http.StatusInternalServerError, err)
44+
}
45+
})
46+
}
47+
48+
func (g *ginFramework) PATCH(path string, handler framework.FrameworkHandler) {
49+
g.engine.PATCH(path, func(c *gin.Context) {
50+
err := handler(NewRequest(c), NewResponse(c))
51+
if err != nil {
52+
c.AbortWithError(http.StatusInternalServerError, err)
53+
}
54+
})
55+
}
56+
57+
func (g *ginFramework) DELETE(path string, handler framework.FrameworkHandler) {
58+
g.engine.DELETE(path, func(c *gin.Context) {
59+
err := handler(NewRequest(c), NewResponse(c))
60+
if err != nil {
61+
c.AbortWithError(http.StatusInternalServerError, err)
62+
}
63+
})
64+
}
65+
66+
func (g *ginFramework) OPTIONS(path string, handler framework.FrameworkHandler) {
67+
g.engine.OPTIONS(path, func(c *gin.Context) {
68+
err := handler(NewRequest(c), NewResponse(c))
69+
if err != nil {
70+
c.AbortWithError(http.StatusInternalServerError, err)
71+
}
72+
})
73+
}
74+
75+
func (g *ginFramework) HEAD(path string, handler framework.FrameworkHandler) {
76+
g.engine.HEAD(path, func(c *gin.Context) {
77+
err := handler(NewRequest(c), NewResponse(c))
78+
if err != nil {
79+
c.AbortWithError(http.StatusInternalServerError, err)
80+
}
81+
})
82+
}
83+
84+
func (g *ginFramework) TRACE(path string, handler framework.FrameworkHandler) {
85+
g.engine.Handle("TRACE", path, func(c *gin.Context) {
86+
err := handler(NewRequest(c), NewResponse(c))
87+
if err != nil {
88+
c.AbortWithError(http.StatusInternalServerError, err)
89+
}
90+
})
91+
}
92+
93+
func (g *ginFramework) ListenAndServe(addr string) error {
94+
return g.engine.Run(addr)
95+
}
96+
97+
func (g *ginFramework) TestRequest(req *http.Request) (*http.Response, error) {
98+
resp := httptest.NewRecorder()
99+
g.engine.ServeHTTP(resp, req)
100+
return resp.Result(), nil
101+
}
102+
103+
func init() {
104+
framework.RegisterFramework("gin", func() framework.Framework {
105+
return New(gin.New())
106+
})
107+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ginframework
2+
3+
import (
4+
"mime/multipart"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/go-simpl/simplapi/pkg/framework"
8+
)
9+
10+
type ginRequest struct {
11+
c *gin.Context
12+
}
13+
14+
func NewRequest(c *gin.Context) framework.FrameworkRequest {
15+
return &ginRequest{
16+
c: c,
17+
}
18+
}
19+
20+
func (r *ginRequest) ParseJSONBody(target interface{}) error {
21+
return r.c.ShouldBindJSON(target)
22+
}
23+
24+
func (r *ginRequest) GetHeader(key string) string {
25+
return r.c.GetHeader(key)
26+
}
27+
28+
func (r *ginRequest) GetPathParam(key string) string {
29+
return r.c.Param(key)
30+
}
31+
32+
func (r *ginRequest) GetQueryParam(key string) string {
33+
return r.c.Query(key)
34+
}
35+
36+
func (r *ginRequest) GetFormValue(key string) string {
37+
return r.c.PostForm(key)
38+
}
39+
40+
func (r *ginRequest) GetFile(key string) (*multipart.FileHeader, error) {
41+
return r.c.FormFile(key)
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ginframework
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/go-simpl/simplapi/pkg/framework"
6+
)
7+
8+
type ginResponse struct {
9+
c *gin.Context
10+
}
11+
12+
func NewResponse(c *gin.Context) framework.FrameworkResponse {
13+
return &ginResponse{
14+
c: c,
15+
}
16+
}
17+
18+
func (r *ginResponse) SetStatusCode(statusCode int) {
19+
r.c.Status(statusCode)
20+
}
21+
22+
func (r *ginResponse) SetHeader(key string, value string) {
23+
r.c.Header(key, value)
24+
}
25+
26+
func (r *ginResponse) SendJSON(data interface{}) error {
27+
r.c.JSON(r.c.Writer.Status(), data)
28+
return nil
29+
}
30+
31+
func (r *ginResponse) SendString(data string) error {
32+
r.c.String(r.c.Writer.Status(), data)
33+
return nil
34+
}

0 commit comments

Comments
 (0)