Skip to content

Commit 93ebb25

Browse files
committed
Handle ping/pong action, plus Github Action for Relase
1 parent 905d2b5 commit 93ebb25

File tree

7 files changed

+124
-1
lines changed

7 files changed

+124
-1
lines changed

.github/workflows/release.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Build and Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
build-linux:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Install Rust
16+
uses: actions-rs/toolchain@v1
17+
with:
18+
toolchain: stable
19+
target: x86_64-unknown-linux-gnu
20+
override: true
21+
22+
- name: Build
23+
run: |
24+
cargo build --release --target x86_64-unknown-linux-gnu
25+
tar -czf anypay-websockets-linux-x86_64.tar.gz -C target/x86_64-unknown-linux-gnu/release anypay-websockets
26+
27+
- name: Create Release
28+
uses: softprops/action-gh-release@v1
29+
if: startsWith(github.ref, 'refs/tags/')
30+
with:
31+
files: |
32+
anypay-websockets-linux-x86_64.tar.gz
33+
env:
34+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Upload artifact
37+
uses: actions/upload-artifact@v3
38+
with:
39+
name: anypay-websockets-linux-x86_64
40+
path: target/x86_64-unknown-linux-gnu/release/anypay-websockets

Cargo.toml

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@ shortid = "1.0.6"
2525
bigdecimal = "0.4.7"
2626
anyhow = "1.0"
2727
alloy = { version = "0.3", features = ["full"] }
28-
futures-util = "0.3"
28+
futures-util = "0.3"
29+
30+
[profile.release]
31+
opt-level = 3
32+
lto = true
33+
codegen-units = 1
34+
panic = 'abort'
35+
strip = true # Automatically strip symbols from the binary
36+
37+
[target.x86_64-unknown-linux-gnu]
38+
linker = "x86_64-unknown-linux-gnu-gcc"

docs/asyncapi.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ channels:
105105
publish:
106106
message:
107107
oneOf:
108+
- name: Ping
109+
payload:
110+
type: object
111+
required:
112+
- type
113+
properties:
114+
type:
115+
type: string
116+
const: Ping
108117
- name: CreateInvoice
109118
payload:
110119
type: object
@@ -132,6 +141,19 @@ channels:
132141
subscribe:
133142
message:
134143
oneOf:
144+
- name: Pong
145+
payload:
146+
type: object
147+
properties:
148+
type:
149+
type: string
150+
const: pong
151+
status:
152+
type: string
153+
enum: [success]
154+
timestamp:
155+
type: integer
156+
format: int64
135157
- name: Response
136158
payload:
137159
oneOf:

scripts/test_ping.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import asyncio
3+
import websockets
4+
import json
5+
from datetime import datetime
6+
7+
async def test_ping():
8+
try:
9+
# Connect to WebSocket server
10+
async with websockets.connect('ws://localhost:8080') as websocket:
11+
# Create ping message
12+
ping_message = {
13+
"action": "ping"
14+
}
15+
16+
print("Sending ping...")
17+
await websocket.send(json.dumps(ping_message))
18+
19+
# Wait for pong response
20+
response = await websocket.recv()
21+
response_data = json.loads(response)
22+
23+
print(f"Received response: {response_data}")
24+
25+
# Validate response
26+
if (response_data.get('type') == 'pong' and
27+
response_data.get('status') == 'success' and
28+
'timestamp' in response_data):
29+
30+
# Convert timestamp to readable format
31+
timestamp = datetime.fromtimestamp(response_data['timestamp'])
32+
print(f"✅ Received pong at {timestamp}")
33+
print(f"Round trip latency: {datetime.utcnow().timestamp() - response_data['timestamp']}s")
34+
else:
35+
print("❌ Invalid pong response:", response_data)
36+
37+
except Exception as e:
38+
print(f"❌ Error: {e}")
39+
40+
if __name__ == "__main__":
41+
asyncio.run(test_ping())

src/amqp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ async fn consume_events(mut consumer: Consumer) {
203203
while let Some(delivery) = consumer.next().await {
204204
if let Ok(delivery) = delivery {
205205
if let Ok(data) = std::str::from_utf8(&delivery.data) {
206+
println!("AMQP Event: {}", data);
206207
tracing::info!("AMQP Event: {}", data);
207208
}
208209
delivery.ack(BasicAckOptions::default()).await.ok();

src/server.rs

+7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ impl AnypayEventsServer {
178178
})
179179
}
180180
}
181+
Message::Ping => {
182+
json!({
183+
"type": "pong",
184+
"status": "success",
185+
"timestamp": chrono::Utc::now().timestamp()
186+
})
187+
},
181188
}
182189
}
183190

src/types.rs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub enum Message {
4545
CancelInvoice {
4646
uid: String,
4747
},
48+
#[serde(rename = "ping")]
49+
Ping,
4850
}
4951

5052
fn deserialize_number_from_string<'de, D>(deserializer: D) -> Result<f64, D::Error>

0 commit comments

Comments
 (0)