Coverage for src/couchers/server.py: 100%

62 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-11-21 04:21 +0000

1from concurrent import futures 

2 

3import grpc 

4 

5from couchers.config import config 

6from couchers.constants import SERVER_THREADS 

7from couchers.interceptors import ( 

8 AuthValidatorInterceptor, 

9 CookieInterceptor, 

10 ErrorSanitizationInterceptor, 

11 OTelInterceptor, 

12 SessionInterceptor, 

13 TracingInterceptor, 

14) 

15from couchers.servicers.account import Account, Iris 

16from couchers.servicers.admin import Admin 

17from couchers.servicers.api import API 

18from couchers.servicers.auth import Auth 

19from couchers.servicers.blocking import Blocking 

20from couchers.servicers.bugs import Bugs 

21from couchers.servicers.communities import Communities 

22from couchers.servicers.conversations import Conversations 

23from couchers.servicers.discussions import Discussions 

24from couchers.servicers.donations import Donations, Stripe 

25from couchers.servicers.events import Events 

26from couchers.servicers.gis import GIS 

27from couchers.servicers.groups import Groups 

28from couchers.servicers.jail import Jail 

29from couchers.servicers.media import Media, get_media_auth_interceptor 

30from couchers.servicers.notifications import Notifications 

31from couchers.servicers.pages import Pages 

32from couchers.servicers.references import References 

33from couchers.servicers.reporting import Reporting 

34from couchers.servicers.requests import Requests 

35from couchers.servicers.resources import Resources 

36from couchers.servicers.search import Search 

37from couchers.servicers.threads import Threads 

38from proto import ( 

39 account_pb2_grpc, 

40 admin_pb2_grpc, 

41 api_pb2_grpc, 

42 auth_pb2_grpc, 

43 blocking_pb2_grpc, 

44 bugs_pb2_grpc, 

45 communities_pb2_grpc, 

46 conversations_pb2_grpc, 

47 discussions_pb2_grpc, 

48 donations_pb2_grpc, 

49 events_pb2_grpc, 

50 gis_pb2_grpc, 

51 groups_pb2_grpc, 

52 iris_pb2_grpc, 

53 jail_pb2_grpc, 

54 media_pb2_grpc, 

55 notifications_pb2_grpc, 

56 pages_pb2_grpc, 

57 references_pb2_grpc, 

58 reporting_pb2_grpc, 

59 requests_pb2_grpc, 

60 resources_pb2_grpc, 

61 search_pb2_grpc, 

62 stripe_pb2_grpc, 

63 threads_pb2_grpc, 

64) 

65 

66 

67def create_main_server(port): 

68 server = grpc.server( 

69 futures.ThreadPoolExecutor(SERVER_THREADS), 

70 interceptors=[ 

71 ErrorSanitizationInterceptor(), 

72 OTelInterceptor(), 

73 TracingInterceptor(), 

74 AuthValidatorInterceptor(), 

75 CookieInterceptor(), 

76 SessionInterceptor(), 

77 ], 

78 ) 

79 server.add_insecure_port(f"[::]:{port}") 

80 

81 account_pb2_grpc.add_AccountServicer_to_server(Account(), server) 

82 iris_pb2_grpc.add_IrisServicer_to_server(Iris(), server) 

83 admin_pb2_grpc.add_AdminServicer_to_server(Admin(), server) 

84 api_pb2_grpc.add_APIServicer_to_server(API(), server) 

85 auth_pb2_grpc.add_AuthServicer_to_server(Auth(), server) 

86 blocking_pb2_grpc.add_BlockingServicer_to_server(Blocking(), server) 

87 bugs_pb2_grpc.add_BugsServicer_to_server(Bugs(), server) 

88 communities_pb2_grpc.add_CommunitiesServicer_to_server(Communities(), server) 

89 conversations_pb2_grpc.add_ConversationsServicer_to_server(Conversations(), server) 

90 discussions_pb2_grpc.add_DiscussionsServicer_to_server(Discussions(), server) 

91 donations_pb2_grpc.add_DonationsServicer_to_server(Donations(), server) 

92 events_pb2_grpc.add_EventsServicer_to_server(Events(), server) 

93 gis_pb2_grpc.add_GISServicer_to_server(GIS(), server) 

94 groups_pb2_grpc.add_GroupsServicer_to_server(Groups(), server) 

95 jail_pb2_grpc.add_JailServicer_to_server(Jail(), server) 

96 notifications_pb2_grpc.add_NotificationsServicer_to_server(Notifications(), server) 

97 pages_pb2_grpc.add_PagesServicer_to_server(Pages(), server) 

98 references_pb2_grpc.add_ReferencesServicer_to_server(References(), server) 

99 reporting_pb2_grpc.add_ReportingServicer_to_server(Reporting(), server) 

100 requests_pb2_grpc.add_RequestsServicer_to_server(Requests(), server) 

101 resources_pb2_grpc.add_ResourcesServicer_to_server(Resources(), server) 

102 search_pb2_grpc.add_SearchServicer_to_server(Search(), server) 

103 stripe_pb2_grpc.add_StripeServicer_to_server(Stripe(), server) 

104 threads_pb2_grpc.add_ThreadsServicer_to_server(Threads(), server) 

105 return server 

106 

107 

108def create_media_server(port, threads=8): 

109 media_server = grpc.server( 

110 futures.ThreadPoolExecutor(threads), 

111 interceptors=[ 

112 TracingInterceptor(), 

113 get_media_auth_interceptor(config["MEDIA_SERVER_BEARER_TOKEN"]), 

114 SessionInterceptor(), 

115 ], 

116 ) 

117 media_server.add_insecure_port(f"[::]:{port}") 

118 media_pb2_grpc.add_MediaServicer_to_server(Media(), media_server) 

119 return media_server