https://github.com/zshipko/worm
Reflection-based RESP3 server framework
https://github.com/zshipko/worm
Last synced: 8 months ago
JSON representation
Reflection-based RESP3 server framework
- Host: GitHub
- URL: https://github.com/zshipko/worm
- Owner: zshipko
- License: isc
- Created: 2019-09-12T01:13:06.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-12T06:40:17.000Z (over 6 years ago)
- Last Synced: 2025-09-07T01:41:38.085Z (9 months ago)
- Language: Go
- Homepage:
- Size: 61.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# worm
A reflection-based RESP3 server framework for Go
## Protocol
`worm` implements the majority of the RESP3 protocol, however the following components are not yet implemented:
- Streaming types
- Attribute type
- Non-string map keys
## Getting started
`worm` uses reflection to build a map of commands based on the methods of a struct value:
```go
type MyCommands struct {
}
func (c *MyCommands) Example(client *worm.Client, args ...*worm.Value) error {
return client.WriteValue(NewArray(args))
}
func (c *MyCommands) Example2(client *worm.Client, arg1 *worm.Value, arg2 *worm.Value) error {
if err := client.WriteArrayHeader(2); err != nil {
return err
}
if err := client.WriteValue(arg1); err != nil {
return err
}
return client.WriteValue(arg2)
}
func (c *MyCommands) SomethingElse(i int) int {
return i + 1
}
```
In the example above, `MyCommands` exports two `worm` commands named `Example` and `Example2`. `SomethingElse`
isn't converted to a command because it has incompatible arguments.
In order to write a valid command, it must:
1. Start with a `*worm.Client` argument
2. Contain any number of `*worm.Value` arguments, including variadic arguments
3. Return an `error` value
Once you have written all your commands, you can easily create a new server:
```go
ctx := MyCommands {}
server, err := worm.NewTCPServer("127.0.0.1:8081", nil, &ctx)
```
And run it:
```go
server.Run()
```
Then, using `redis-cli`, you can query it:
```shell
$ redis-cli -p 8081
127.0.0.1:8081> example testing 1234
1) testing
2) 1234
```