Skip to content

Commit 4e1af30

Browse files
committed
feature: initial grpc setup
0 parents  commit 4e1af30

File tree

13 files changed

+452
-0
lines changed

13 files changed

+452
-0
lines changed

.gitignore

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
MANIFEST
23+
24+
# Virtual Environments
25+
venv/
26+
env/
27+
ENV/
28+
.env
29+
.venv
30+
env.bak/
31+
venv.bak/
32+
33+
# gRPC generated files
34+
*_pb2.py
35+
*_pb2_grpc.py
36+
37+
# IDE specific files
38+
.idea/
39+
.vscode/
40+
*.swp
41+
*.swo
42+
.DS_Store
43+
.project
44+
.pydevproject
45+
.settings/
46+
47+
# Logs
48+
*.log
49+
logs/
50+
51+
# Local configuration
52+
*.local.py
53+
*.local.yaml
54+
*.local.yml
55+
*.local.json
56+
57+
# Database
58+
*.sqlite3
59+
*.db
60+
61+
# Test cache
62+
.pytest_cache/
63+
.coverage
64+
htmlcov/
65+
.tox/
66+
nosetests.xml
67+
coverage.xml
68+
*.cover
69+
70+
# Documentation
71+
docs/_build/
72+
docs/site/
73+
74+
# Environment variables
75+
.env
76+
.env.local
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# mypy
82+
.mypy_cache/
83+
.dmypy.json
84+
dmypy.json
85+
86+
# Docker
87+
.dockerignore
88+
docker-compose.override.yml

Pipfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
grpcio = "*"
8+
grpcio-tools = "*"
9+
protobuf = "*"
10+
watchdog = "*"
11+
12+
[dev-packages]
13+
14+
[requires]
15+
python_version = "3.12"
16+
python_full_version = "3.12.3"

Pipfile.lock

Lines changed: 201 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__docs/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* https://github.com/AliBigdeli/FastApi-GRPC-Todo-Microservice-App/tree/main

generate.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python -m grpc_tools.protoc -I=protobuf/ --python_out=src/proto --grpc_python_out=src/proto protobuf/*.proto
2+
cd src/proto && sed -i '' 's/^\(import.*pb2\)/from . \1/g' *.py

protobuf/hello.proto

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
syntax = "proto3";
2+
3+
package hello;
4+
5+
// Define the greeting service
6+
service Greeter {
7+
// Define a simple RPC method
8+
rpc SayHello (HelloRequest) returns (HelloResponse) {}
9+
}
10+
11+
// Request message
12+
message HelloRequest {
13+
string name = 1;
14+
}
15+
16+
// Response message
17+
message HelloResponse {
18+
string message = 1;
19+
}

run_project.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3 ./src/hot_reload.py ./src/main.py

src/__init__.py

Whitespace-only changes.

src/hot_reload.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import sys
3+
import time
4+
import subprocess
5+
from watchdog.observers import Observer
6+
from watchdog.events import FileSystemEventHandler
7+
8+
class ServerRestartHandler(FileSystemEventHandler):
9+
def __init__(self, server_file):
10+
self.server_file = server_file
11+
self.process = None
12+
self.start_server()
13+
14+
def start_server(self):
15+
# Kill previous process if it exists
16+
if self.process:
17+
print("Stopping gRPC server...")
18+
self.process.terminate()
19+
self.process.wait()
20+
21+
22+
# Start new server process
23+
print("Starting gRPC server...")
24+
self.process = subprocess.Popen([sys.executable, self.server_file])
25+
26+
def on_modified(self, event):
27+
# Check if it's a Python file
28+
if event.src_path.endswith('.py'):
29+
print(f"Detected change in {event.src_path}")
30+
self.start_server()
31+
32+
if __name__ == "__main__":
33+
if len(sys.argv) < 2:
34+
print("Usage: python hot_reload.py <server_file.py>")
35+
sys.exit(1)
36+
37+
server_file = sys.argv[1]
38+
path = os.path.dirname(os.path.abspath(server_file)) or '.'
39+
40+
event_handler = ServerRestartHandler(server_file)
41+
observer = Observer()
42+
observer.schedule(event_handler, path, recursive=True)
43+
observer.start()
44+
45+
try:
46+
while True:
47+
time.sleep(1)
48+
except KeyboardInterrupt:
49+
observer.stop()
50+
if event_handler.process:
51+
event_handler.process.terminate()
52+
53+
observer.join()

0 commit comments

Comments
 (0)