From 6922c3230afd6905fd27aaac7f3072d0e9ce4a28 Mon Sep 17 00:00:00 2001 From: ringsaturn Date: Sun, 28 May 2023 22:14:34 +0800 Subject: [PATCH] replace gin with hertz (#77) --- go.mod | 14 ++++++++++- go.sum | 42 +++++++++++++++++++++++++++++++ handler/api.go | 46 ++++++++++++++++++---------------- handler/engine.go | 50 ++++++++++++++++++------------------ handler/engine_test.go | 28 ++++++++++----------- handler/web.go | 40 +++++++++++++++-------------- main.go | 4 +-- pkg/htu/htu.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 199 insertions(+), 82 deletions(-) create mode 100644 pkg/htu/htu.go diff --git a/go.mod b/go.mod index 3432dd4..bffc510 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ringsaturn/tzf-server go 1.20 require ( - github.com/gin-gonic/gin v1.9.0 + github.com/cloudwego/hertz v0.6.4 github.com/paulmach/orb v0.9.2 github.com/ringsaturn/gtu v0.1.0 github.com/ringsaturn/tzf v0.13.0 @@ -13,14 +13,22 @@ require ( ) require ( + github.com/bytedance/go-tagexpr/v2 v2.9.2 // indirect + github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7 // indirect github.com/bytedance/sonic v1.8.5 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/cloudwego/netpoll v0.3.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.9.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/goccy/go-json v0.10.1 // indirect + github.com/golang/protobuf v1.5.0 // indirect + github.com/henrylee2cn/ameda v1.4.10 // indirect + github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/leodido/go-urn v1.2.2 // indirect @@ -28,10 +36,14 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/nyaruka/phonenumbers v1.0.55 // indirect github.com/pelletier/go-toml/v2 v2.0.7 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/geoindex v1.7.0 // indirect github.com/tidwall/geojson v1.4.3 // indirect + github.com/tidwall/gjson v1.13.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect github.com/tidwall/rtree v1.10.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twpayne/go-polyline v1.1.1 // indirect diff --git a/go.sum b/go.sum index 4c5334c..a58db92 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,26 @@ +github.com/bytedance/go-tagexpr/v2 v2.9.2 h1:QySJaAIQgOEDQBLS3x9BxOWrnhqu5sQ+f6HaZIxD39I= +github.com/bytedance/go-tagexpr/v2 v2.9.2/go.mod h1:5qsx05dYOiUXOUgnQ7w3Oz8BYs2qtM/bJokdLb79wRM= +github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7 h1:PtwsQyQJGxf8iaPptPNaduEIu9BnrNms+pcRdHAxZaM= +github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q= +github.com/bytedance/mockey v1.2.1 h1:g84ngI88hz1DR4wZTL3yOuqlEcq67MretBfQUdXwrmw= +github.com/bytedance/mockey v1.2.1/go.mod h1:+Jm/fzWZAuhEDrPXVjDf/jLM2BlLXJkwk94zf2JZ3X4= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.8.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/bytedance/sonic v1.8.5 h1:kjX0/vo5acEQ/sinD/18SkA/lDDUk23F0RcaHvI7omc= github.com/bytedance/sonic v1.8.5/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/cloudwego/hertz v0.6.4 h1:H6FGXZrtjjG/MwgxYi4k8baKZEwDuz0qHgSihoymJ2o= +github.com/cloudwego/hertz v0.6.4/go.mod h1:KhztQcZtMQ46gOjZcmCy557AKD29cbumGEV0BzwevwA= +github.com/cloudwego/netpoll v0.3.2 h1:/998ICrNMVBo4mlul4j7qcIeY7QnEfuCCPPwck9S3X4= +github.com/cloudwego/netpoll v0.3.2/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw= +github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= +github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.0 h1:OjyFBKICoexlu99ctXNR2gg+c5pKrKMuyjgARg9qeY8= @@ -22,14 +35,25 @@ github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVL github.com/goccy/go-json v0.10.1 h1:lEs5Ob+oOG/Ze199njvzHbhn6p9T+h64F5hRj69iTTo= github.com/goccy/go-json v0.10.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/henrylee2cn/ameda v1.4.8/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4= +github.com/henrylee2cn/ameda v1.4.10 h1:JdvI2Ekq7tapdPsuhrc4CaFiqw6QXFvZIULWJgQyCAk= +github.com/henrylee2cn/ameda v1.4.10/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4= +github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8 h1:yE9ULgp02BhYIrO6sdV/FPe0xQM6fNHkVQW2IAymfM0= +github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8/go.mod h1:Nhe/DM3671a5udlv2AdV2ni/MZzgfv2qrPL5nIi3EGQ= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= @@ -54,6 +78,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= +github.com/nyaruka/phonenumbers v1.0.55 h1:bj0nTO88Y68KeUQ/n3Lo2KgK7lM1hF7L9NFuwcCl3yg= +github.com/nyaruka/phonenumbers v1.0.55/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U= github.com/paulmach/orb v0.9.2 h1:p/YWV2uJwamAynnDOJGNbPBVtDHj3vG51k9tR1rFwJE= github.com/paulmach/orb v0.9.2/go.mod h1:5mULz1xQfs3bmQm63QEJA6lNGujuRafwA5S/EnuLaLU= github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKfDlhJCDw2gY= @@ -71,11 +97,17 @@ github.com/ringsaturn/tzf-rel v0.0.2023-b h1:27Kt3ewlXJ/nkYFedYWmKbj7CUWzG0UxFYX github.com/ringsaturn/tzf-rel v0.0.2023-b/go.mod h1:TvyUIUpF3aCH98QYjTmMb1cqK7pFswdFLoIVZwGNV/M= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -91,9 +123,13 @@ github.com/tidwall/geoindex v1.7.0 h1:jtk41sfgwIt8MEDyC3xyKSj75iXXf6rjReJGDNPtR5 github.com/tidwall/geoindex v1.7.0/go.mod h1:rvVVNEFfkJVWGUdEfU8QaoOg/9zFX0h9ofWzA60mz1I= github.com/tidwall/geojson v1.4.3 h1:yae/k/DhJdc9psaTJQ3pNOdbol70eH+nCijy6O7TxBw= github.com/tidwall/geojson v1.4.3/go.mod h1:1cn3UWfSYCJOq53NZoQ9rirdw89+DM0vw+ZOAVvuReg= +github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M= +github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/lotsa v1.0.2/go.mod h1:X6NiU+4yHA3fE3Puvpnn1XMDrFZrE9JO2/w+UMuqgR8= github.com/tidwall/lotsa v1.0.3 h1:lFAp3PIsS58FPmz+LzhE1mcZ67tBBCRPv5j66g6y7sg= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= @@ -116,6 +152,7 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.mongodb.org/mongo-driver v1.11.4 h1:4ayjakA013OdpGyL2K3ZqylTac/rMjrJOMZ1EHizXas= go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g= +golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= @@ -129,6 +166,7 @@ golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 h1:pVgRXcIictcr+lBQIFeiwuwtD golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -146,6 +184,8 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= @@ -158,6 +198,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= @@ -172,6 +213,7 @@ google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/handler/api.go b/handler/api.go index dc2ef43..5361730 100644 --- a/handler/api.go +++ b/handler/api.go @@ -1,61 +1,63 @@ package handler import ( + "context" "net/http" - "github.com/gin-gonic/gin" + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/common/utils" ) type LocationRequest struct { - Lng float64 `form:"lng"` - Lat float64 `form:"lat"` + Lng float64 `query:"lng"` + Lat float64 `query:"lat"` } -func GetTimezoneName(c *gin.Context) { +func GetTimezoneName(ctx context.Context, c *app.RequestContext) { req := &LocationRequest{} - err := c.ShouldBindQuery(req) + err := c.Bind(req) if err != nil { - c.JSON(http.StatusUnprocessableEntity, gin.H{"err": err.Error(), "uri": c.Request.RequestURI}) + c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": c.Request.RequestURI}) return } timezone := finder.GetTimezoneName(req.Lng, req.Lat) if timezone == "" { - c.JSON(http.StatusInternalServerError, gin.H{"err": "no timezone found"}) + c.JSON(http.StatusInternalServerError, utils.H{"err": "no timezone found"}) return } - c.JSON(http.StatusOK, gin.H{"timezone": timezone}) + c.JSON(http.StatusOK, utils.H{"timezone": timezone}) } -func GetTimezoneNames(c *gin.Context) { +func GetTimezoneNames(ctx context.Context, c *app.RequestContext) { req := &LocationRequest{} - err := c.ShouldBindQuery(req) + err := c.Bind(req) if err != nil { - c.JSON(http.StatusUnprocessableEntity, gin.H{"err": err.Error(), "uri": c.Request.RequestURI}) + c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": c.Request.RequestURI}) return } timezones, err := finder.GetTimezoneNames(req.Lng, req.Lat) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"err": err.Error()}) + c.JSON(http.StatusInternalServerError, utils.H{"err": err.Error()}) return } - c.JSON(http.StatusOK, gin.H{"timezones": timezones}) + c.JSON(http.StatusOK, utils.H{"timezones": timezones}) } -func GetAllSupportTimezoneNames(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"timezones": finder.TimezoneNames()}) +func GetAllSupportTimezoneNames(ctx context.Context, c *app.RequestContext) { + c.JSON(http.StatusOK, utils.H{"timezones": finder.TimezoneNames()}) } type GetTimezoneInfoRequest struct { - Name string `form:"name"` - Lng float64 `form:"lng"` - Lat float64 `form:"lat"` + Name string `query:"name"` + Lng float64 `query:"lng"` + Lat float64 `query:"lat"` } -func GetTimezoneShape(c *gin.Context) { +func GetTimezoneShape(ctx context.Context, c *app.RequestContext) { req := &GetTimezoneInfoRequest{} - err := c.ShouldBindQuery(req) + err := c.Bind(req) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()}) + c.JSON(http.StatusBadRequest, utils.H{"err": err.Error()}) return } if req.Name == "" { @@ -63,7 +65,7 @@ func GetTimezoneShape(c *gin.Context) { } shape, err := tzData.GetShape(req.Name) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"err": err.Error()}) + c.JSON(http.StatusInternalServerError, utils.H{"err": err.Error()}) return } c.JSON(http.StatusOK, shape) diff --git a/handler/engine.go b/handler/engine.go index dc06a64..3d560f5 100644 --- a/handler/engine.go +++ b/handler/engine.go @@ -1,13 +1,16 @@ package handler import ( + "context" "embed" "errors" "html/template" "net/http" "os" - "github.com/gin-gonic/gin" + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/app/server" + "github.com/cloudwego/hertz/pkg/common/utils" "github.com/paulmach/orb/maptile" "github.com/ringsaturn/tzf" tzfrel "github.com/ringsaturn/tzf-rel" @@ -132,7 +135,7 @@ func setupPolygonFinder(dataPath string) (tzf.F, VisualizableTimezoneData, error return finder, &polygonData{data: tzpb}, err } -func Setup(option *SetupFinderOptions) *gin.Engine { +func Setup(option *SetupFinderOptions) *server.Hertz { if option == nil { option = &SetupFinderOptions{ FinderType: PolygonFinder, @@ -149,32 +152,31 @@ func Setup(option *SetupFinderOptions) *gin.Engine { return setupEngine() } -func setupEngine() *gin.Engine { - engine := gin.Default() +func setupEngine() *server.Hertz { + h := server.Default(server.WithHostPorts(":8080")) - engine.SetHTMLTemplate(template.Must(template.New("").ParseFS(f, "template/*.html"))) + h.SetHTMLTemplate(template.Must(template.New("").ParseFS(f, "template/*.html"))) - engine.GET("/", Index) - engine.GET("/ping", Ping) + h.GET("/", Index) + h.GET("/ping", Ping) - apiV1Group := engine.Group("/api/v1") - { - apiV1Group.GET("/tz", GetTimezoneName) - apiV1Group.GET("/tzs", GetTimezoneNames) - apiV1Group.GET("/tzs/all", GetAllSupportTimezoneNames) - apiV1Group.GET("/tz/geojson", GetTimezoneShape) - } + apiV1Group := h.Group("/api/v1") + apiV1Group.GET("/tz", GetTimezoneName) + apiV1Group.GET("/tzs", GetTimezoneNames) + apiV1Group.GET("/tzs/all", GetAllSupportTimezoneNames) + apiV1Group.GET("/tz/geojson", GetTimezoneShape) - webPageGroup := engine.Group("/web") - { - webPageGroup.GET("/tz", GetTimezoneInfoPage) - webPageGroup.GET("/tzs", GetTimezonesInfoPage) - webPageGroup.GET("/tzs/all", GetAllSupportTimezoneNamesPage) - webPageGroup.GET("/tz/geojson/viewer", GetGeoJSONViewerForTimezone) - } - return engine + webPageGroup := h.Group("/web") + webPageGroup.GET("/tz", GetTimezoneInfoPage) + webPageGroup.GET("/tzs", GetTimezonesInfoPage) + webPageGroup.GET("/tzs/all", GetAllSupportTimezoneNamesPage) + webPageGroup.GET("/tz/geojson/viewer", GetGeoJSONViewerForTimezone) + + return h } -func Index(c *gin.Context) { c.Redirect(http.StatusTemporaryRedirect, "/web/tzs/all") } +func Index(ctx context.Context, c *app.RequestContext) { + c.Redirect(http.StatusTemporaryRedirect, []byte("/web/tzs/all")) +} -func Ping(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) } +func Ping(ctx context.Context, c *app.RequestContext) { c.JSON(http.StatusOK, utils.H{}) } diff --git a/handler/engine_test.go b/handler/engine_test.go index 64fb6c6..f0dfc25 100644 --- a/handler/engine_test.go +++ b/handler/engine_test.go @@ -4,12 +4,13 @@ import ( "encoding/json" "io" "net/http" - "net/http/httptest" "testing" - "github.com/gin-gonic/gin" + "github.com/cloudwego/hertz/pkg/app/server" + "github.com/cloudwego/hertz/pkg/common/ut" "github.com/ringsaturn/gtu" "github.com/ringsaturn/tzf-server/handler" + "github.com/ringsaturn/tzf-server/pkg/htu" "github.com/stretchr/testify/assert" ) @@ -21,12 +22,11 @@ var ( func TestEngine(t *testing.T) { type args struct { t *testing.T - engine *gin.Engine + engine *server.Hertz method string url string body io.Reader - validate gtu.ValidationFunc - options []gtu.RequestOption + validate htu.ValidationFunc } tests := []struct { name string @@ -40,8 +40,8 @@ func TestEngine(t *testing.T) { method: gtu.GET, url: "/", body: nil, - validate: func(t *testing.T, httpResp *httptest.ResponseRecorder) { - assert.Equal(t, httpResp.Result().StatusCode, http.StatusTemporaryRedirect) + validate: func(t *testing.T, httpResp *ut.ResponseRecorder) { + assert.Equal(t, httpResp.Result().StatusCode(), http.StatusTemporaryRedirect) }, }, }, @@ -53,8 +53,8 @@ func TestEngine(t *testing.T) { method: gtu.GET, url: "/ping", body: nil, - validate: func(t *testing.T, httpResp *httptest.ResponseRecorder) { - assert.Equal(t, httpResp.Result().StatusCode, http.StatusOK) + validate: func(t *testing.T, httpResp *ut.ResponseRecorder) { + assert.Equal(t, httpResp.Result().StatusCode(), http.StatusOK) }, }, }, @@ -66,8 +66,8 @@ func TestEngine(t *testing.T) { method: gtu.GET, url: "/api/v1/tz?lng=116.3883&lat=39.9289", body: nil, - validate: func(t *testing.T, httpResp *httptest.ResponseRecorder) { - assert.Equal(t, httpResp.Result().StatusCode, http.StatusOK) + validate: func(t *testing.T, httpResp *ut.ResponseRecorder) { + assert.Equal(t, httpResp.Result().StatusCode(), http.StatusOK) resp := map[string]string{} err := json.Unmarshal(httpResp.Body.Bytes(), &resp) if err != nil { @@ -85,8 +85,8 @@ func TestEngine(t *testing.T) { method: gtu.GET, url: "/api/v1/tz?lng=116.3883&lat=39.9289", body: nil, - validate: func(t *testing.T, httpResp *httptest.ResponseRecorder) { - assert.Equal(t, httpResp.Result().StatusCode, http.StatusOK) + validate: func(t *testing.T, httpResp *ut.ResponseRecorder) { + assert.Equal(t, httpResp.Result().StatusCode(), http.StatusOK) resp := map[string]string{} err := json.Unmarshal(httpResp.Body.Bytes(), &resp) if err != nil { @@ -99,7 +99,7 @@ func TestEngine(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gtu.Simple(tt.args.t, tt.args.engine, tt.args.method, tt.args.url, tt.args.body, tt.args.validate, tt.args.options...) + htu.Simple(tt.args.t, tt.args.engine, tt.args.method, tt.args.url, tt.args.body, tt.args.validate) }) } } diff --git a/handler/web.go b/handler/web.go index b3e2cde..3ad3a0c 100644 --- a/handler/web.go +++ b/handler/web.go @@ -1,10 +1,12 @@ package handler import ( + "context" "fmt" "net/http" - "github.com/gin-gonic/gin" + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/common/utils" ) type GetTimezoneInfoPageResponseItem struct { @@ -17,16 +19,16 @@ type GetTimezoneInfoPageResponse struct { Items []*GetTimezoneInfoPageResponseItem } -func GetTimezoneInfoPage(c *gin.Context) { +func GetTimezoneInfoPage(ctx context.Context, c *app.RequestContext) { req := &GetTimezoneInfoRequest{} - if err := c.ShouldBindQuery(req); err != nil { - c.JSON(http.StatusUnprocessableEntity, gin.H{"err": err.Error(), "uri": c.Request.RequestURI}) + if err := c.Bind(req); err != nil { + c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": c.Request.RequestURI}) return } timezone := finder.GetTimezoneName(req.Lng, req.Lat) if timezone == "" { - c.JSON(http.StatusInternalServerError, gin.H{"err": "no timezone found"}) + c.JSON(http.StatusInternalServerError, utils.H{"err": "no timezone found"}) return } @@ -37,23 +39,23 @@ func GetTimezoneInfoPage(c *gin.Context) { resp.Items = append(resp.Items, &GetTimezoneInfoPageResponseItem{ Name: timezone, - URL: fmt.Sprintf("http://%v/web/tz/geojson/viewer?name=%v", string(c.Request.Host), timezone), + URL: fmt.Sprintf("http://%v/web/tz/geojson/viewer?name=%v", string(c.Request.Host()), timezone), }) c.HTML(200, "info_multi.html", resp) } -func GetTimezonesInfoPage(c *gin.Context) { +func GetTimezonesInfoPage(ctx context.Context, c *app.RequestContext) { req := &LocationRequest{} - err := c.ShouldBindQuery(req) + err := c.Bind(req) if err != nil { - c.JSON(http.StatusUnprocessableEntity, gin.H{"err": err.Error(), "uri": c.Request.RequestURI}) + c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": string(c.Request.RequestURI())}) return } names, err := finder.GetTimezoneNames(req.Lng, req.Lat) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"err": "no timezone found"}) + c.JSON(http.StatusInternalServerError, utils.H{"err": "no timezone found"}) return } @@ -65,13 +67,13 @@ func GetTimezonesInfoPage(c *gin.Context) { for _, name := range names { resp.Items = append(resp.Items, &GetTimezoneInfoPageResponseItem{ Name: name, - URL: fmt.Sprintf("http://%v/web/tz/geojson/viewer?name=%v", string(c.Request.Host), name), + URL: fmt.Sprintf("http://%v/web/tz/geojson/viewer?name=%v", string(c.Request.Host()), name), }) } c.HTML(200, "info_multi.html", resp) } -func GetAllSupportTimezoneNamesPage(c *gin.Context) { +func GetAllSupportTimezoneNamesPage(ctx context.Context, c *app.RequestContext) { resp := &GetTimezoneInfoPageResponse{ Title: "All timezones", Items: make([]*GetTimezoneInfoPageResponseItem, 0), @@ -79,11 +81,11 @@ func GetAllSupportTimezoneNamesPage(c *gin.Context) { resp.Items = append(resp.Items, &GetTimezoneInfoPageResponseItem{ Name: "All", - URL: fmt.Sprintf("http://%v/web/tz/geojson/viewer?name=%v", string(c.Request.Host), "all"), + URL: fmt.Sprintf("http://%v/web/tz/geojson/viewer?name=%v", string(c.Request.Host()), "all"), }) for _, name := range finder.TimezoneNames() { - viewerURL := fmt.Sprintf("http://%v/web/tz/geojson/viewer?name=%v", string(c.Request.Host), name) + viewerURL := fmt.Sprintf("http://%v/web/tz/geojson/viewer?name=%v", string(c.Request.Host()), name) resp.Items = append(resp.Items, &GetTimezoneInfoPageResponseItem{ Name: name, URL: viewerURL, @@ -93,20 +95,20 @@ func GetAllSupportTimezoneNamesPage(c *gin.Context) { } // Render GeoJSON on web -func GetGeoJSONViewerForTimezone(c *gin.Context) { +func GetGeoJSONViewerForTimezone(ctx context.Context, c *app.RequestContext) { req := &GetTimezoneInfoRequest{} - if err := c.ShouldBindQuery(req); err != nil { - c.JSON(http.StatusUnprocessableEntity, gin.H{"err": err.Error(), "uri": c.Request.RequestURI}) + if err := c.Bind(req); err != nil { + c.JSON(http.StatusUnprocessableEntity, utils.H{"err": err.Error(), "uri": string(c.Request.RequestURI())}) return } timezone := finder.GetTimezoneName(req.Lng, req.Lat) if timezone == "" { - c.JSON(http.StatusInternalServerError, gin.H{"err": "no timezone found"}) + c.JSON(http.StatusInternalServerError, utils.H{"err": "no timezone found"}) return } c.HTML(http.StatusOK, "viewer.html", map[string]any{ - "URL": fmt.Sprintf("http://%v/api/v1/tz/geojson?lng=%v&lat=%v&name=%v", c.Request.Host, req.Lng, req.Lat, req.Name), + "URL": fmt.Sprintf("http://%v/api/v1/tz/geojson?lng=%v&lat=%v&name=%v", string(c.Request.Host()), req.Lng, req.Lat, req.Name), }) } diff --git a/main.go b/main.go index 6c58473..b3abc1d 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,9 @@ func main() { dataPath := flag.String("path", "", "custom data") flag.Parse() - engine := handler.Setup(&handler.SetupFinderOptions{ + h := handler.Setup(&handler.SetupFinderOptions{ FinderType: handler.FinderType((*finderType)), CustomDataPath: *dataPath, }) - panic(engine.Run()) + panic(h.Run()) } diff --git a/pkg/htu/htu.go b/pkg/htu/htu.go new file mode 100644 index 0000000..d955520 --- /dev/null +++ b/pkg/htu/htu.go @@ -0,0 +1,57 @@ +package htu + +import ( + "encoding/json" + "io" + "testing" + + "github.com/cloudwego/hertz/pkg/app/server" + "github.com/cloudwego/hertz/pkg/common/ut" +) + +const ( + GET = "GET" + HEAD = "HEAD" + POST = "POST" + PUT = "PUT" + PATCH = "PATCH" + DELETE = "DELETE" + TRACE = "TRACE" + OPTIONS = "OPTIONS" +) + +type ValidationFunc func(t *testing.T, resp *ut.ResponseRecorder) + +func Simple( + t *testing.T, + s *server.Hertz, + method string, url string, body io.Reader, + validate ValidationFunc, +) { + w := ut.PerformRequest( + s.Engine, method, url, + nil, + ) + validate(t, w) +} + +type ValidationFuncForJSONAPI func(t *testing.T, expectedResponse interface{}) + +func JSONAPI( + t *testing.T, + s *server.Hertz, + method string, url string, + expectedResponse interface{}, + validate ValidationFuncForJSONAPI, +) { + w := ut.PerformRequest( + s.Engine, method, url, + nil, + ) + + if err := json.Unmarshal(w.Body.Bytes(), expectedResponse); err != nil { + t.Error(err.Error()) + } + + validate(t, expectedResponse) +}