A small example of using oto for RPC code generation in the form of a shopping list CLI.
go install github.com/peterhellberg/kop/cmd/...@latest
(This will install two binaries, kop
and kop-server
)
Run kop-server
in one terminal (this will by default start a web server on localhost:12432
)
Note
If you change the PORT
for kop-server
, then you will have to set the KOP_ENDPOINT
variable accordingly.
(Default is http://localhost:12432/rpc/
)
Then run kop Eggs Milk Flour
to create an initial list using the command line interface.
- EGGS
- FLOUR
- MILK
Important
If you speak Swedish, then you will want to alias köp='kop'
(and if not, then you might want to alias buy='kop'
)
You can then remove an item from the list by calling kop no Eggs
- FLOUR
- MILK
If you do not want to use the cli then you can use cURL directly
curl -d '{}' localhost:12432/rpc/List.Items
{
"items": [
"FLOUR",
"MILK"
]
}
And even add something else to the list 🍺
curl -d '{"items": ["Beer"]}' localhost:12432/rpc/List.Add
{
"items": [
"BEER",
"FLOUR",
"MILK"
]
}
And when you want to start a new list then just run kop nothing
The definitions for the RPC service are found in definitions/definitions.go
The generated code for the RPC service is in rpc/gen.go
The example implementation of the rpc.List
interface is in list/list.go
I added a small HTML frontend (For use from my phone, over Tailscale) using html/template and PicoCSS
It is available on http://localhost:12432/
when running kop-server
Copyright (c) 2023 Peter Hellberg
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.