Skip to content

Commit

Permalink
server template working
Browse files Browse the repository at this point in the history
  • Loading branch information
codeitlikemiley committed Dec 16, 2023
1 parent ef65589 commit 8662c10
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 82 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DB_URL=localhost:5432
PRODUCTION=true
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
[package]
name = "server"
name = "{{crate_name}}"
version = "0.1.0"
edition = "2021"

[dependencies]
tonic = "0.10.2"
tokio = { version = "1.35.0", features = [] }
{% for service in service_names.split(",") %}
{{service.trim()}}_service = { path = "../services/{{service.trim()}}" }
{% endfor %}
tokio = {{"{"}} version = "1.35.0", features = [] {{"}"}}

{{service_name}}_service = {{"{"}} path = "../services/{{service_name}}" {{"}"}}
55 changes: 0 additions & 55 deletions Dockerfile

This file was deleted.

81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# GRPC Server Generator

[![Build Template](https://github.com/codeitlikemiley/server_template/actions/workflows/build.yml/badge.svg)](https://github.com/codeitlikemiley/server_template/actions/workflows/build.yml)

## Requirements

Note: Install the following before you can use the template generator.

- [rust, rustup, cargo](https://www.rust-lang.org/tools/install)
- [cargo-workspace](https://crates.io/crates/cargo-workspaces)
- [cargo-generate](https://cargo-generate.github.io/cargo-generate/installation.html)
- [protoc](https://grpc.io/docs/protoc-installation/)


## Project Structure

```sh
workspace (root)
├── Cargo.toml
├── server <-- Generate Template this from **server_template**
│ └── .env.example
│ └── rust-toolchain.toml
│ └── Cargo.toml
│ └── README.md
├── frontend
└── services
```

## Usage

### Create a workspace

```sh
cd <workspace_root>
cargo workspaces init .
```
or you can simply create a `Cargo.toml` with this content

```toml
[workspace]
resolver = "2"
members = [
server, # <- add this
# add here services here after you generated it
]
```

### Generate a new GRPC server template.

```sh
cargo generate --git codeitlikemiley/server_template --name server
cd server
```

### Build , Testing and Documentation

```sh
cargo build
cargo test
cargo doc --open
```

### Run Server

```sh
cargo run -p server
```

### Adding services

To add more services please check this [Services Template Repository](https://github.com/codeitlikemiley/services_template)


## [License](LICENSE)

## Pull Requests

If you need to make changes to the template, please submit a pull request.
Empty file removed README_TEMPLATE.md
Empty file.
7 changes: 2 additions & 5 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
[placeholders]
"service_names" = { type = "string", prompt = "Enter comma-separated service names (e.g., auth, user):" }

[hooks]
pre = ["generate_services.rhai"]
"service_name" = { type = "string", prompt = "Initial Service: ", default = "example"}

[template]
include = ["src/**/*", "Cargo.toml"]
exclude = [".liquidrc","cargo-generate.toml", ".git", ".gitignore"]
15 changes: 0 additions & 15 deletions generate_services.rhai

This file was deleted.

3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "1.74.0"
components = [ "rustfmt", "clippy" ]
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
use {{service_name}}_service::{{service_name}}::{{service_name}}_service_server::AuthServiceServer;
use {{service_name}}_service::{{service_name}}_impl::AuthServiceImpl;
use tonic::transport::Server;

fn main () {
println!("Hello, world!");
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse()?;
let {{service_name}}_service = AuthServiceImpl::default();

Server::builder()
.add_service(AuthServiceServer::new({{service_name}}_service))
.serve(addr)
.await?;
Ok(())
}

0 comments on commit 8662c10

Please sign in to comment.