https://github.com/reddec/nano-rpc
Tiny lib for golang for expose methods over HTTP
https://github.com/reddec/nano-rpc
Last synced: over 1 year ago
JSON representation
Tiny lib for golang for expose methods over HTTP
- Host: GitHub
- URL: https://github.com/reddec/nano-rpc
- Owner: reddec
- License: mit
- Created: 2018-12-15T13:59:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-16T04:26:06.000Z (over 7 years ago)
- Last Synced: 2025-03-09T00:06:39.750Z (over 1 year ago)
- Language: Go
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nano HTTP RPC lib
[](http://godoc.org/github.com/reddec/nano-rpc)
`import "github.com/reddec/nano-rpc"`
No dependency, super-tiny library that lets you expose any suitable function to HTTP POST interface.
Function should have one input argument and returns tuple of value and error.
Ex:
**good**
* `func hello(name string) (string, error)`
* `func bye(data MyStruct) (bool, error)`
* `func sample(a *int) (*MyStruct, error)`
**bad**
* `func hello(name, lastname string) (string, error)` - two arguments
* `func sample() (string, error)` - no arguments
* `func close(data string) error` - only one return
* `func shutdown(delay int) (int, int)` - last argument is not an error
Methods are expose as POST methods using method name as last part of path.
Ex:
Method `greetings` will be exposed as `POST /greetings`
## Server usage
```go
package main
import (
"github.com/reddec/nano-rpc"
"net/http"
)
func main() {
server := &nano.Server{}
server.AddFunc("greetings", func(name string) (string, error) {
return "hello " + name, nil
})
http.ListenAndServe(":8080", server)
}
```
Instead of `AddFunc` you can use `Add` that will scan all methods in object and expose suitable ones.
## Client usage
```go
package main
import (
"fmt"
"github.com/reddec/nano-rpc"
)
func main() {
client := &nano.Client{
URL: "http://127.0.0.1:8080",
}
var ans string
err := client.Invoke("greetings", "Foo", &ans)
if err!=nil{
panic(err)
}
fmt.Println(ans)
}
```