Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changelog/1762114944.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
applies_to:
- server
authors:
- drganjoo
references:
- smithy-rs#3362
breaking: false
new_feature: true
bug_fix: false
---
Add http-1x examples demonstrating server SDK usage with both [email protected] and [email protected]

Adds legacy examples for http@0 and updated examples for http@1 with hyper 1.x compatibility.
3 changes: 3 additions & 0 deletions examples/legacy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pokemon-service-client/
pokemon-service-server-sdk/
Cargo.lock
15 changes: 15 additions & 0 deletions examples/legacy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Without this configuration, the workspace will be read from `rust-runtime`, causing the build to fail.
[workspace]
resolver = "2"
members = [
"pokemon-service-common",
"pokemon-service",
"pokemon-service-tls",
"pokemon-service-lambda",
"pokemon-service-server-sdk",
"pokemon-service-client",
"pokemon-service-client-usage",
]

[profile.release]
lto = true
44 changes: 44 additions & 0 deletions examples/legacy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
SRC_DIR := $(shell git rev-parse --show-toplevel)
CUR_DIR := $(shell pwd)
GRADLE := $(SRC_DIR)/gradlew
SERVER_SDK_DST := $(CUR_DIR)/pokemon-service-server-sdk
CLIENT_SDK_DST := $(CUR_DIR)/pokemon-service-client
SERVER_SDK_SRC := $(SRC_DIR)/codegen-server-test/build/smithyprojections/codegen-server-test/pokemon-service-server-sdk-http0x/rust-server-codegen
CLIENT_SDK_SRC := $(SRC_DIR)/codegen-client-test/build/smithyprojections/codegen-client-test/pokemon-service-client-http0x/rust-client-codegen

all: codegen

