Note: Install the following before you can use the template generator.
- rust, rustup, cargo
- cargo-generate
- protoc
- server_template
- workspacer (optional cli tools to manage rust workspace)
Note: You can ignore this step if you already set it up
- Set up Workspace
mkdir <workspace_root>
cd <workspace_root>
- Update workspace
Cargo.toml
file content below.
Note: The workspacer command above would generate this Code on Cargo.toml
[workspace]
resolver = "2"
members = [
server,
# add here services here after you generated it
auth, # example service
]
or you can download and install workspacer cli and use it to manage your workspace
mkdir <workspace_root>
workspacer init
# by default it has the server in the member
# to add more services
worspacer add services/auth
# you can also remove service
workspacer rm service/auth
# to view all current members of workspace
workspacer ls
Example Workspace Tree Structure
workspace (root)
├── Cargo.toml
│
├── backend (server_template)
│
├── frontend (dioxus_template)
│
└── services (service_template)
└── <service_name>
├── proto
│ └── <service_name>.proto
├── src
│ ├── <service_name.rs> (generated with cargo build)
│ ├── <service_name_impl.rs>
│ └── lib.rs
├── cargo-generate.toml
└── Cargo.toml
- Create the Server
cd <workspace_root>
cargo generate --git codeitlikemiley/server_template --name server
~/.cargo/cargo-generate.toml
if this file dont exist please create it first touch ~/.cargo/cargo-generate.toml
- Create Folder for Templates
cd ~/.cargo
mkdir templates
cd templates
git clone https://github.com/codeitlikemiley/services_template
git clone https://github.com/codeitlikemiley/server_template
- Add to favorites services_template and server_template
[values]
gh_username = "YOUR_USERNAME"
ide = "none|vscode"
[favorites.services]
path = "/Users/YOUR_USERNAME/.cargo/templates/services_template"
[favorites.server]
path = "/Users/YOUR_USERNAME/.cargo/templates/server_template/"
- Generate Server and Services template with short cut
cargo generate services --name auth
cargo generate server --name server
- Change directory to the services directory.
cd <workspace_root>/services
- Generate a new GRPC Service.
cargo generate --git codeitlikemiley/services_template --name auth
- Run Server
cargo run -p server
- Use
grpcurl
Client
To invoke the specific rpc method use the following command
grpcurl -plaintext -import-path ./services/auth/proto -proto auth.proto -d '{"name": "Tonic"}' '[::1]:50051' auth.AuthService.Login
example output:
{
"message": "Hello Uriah!"
}
Note: that the example auth.AuthService.Login
is the package_name.ServiceName.MethodName
from the auth.proto
file
auth.proto
package auth;
service AuthService {
rpc Login (LoginRequest) returns (LoginResponse) {}
}
If you need to make changes to the template, please submit a pull request.
For more info how to use cargo-generate check this link
Learn about Liquid Templating here
To learn more about Rust Tonic click this link
Learn more about Embedded Scripting for Rust here , link for LSP on vscode
Example usage of Rhai Scripting on the template here