Skip to content

Commit

Permalink
add redis protocol server (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn authored Jun 12, 2023
1 parent fc3c658 commit 48d86b8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Simple HTTP API convert longitude&latitude to timezone name
# Simple server convert longitude&latitude to timezone name

> **Note**: It's designed as a debugger tool for package
> [tzf](https://github.com/ringsaturn/tzf), not production ready.
Expand Down Expand Up @@ -69,3 +69,33 @@ Output:
]
}
```

## Redis Protocol Commands

### `redis-cli`

```
$ redis-cli -p 6380
127.0.0.1:6380> GET_TZ 116.3883 39.9289
Asia/Shanghai
127.0.0.1:6380> GET_TZS 87.4160 44.0400
1) "Asia/Shanghai"
2) "Asia/Urumqi"
```

## `redis-py`

```python
from redis import Redis

rc = Redis.from_url("redis://localhost:6380")

print(rc.ping())
# Output: True

print(rc.execute_command("get_tz", 116.3883, 39.9289).decode())
# Output: Asia/Shanghai

print(rc.execute_command("get_tzs", 87.4160, 44.0400))
# Output: [b'Asia/Shanghai', b'Asia/Urumqi']
```
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ go 1.20
require (
github.com/cloudwego/hertz v0.6.4
github.com/paulmach/orb v0.9.2
github.com/pkg/errors v0.9.1
github.com/ringsaturn/tzf v0.13.0
github.com/ringsaturn/tzf-rel v0.0.2023-b
github.com/stretchr/testify v1.8.4
github.com/tidwall/redcon v1.6.2
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/protobuf v1.30.0
)

Expand All @@ -28,6 +31,7 @@ require (
github.com/nyaruka/phonenumbers v1.0.55 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/tidwall/btree v1.1.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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ 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=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -92,6 +93,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/btree v1.1.0 h1:5P+9WU8ui5uhmcg3SoPyTwoI0mVyZ1nps7YQzTZFkYM=
github.com/tidwall/btree v1.1.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4=
github.com/tidwall/cities v0.1.0 h1:CVNkmMf7NEC9Bvokf5GoSsArHCKRMTgLuubRTHnH0mE=
github.com/tidwall/cities v0.1.0/go.mod h1:lV/HDp2gCcRcHJWqgt6Di54GiDrTZwh1aG2ZUPNbqa4=
github.com/tidwall/geoindex v1.4.4/go.mod h1:rvVVNEFfkJVWGUdEfU8QaoOg/9zFX0h9ofWzA60mz1I=
Expand All @@ -110,6 +113,8 @@ github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/redcon v1.6.2 h1:5qfvrrybgtO85jnhSravmkZyC0D+7WstbfCs3MmPhow=
github.com/tidwall/redcon v1.6.2/go.mod h1:p5Wbsgeyi2VSTBWOcA5vRXrOb9arFTcU2+ZzFjqV75Y=
github.com/tidwall/rtree v1.3.1/go.mod h1:S+JSsqPTI8LfWA4xHBo5eXzie8WJLVFeppAutSegl6M=
github.com/tidwall/rtree v1.10.0 h1:+EcI8fboEaW1L3/9oW/6AMoQ8HiEIHyR7bQOGnmz4Mg=
github.com/tidwall/rtree v1.10.0/go.mod h1:iDJQ9NBRtbfKkzZu02za+mIlaP+bjYPnunbSNidpbCQ=
Expand Down Expand Up @@ -147,6 +152,7 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
51 changes: 51 additions & 0 deletions handler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package handler
import (
"context"
"net/http"
"strconv"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/pkg/errors"
"github.com/tidwall/redcon"
)

type LocationRequest struct {
Expand Down Expand Up @@ -70,3 +73,51 @@ func GetTimezoneShape(ctx context.Context, c *app.RequestContext) {
}
c.JSON(http.StatusOK, shape)
}

func parseCoordinates(cmd redcon.Command) (float64, float64, error) {
if len(cmd.Args) != 3 {
return 0, 0, errors.New("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
}
lng, err := strconv.ParseFloat(string(cmd.Args[1]), 64)
if err != nil {
return 0, 0, err
}
lat, err := strconv.ParseFloat(string(cmd.Args[2]), 64)
if err != nil {
return 0, 0, err
}
return lng, lat, nil
}

func RedisGetTZCmd(conn redcon.Conn, cmd redcon.Command) {
lng, lat, err := parseCoordinates(cmd)
if err != nil {
conn.WriteError(err.Error())
return
}

timezone_name := finder.GetTimezoneName(lng, lat)
if timezone_name == "" {
conn.WriteError("no tz found")
return
}
conn.WriteString(timezone_name)
}

func RedisGetTZsCmd(conn redcon.Conn, cmd redcon.Command) {
lng, lat, err := parseCoordinates(cmd)
if err != nil {
conn.WriteError(err.Error())
return
}

timezone_names, err := finder.GetTimezoneNames(lng, lat)
if err != nil {
conn.WriteError("no tz found")
return
}
conn.WriteArray(len(timezone_names))
for _, name := range timezone_names {
conn.WriteBulkString(name)
}
}
34 changes: 33 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"context"
"flag"
"strings"

"github.com/ringsaturn/tzf-server/handler"
"github.com/tidwall/redcon"
"golang.org/x/sync/errgroup"
)

func main() {
Expand All @@ -15,5 +19,33 @@ func main() {
FinderType: handler.FinderType((*finderType)),
CustomDataPath: *dataPath,
})
panic(h.Run())

g, _ := errgroup.WithContext(context.Background())

g.Go(h.Run)

g.Go(func() error {
err := redcon.ListenAndServe(":6380",
func(conn redcon.Conn, cmd redcon.Command) {
switch strings.ToLower(string(cmd.Args[0])) {
default:
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
case "ping":
conn.WriteString("PONG")
case "quit":
conn.WriteString("OK")
conn.Close()
case "get_tz":
handler.RedisGetTZCmd(conn, cmd)
case "get_tzs":
handler.RedisGetTZsCmd(conn, cmd)
}
},
func(conn redcon.Conn) bool { return true },
func(conn redcon.Conn, err error) {},
)
return err
})

panic(g.Wait())
}

0 comments on commit 48d86b8

Please sign in to comment.