codegen:
$(GRADLE) --project-dir $(SRC_DIR) -P modules='pokemon-service-server-sdk-http0x,pokemon-service-client-http0x' :codegen-client-test:assemble :codegen-server-test:assemble
mkdir -p $(SERVER_SDK_DST) $(CLIENT_SDK_DST)
cp -av $(SERVER_SDK_SRC)/* $(SERVER_SDK_DST)/
cp -av $(CLIENT_SDK_SRC)/* $(CLIENT_SDK_DST)/

build: codegen
cargo build

run: codegen
cargo run

clippy: codegen
cargo clippy

test: codegen
cargo test

doc-open: codegen
cargo doc --no-deps --open

clean:
cargo clean || echo "Unable to run cargo clean"

lambda_watch:
cargo lambda watch

lambda_invoke:
cargo lambda invoke pokemon-service-lambda --data-file pokemon-service/tests/fixtures/example-apigw-request.json

distclean: clean
rm -rf $(SERVER_SDK_DST) $(CLIENT_SDK_DST) Cargo.lock

.PHONY: all
179 changes: 179 additions & 0 deletions examples/legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# Legacy HTTP 0.x Examples

This directory contains examples for Smithy-rs using HTTP 0.x (hyper 0.14, http 0.2). These examples use the legacy HTTP stack with `aws-smithy-legacy-http` and `aws-smithy-legacy-http-server`.

For HTTP 1.x examples (hyper 1.x, http 1.x), see the parent [examples](../) directory.

## Building

### 1. Generate the SDKs

From this directory, run:

```bash
make codegen
```

This will generate:
- `pokemon-service-server-sdk-http0x` - Server SDK using HTTP 0.x
- `pokemon-service-client-http0x` - Client SDK using HTTP 0.x

The generated SDKs are copied to:
- `pokemon-service-server-sdk/`
- `pokemon-service-client/`

### 2. Build all examples

```bash
cargo build
```

Or to check without building artifacts:

```bash
cargo check
```

## Running the Examples

### Start the Pokemon Service

In one terminal, start the server:

```bash
cargo run --bin pokemon-service
```

The server will start on `http://localhost:13734`

### Run Client Examples

In another terminal, from the `pokemon-service-client-usage/` directory:

```bash
cd pokemon-service-client-usage
cargo run --example simple-client
```

#### Available Client Examples

| Example | Description |
|---------|-------------|
| `simple-client` | Basic client usage - creates a client and calls an operation |
| `endpoint-resolver` | Custom endpoint resolver configuration |
| `handling-errors` | Sending input parameters and handling errors |
| `custom-header` | Adding custom headers to requests |
| `custom-header-using-interceptor` | Accessing operation name in an interceptor |
| `response-header-interceptor` | Getting operation name and accessing response before deserialization |
| `use-config-bag` | Using the property bag to pass data across interceptors |
| `retry-customize` | Customizing retry settings |
| `timeout-config` | Configuring timeouts |
| `mock-request` | Using custom HttpConnector for mock responses |
| `trace-serialize` | Tracing request/response during serialization |
| `client-connector` | Changing TLS configuration |

To list all available examples:

```bash
cd pokemon-service-client-usage
cargo run --example
```

### Other Services

#### Pokemon Service with TLS

```bash
cargo run --bin pokemon-service-tls
```

#### Pokemon Service on AWS Lambda

```bash
cargo run --bin pokemon-service-lambda
```

## Project Structure

```
legacy/
├── pokemon-service/ # Main HTTP service implementation
├── pokemon-service-tls/ # TLS-enabled service
├── pokemon-service-lambda/ # AWS Lambda service
├── pokemon-service-common/ # Shared service logic
├── pokemon-service-client-usage/ # Client usage examples
├── pokemon-service-server-sdk/ # Generated server SDK (HTTP 0.x)
└── pokemon-service-client/ # Generated client SDK (HTTP 0.x)
```

## Key Dependencies (HTTP 0.x)

- `hyper = "0.14"`
- `http = "0.2"`
- `aws-smithy-legacy-http`
- `aws-smithy-legacy-http-server`

## Regenerating SDKs

If you need to regenerate the SDKs from scratch:

```bash
rm -rf pokemon-service-server-sdk pokemon-service-client
make codegen
```

## Testing

Run all tests:

```bash
cargo test
```

Run tests for a specific package:

```bash
cargo test -p pokemon-service
```

## Troubleshooting

### Port Already in Use

If port 13734 is already in use, you can specify a different port:

```bash
cargo run --bin pokemon-service -- --port 8080
```

Then update the client examples to use the new port by setting the environment variable:

```bash
POKEMON_SERVICE_URL=http://localhost:8080 cargo run --example simple-client
```

### SDK Generation Issues

If the generated SDKs have issues, try cleaning and regenerating:

```bash
# Clean generated SDKs
rm -rf pokemon-service-server-sdk pokemon-service-client

# Clean gradle cache
cd ../..
./gradlew clean

# Regenerate
cd examples/legacy
make codegen
```

## Migration to HTTP 1.x

For new projects, we recommend using the HTTP 1.x examples in the parent [examples](../) directory. These legacy examples are maintained for backward compatibility and for projects that need to use the HTTP 0.x stack.

The main differences:
- HTTP 1.x uses `hyper 1.x`, `http 1.x`
- HTTP 1.x uses `aws-smithy-http`, `aws-smithy-http-server` (not legacy versions)
- HTTP 1.x has better performance and modern async runtime support
39 changes: 39 additions & 0 deletions examples/legacy/pokemon-service-client-usage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "pokemon-service-client-usage"
version = "0.1.0"
edition = "2021"
publish = false

[features]


[dependencies]
# The generated client utilizes types defined in other crates, such as `aws_smithy_types`
# and `aws_smithy_http`. However, most of these types are re-exported by the generated client,
# eliminating the need to directly depend on the crates that provide them. In rare instances,
# you may still need to include one of these crates as a dependency. Examples that require this
# are specifically noted in comments above the corresponding dependency in this file.
pokemon-service-client = { path = "../pokemon-service-client/", package = "pokemon-service-client-http0x", features = ["behavior-version-latest"] }

# Required for getting the operation name from the `Metadata`.
aws-smithy-legacy-http = { path = "../../../rust-runtime/aws-smithy-legacy-http/" }

# Required for `Storable` and `StoreReplace` in `response-header-interceptor` example.
aws-smithy-types = { path = "../../../rust-runtime/aws-smithy-types/" }

# Required for `HyperClientBuilder` in `client-connector` example.
aws-smithy-runtime = { path = "../../../rust-runtime/aws-smithy-runtime/", features=["test-util"] }

# Required for `Metadata` in `custom-header-using-interceptor` example.
aws-smithy-runtime-api = { path = "../../../rust-runtime/aws-smithy-runtime-api/", features=["client"] }


hyper = { version = "0.14.25", features = ["client", "full"] }
tokio = {version = "1.26.0", features=["full"]}
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
rustls = "0.21.8"
hyper-rustls = "0.24.1"
http = "0.2.9"
uuid = {version="1.4.1", features = ["v4"]}
thiserror = "1.0.49"
49 changes: 49 additions & 0 deletions examples/legacy/pokemon-service-client-usage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# smithy-rs Client Examples

This package contains some examples on how to use the Smithy Client to communicate
with a Smithy-based service.

## Pre-requisites

1. Build the `pokemon-service-client` and `pokemon-service` by invoking `make` in the
[examples](https://github.com/smithy-lang/smithy-rs/tree/main/examples) folder.

```console
make
```

2. Run the Pokemon service locally by issuing the following command from the
[examples](https://github.com/smithy-lang/smithy-rs/tree/main/examples) folder. This
will launch the Smithy-Rs based service on TCP port 13734.

```console
cargo run --bin pokemon-service
```

## Running the examples

You can view a list of examples by running `cargo run --example` from the
[pokemon-service-client-usage](https://github.com/smithy-lang/smithy-rs/tree/main/examples/pokemon-service-client-usage)
folder. To run an example, pass its name to the `cargo run --example` command, e.g.:

```console
cargo run --example simple-client
```

## List of examples

| Rust Example | Description |
|--------------------------------|-------------------------------------------------------------------------|
| simple-client | Creates a Smithy Client and calls an operation on it. |
| endpoint-resolver | How to set a custom endpoint resolver. |
| handling-errors | How to send an input parameter to an operation, and to handle errors. |
| custom-header | How to add headers to a request. |
| custom-header-using-interceptor| How to access operation name being called in an interceptor. |
| response-header-interceptor | How to get operation name and access response before it is deserialized.|
| use-config-bag | How to use the property bag to pass data across interceptors. |
| retries-customize | Customize retry settings. |
| retries-disable | How to disable retries. |
| timeout-config | How to configure timeouts. |
| mock-request | Use a custom HttpConnector / Client to generate mock responses. |
| trace-serialize | Trace request and response as they are serialized / deserialized. |
| client-connector | Shows how to change TLS related configuration. |
Loading
Loading