diff options
| author | Doog <157747121+doogongithub@users.noreply.github.com> | 2024-02-24 20:08:35 -0500 |
|---|---|---|
| committer | Doog <157747121+doogongithub@users.noreply.github.com> | 2024-02-24 20:08:35 -0500 |
| commit | e37c73e33a4aaf7fb8d25b5af03627f20bcda19f (patch) | |
| tree | 277a534e826325e25f881e61e322b4e2e7ec94f9 /api | |
| parent | 3eafb413a48cde60dea8a7355ee621c6acca952f (diff) | |
add gitignore
Diffstat (limited to 'api')
| -rw-r--r-- | api/go.mod | 6 | ||||
| -rw-r--r-- | api/go.sum | 9 | ||||
| -rw-r--r-- | api/lib/models.go | 23 | ||||
| -rw-r--r-- | api/main.go | 127 |
4 files changed, 145 insertions, 20 deletions
| @@ -3,12 +3,16 @@ module water/api | |||
| 3 | go 1.18 | 3 | go 1.18 |
| 4 | 4 | ||
| 5 | require ( | 5 | require ( |
| 6 | github.com/gin-gonic/gin v1.9.1 | ||
| 7 | github.com/mattn/go-sqlite3 v1.14.22 | ||
| 8 | ) | ||
| 9 | |||
| 10 | require ( | ||
| 6 | github.com/bytedance/sonic v1.11.0 // indirect | 11 | github.com/bytedance/sonic v1.11.0 // indirect |
| 7 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect | 12 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect |
| 8 | github.com/chenzhuoyu/iasm v0.9.1 // indirect | 13 | github.com/chenzhuoyu/iasm v0.9.1 // indirect |
| 9 | github.com/gabriel-vasile/mimetype v1.4.3 // indirect | 14 | github.com/gabriel-vasile/mimetype v1.4.3 // indirect |
| 10 | github.com/gin-contrib/sse v0.1.0 // indirect | 15 | github.com/gin-contrib/sse v0.1.0 // indirect |
| 11 | github.com/gin-gonic/gin v1.9.1 // indirect | ||
| 12 | github.com/go-playground/locales v0.14.1 // indirect | 16 | github.com/go-playground/locales v0.14.1 // indirect |
| 13 | github.com/go-playground/universal-translator v0.18.1 // indirect | 17 | github.com/go-playground/universal-translator v0.18.1 // indirect |
| 14 | github.com/go-playground/validator/v10 v10.18.0 // indirect | 18 | github.com/go-playground/validator/v10 v10.18.0 // indirect |
| @@ -10,6 +10,7 @@ github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLI | |||
| 10 | github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= | 10 | github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= |
| 11 | github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= | 11 | github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= |
| 12 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | 12 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
| 13 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| 13 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | 14 | 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 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= |
| 15 | github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= | 16 | github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= |
| @@ -17,6 +18,7 @@ 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-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= |
| 18 | github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= | 19 | github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= |
| 19 | github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= | 20 | github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= |
| 21 | github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= | ||
| 20 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= | 22 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= |
| 21 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= | 23 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= |
| 22 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= | 24 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= |
| @@ -25,6 +27,7 @@ github.com/go-playground/validator/v10 v10.18.0 h1:BvolUXjp4zuvkZ5YN5t7ebzbhlUtP | |||
| 25 | github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= | 27 | github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= |
| 26 | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= | 28 | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= |
| 27 | github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= | 29 | github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= |
| 30 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= | ||
| 28 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= | 31 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= |
| 29 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= | 32 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= |
| 30 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= | 33 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= |
| @@ -36,6 +39,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= | |||
| 36 | github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= | 39 | github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= |
| 37 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | 40 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= |
| 38 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | 41 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= |
| 42 | github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= | ||
| 43 | github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
| 39 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= | 44 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= |
| 40 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= | 45 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= |
| 41 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= | 46 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= |
| @@ -43,6 +48,7 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G | |||
| 43 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= | 48 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= |
| 44 | github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= | 49 | github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= |
| 45 | github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= | 50 | github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= |
| 51 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| 46 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | 52 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
| 47 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | 53 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
| 48 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | 54 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= |
| @@ -52,6 +58,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ | |||
| 52 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | 58 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
| 53 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | 59 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= |
| 54 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | 60 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= |
| 61 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
| 55 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | 62 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= |
| 56 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= | 63 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= |
| 57 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= | 64 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= |
| @@ -70,8 +77,10 @@ golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= | |||
| 70 | golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | 77 | golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= |
| 71 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | 78 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= |
| 72 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | 79 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= |
| 80 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | ||
| 73 | google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= | 81 | google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= |
| 74 | google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= | 82 | google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= |
| 83 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| 75 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | 84 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
| 76 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | 85 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
| 77 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | 86 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
diff --git a/api/lib/models.go b/api/lib/models.go new file mode 100644 index 0000000..92e5703 --- /dev/null +++ b/api/lib/models.go | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | package models | ||
| 2 | |||
| 3 | import "time" | ||
| 4 | |||
| 5 | type Statistic struct { | ||
| 6 | ID int64 `json:"id"` | ||
| 7 | Date time.Time `json:"date"` | ||
| 8 | UserID int64 `json:"user_id"` | ||
| 9 | Quantity int `json:"quantity"` | ||
| 10 | } | ||
| 11 | |||
| 12 | type User struct { | ||
| 13 | ID int64 | ||
| 14 | Name string | ||
| 15 | } | ||
| 16 | |||
| 17 | type Token struct { | ||
| 18 | ID int64 | ||
| 19 | UserID int64 | ||
| 20 | Token string | ||
| 21 | CreatedAt time.Time | ||
| 22 | ExpiredAt time.Time | ||
| 23 | } | ||
diff --git a/api/main.go b/api/main.go index ebae5d1..292a5f9 100644 --- a/api/main.go +++ b/api/main.go | |||
| @@ -4,8 +4,13 @@ import ( | |||
| 4 | "net/http" | 4 | "net/http" |
| 5 | "crypto/rand" | 5 | "crypto/rand" |
| 6 | "encoding/base64" | 6 | "encoding/base64" |
| 7 | "database/sql" | ||
| 8 | "strings" | ||
| 9 | "errors" | ||
| 7 | 10 | ||
| 8 | "github.com/gin-gonic/gin" | 11 | "github.com/gin-gonic/gin" |
| 12 | _ "github.com/mattn/go-sqlite3" | ||
| 13 | "water/api/lib" | ||
| 9 | ) | 14 | ) |
| 10 | 15 | ||
| 11 | func CORSMiddleware() gin.HandlerFunc { | 16 | func CORSMiddleware() gin.HandlerFunc { |
| @@ -30,6 +35,44 @@ func generateToken() string { | |||
| 30 | return base64.StdEncoding.EncodeToString(token) | 35 | return base64.StdEncoding.EncodeToString(token) |
| 31 | } | 36 | } |
| 32 | 37 | ||
| 38 | func establishDBConnection() *sql.DB { | ||
| 39 | db, err := sql.Open("sqlite3", "../db/water.sqlite3") | ||
| 40 | if err != nil { | ||
| 41 | panic(err) | ||
| 42 | } | ||
| 43 | return db | ||
| 44 | } | ||
| 45 | |||
| 46 | func checkForTokenInContext(c *gin.Context) (string, error) { | ||
| 47 | authorizationHeader := c.GetHeader("Authorization") | ||
| 48 | if authorizationHeader == "" { | ||
| 49 | return "", errors.New("Authorization header is missing") | ||
| 50 | } | ||
| 51 | |||
| 52 | parts := strings.Split(authorizationHeader, " ") | ||
| 53 | |||
| 54 | if len(parts) != 2 || parts[0] != "Bearer" { | ||
| 55 | return "", errors.New("Invalid Authorization header format") | ||
| 56 | } | ||
| 57 | |||
| 58 | return parts[1], nil | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | func TokenRequired() gin.HandlerFunc { | ||
| 63 | return func(c *gin.Context) { | ||
| 64 | _, err := checkForTokenInContext(c) | ||
| 65 | |||
| 66 | if err != nil { | ||
| 67 | c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) | ||
| 68 | c.Abort() | ||
| 69 | return | ||
| 70 | } | ||
| 71 | |||
| 72 | c.Next() | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 33 | type User struct { | 76 | type User struct { |
| 34 | Username string | 77 | Username string |
| 35 | Password string | 78 | Password string |
| @@ -44,6 +87,8 @@ func setupRouter() *gin.Engine { | |||
| 44 | // gin.DisableConsoleColor() | 87 | // gin.DisableConsoleColor() |
| 45 | r := gin.Default() | 88 | r := gin.Default() |
| 46 | r.Use(CORSMiddleware()) | 89 | r.Use(CORSMiddleware()) |
| 90 | r.Use(gin.Logger()) | ||
| 91 | r.Use(gin.Recovery()) | ||
| 47 | 92 | ||
| 48 | api := r.Group("api/v1") | 93 | api := r.Group("api/v1") |
| 49 | 94 | ||
| @@ -68,26 +113,70 @@ func setupRouter() *gin.Engine { | |||
| 68 | }) | 113 | }) |
| 69 | 114 | ||
| 70 | stats := api.Group("stats") | 115 | stats := api.Group("stats") |
| 116 | stats.Use(TokenRequired()) | ||
| 117 | { | ||
| 118 | stats.GET("/", func(c *gin.Context) { | ||
| 119 | db := establishDBConnection() | ||
| 120 | defer db.Close() | ||
| 121 | |||
| 122 | rows, err := db.Query("SELECT * FROM statistics"); | ||
| 123 | if err != nil { | ||
| 124 | c.JSON(500, gin.H{"error": err.Error()}) | ||
| 125 | return | ||
| 126 | } | ||
| 127 | defer rows.Close() | ||
| 128 | |||
| 129 | var data []models.Statistic | ||
| 130 | for rows.Next() { | ||
| 131 | var stat models.Statistic | ||
| 132 | if err := rows.Scan(&stat.ID, &stat.Date, &stat.UserID, &stat.Quantity); err != nil { | ||
| 133 | c.JSON(500, gin.H{"error": err.Error()}) | ||
| 134 | return | ||
| 135 | } | ||
| 136 | data = append(data, stat) | ||
| 137 | } | ||
| 138 | |||
| 139 | c.JSON(http.StatusOK, data) | ||
| 140 | }) | ||
| 141 | |||
| 142 | stats.POST("/", func(c *gin.Context) { | ||
| 143 | var stat models.Statistic | ||
| 144 | |||
| 145 | if err := c.BindJSON(&stat); err != nil { | ||
| 146 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| 147 | return | ||
| 148 | } | ||
| 149 | |||
| 150 | db := establishDBConnection() | ||
| 151 | defer db.Close() | ||
| 152 | |||
| 153 | result, err := db.Exec("INSERT INTO statistics (date, user_id, quantity) values (?, ?, ?)", stat.Date, stat.UserID, stat.Quantity) | ||
| 154 | |||
| 155 | if err != nil { | ||
| 156 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| 157 | } | ||
| 158 | |||
| 159 | id, err := result.LastInsertId() | ||
| 160 | if err != nil { | ||
| 161 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| 162 | } | ||
| 163 | |||
| 164 | c.JSON(http.StatusCreated, gin.H{"status": "created", "id": id}) | ||
| 165 | }) | ||
| 166 | |||
| 167 | stats.GET("/:uuid", func(c *gin.Context) { | ||
| 168 | c.JSON(http.StatusOK, gin.H{"status": "ok", "uuid": c.Param("uuid")}) | ||
| 169 | }) | ||
| 170 | |||
| 171 | stats.PATCH("/:uuid", func(c *gin.Context) { | ||
| 172 | c.JSON(http.StatusNoContent, gin.H{"status": "No Content"}) | ||
| 173 | }) | ||
| 174 | |||
| 175 | stats.DELETE("/:uuid", func(c *gin.Context) { | ||
| 176 | c.JSON(http.StatusNoContent, gin.H{"status": "No Content"}) | ||
| 177 | }) | ||
| 178 | } | ||
| 71 | 179 | ||
| 72 | stats.GET("/", func(c *gin.Context) { | ||
| 73 | c.JSON(http.StatusOK, gin.H{"status": "ok"}) | ||
| 74 | }) | ||
| 75 | |||
| 76 | stats.POST("/", func(c *gin.Context) { | ||
| 77 | c.JSON(http.StatusCreated, gin.H{"status": "created"}) | ||
| 78 | }) | ||
| 79 | |||
| 80 | stats.GET("/:uuid", func(c *gin.Context) { | ||
| 81 | c.JSON(http.StatusOK, gin.H{"status": "ok", "uuid": c.Param("uuid")}) | ||
| 82 | }) | ||
| 83 | |||
| 84 | stats.PATCH("/:uuid", func(c *gin.Context) { | ||
| 85 | c.JSON(http.StatusNoContent, gin.H{"status": "No Content"}) | ||
| 86 | }) | ||
| 87 | |||
| 88 | stats.DELETE("/:uuid", func(c *gin.Context) { | ||
| 89 | c.JSON(http.StatusNoContent, gin.H{"status": "No Content"}) | ||
| 90 | }) | ||
| 91 | 180 | ||
| 92 | return r | 181 | return r |
| 93 | } | 182 | } |
