-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_server.py
146 lines (133 loc) · 6.18 KB
/
grpc_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from concurrent import futures
import logging
import time
from typing import Any, Callable
import grpc
from grpc_interceptor import ServerInterceptor
from takasho.schema.common_featureset.player_api import achievement, \
announcement, baas_product, game_message, game_product, game_status, \
loot_box, player_achievement, player_event_log, player_inventory, \
player_key_value_store, player_preference, player_storage, \
push_notification, ondemand_master, step_up_loot_box, system, wallet
from takasho.schema.fes.player_api import club_achievement, club_chat, \
club_player, friend, login_bonus, reconstruction_spot, regular_ranking, \
score_ranking, zendesk
from takasho.schema.fes.player_api import player_preference \
as fes_player_preference
from takasho.schema.fes.player_api.subscription import renewal_reward
class TakashoInterceptor(ServerInterceptor):
def intercept(
self,
method: Callable,
request: Any,
context: grpc.ServicerContext,
method_name: str,
) -> Any:
"""Send initial metadata and trailing metadata specific to Takasho as
part of the RPC response.
Args:
method: The next interceptor, or method implementation.
request: The RPC request, as a protobuf message.
context: The ServicerContext pass by gRPC to the service.
method_name: A string of the form "/protobuf.package.Service/Method"
Returns:
The result of method(request, context), which is typically the RPC
method response, as a protobuf message.
"""
print('Calling ' + method_name)
# Hardcode server time to Oct 13 for now
# server_timestamp = str(int(time.time()))
server_timestamp = '1665641020'
# formatted_timestamp = time.strftime(
# '%a, %d %b %Y %H:%M:%S GMT', time.gmtime())
formatted_timestamp = 'Thu, 13 Oct 2022 06:03:40 GMT'
start_time = time.perf_counter_ns()
response = method(request, context)
end_time = time.perf_counter_ns()
service_time = str((end_time - start_time) // 1_000_000)
print(response)
context.send_initial_metadata((
('x-takasho-debug-adjustment-timestamp', server_timestamp),
('x-takasho-requested-timestamp', server_timestamp),
('x-envoy-upstream-service-time', service_time),
('date', formatted_timestamp),
('server', 'envoy'),
('via', '1.1 google'),
('alt-svc', 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000')
))
context.set_trailing_metadata((
('x-takasho-respond-timestamp', server_timestamp),
('x-takasho-server-version', 'v1.16.3'),
))
return response
def serve():
port = '50051'
server = grpc.server(
futures.ThreadPoolExecutor(max_workers=10),
interceptors=[TakashoInterceptor()]
)
ondemand_master.add_OndemandMasterServicer_to_server(
ondemand_master.OndemandMaster(), server)
system.add_SystemServicer_to_server(system.System(), server)
zendesk.add_ZendeskServicer_to_server(zendesk.Zendesk(), server)
player_preference.add_PlayerPreferenceServicer_to_server(
player_preference.PlayerPreference(), server)
player_storage.add_PlayerStorageServicer_to_server(
player_storage.PlayerStorage(), server)
fes_player_preference.add_FesPlayerPreferenceServicer_to_server(
fes_player_preference.FesPlayerPreference(), server)
push_notification.add_PushNotificationServicer_to_server(
push_notification.PushNotification(), server)
game_status.add_GameStatusServicer_to_server(
game_status.GameStatus(), server)
game_product.add_GameProductServicer_to_server(
game_product.GameProduct(), server)
wallet.add_WalletServicer_to_server(wallet.Wallet(), server)
player_inventory.add_PlayerInventoryServicer_to_server(
player_inventory.PlayerInventory(), server)
friend.add_FriendServicer_to_server(friend.Friend(), server)
score_ranking.add_ScoreRankingServicer_to_server(
score_ranking.ScoreRanking(), server)
loot_box.add_LootBoxV3Servicer_to_server(loot_box.LootBoxV3(), server)
step_up_loot_box.add_StepUpLootBoxV2Servicer_to_server(
step_up_loot_box.StepUpLootBoxV2(), server)
club_player.add_ClubPlayerServicer_to_server(
club_player.ClubPlayer(), server)
baas_product.add_BaasProductServicer_to_server(
baas_product.BaasProduct(), server)
renewal_reward.add_RenewalRewardServicer_to_server(
renewal_reward.RenewalReward(), server)
reconstruction_spot.add_ReconstructionSpotServicer_to_server(
reconstruction_spot.ReconstructionSpot(), server)
login_bonus.add_LoginBonusServicer_to_server(
login_bonus.LoginBonus(), server)
player_achievement.add_PlayerAchievementServicer_to_server(
player_achievement.PlayerAchievement(), server)
game_message.add_GameMessageServicer_to_server(
game_message.GameMessage(), server)
announcement.add_AnnouncementServicer_to_server(
announcement.Announcement(), server)
club_chat.add_ClubChatServicer_to_server(club_chat.ClubChat(), server)
club_achievement.add_ClubAchievementServicer_to_server(
club_achievement.ClubAchievement(), server)
player_event_log.add_PlayerEventLogServicer_to_server(
player_event_log.PlayerEventLog(), server)
achievement.add_AchievementServicer_to_server(
achievement.Achievement(), server)
regular_ranking.add_RegularRankingServicer_to_server(
regular_ranking.RegularRanking(), server)
player_key_value_store.add_PlayerKeyValueStoreServicer_to_server(
player_key_value_store.PlayerKeyValueStore(), server)
with open('keys/grpc.key', 'rb') as f:
private_key = f.read()
with open('keys/grpc.crt', 'rb') as f:
certificate_chain = f.read()
credentials = grpc.ssl_server_credentials(
[(private_key, certificate_chain)])
server.add_secure_port('[::]:' + port, credentials)
server.start()
print('Server started, listening on ' + port)
server.wait_for_termination()
if __name__ == '__main__':
logging.basicConfig()
serve()