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

62 statements  

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

1from concurrent import futures 

2 

3import grpc 

4 

5from couchers.config import config 

6from couchers.constants import SERVER_THREADS 

7from couchers.interceptors import AuthValidatorInterceptor, ErrorSanitizationInterceptor, TracingInterceptor 

8from couchers.servicers.account import Account, Iris 

9from couchers.servicers.admin import Admin 

10from couchers.servicers.api import API 

11from couchers.servicers.auth import Auth 

12from couchers.servicers.blocking import Blocking 

13from couchers.servicers.bugs import Bugs 

14from couchers.servicers.communities import Communities 

15from couchers.servicers.conversations import Conversations 

16from couchers.servicers.discussions import Discussions 

17from couchers.servicers.donations import Donations, Stripe 

18from couchers.servicers.events import Events 

19from couchers.servicers.gis import GIS 

20from couchers.servicers.groups import Groups 

21from couchers.servicers.jail import Jail 

22from couchers.servicers.media import Media, get_media_auth_interceptor 

23from couchers.servicers.notifications import Notifications 

24from couchers.servicers.pages import Pages 

25from couchers.servicers.references import References 

26from couchers.servicers.reporting import Reporting 

27from couchers.servicers.requests import Requests 

28from couchers.servicers.resources import Resources 

29from couchers.servicers.search import Search 

30from couchers.servicers.threads import Threads 

31from proto import ( 

32 account_pb2_grpc, 

33 admin_pb2_grpc, 

34 api_pb2_grpc, 

35 auth_pb2_grpc, 

36 blocking_pb2_grpc, 

37 bugs_pb2_grpc, 

38 communities_pb2_grpc, 

39 conversations_pb2_grpc, 

40 discussions_pb2_grpc, 

41 donations_pb2_grpc, 

42 events_pb2_grpc, 

43 gis_pb2_grpc, 

44 groups_pb2_grpc, 

45 iris_pb2_grpc, 

46 jail_pb2_grpc, 

47 media_pb2_grpc, 

48 notifications_pb2_grpc, 

49 pages_pb2_grpc, 

50 references_pb2_grpc, 

51 reporting_pb2_grpc, 

52 requests_pb2_grpc, 

53 resources_pb2_grpc, 

54 search_pb2_grpc, 

55 stripe_pb2_grpc, 

56 threads_pb2_grpc, 

57) 

58 

59 

60def create_main_server(port): 

61 server = grpc.server( 

62 futures.ThreadPoolExecutor(SERVER_THREADS), 

63 interceptors=[ 

64 ErrorSanitizationInterceptor(), 

65 TracingInterceptor(), 

66 AuthValidatorInterceptor(), 

67 ], 

68 ) 

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

70 

71 account_pb2_grpc.add_AccountServicer_to_server(Account(), server) 

72 iris_pb2_grpc.add_IrisServicer_to_server(Iris(), server) 

73 admin_pb2_grpc.add_AdminServicer_to_server(Admin(), server) 

74 api_pb2_grpc.add_APIServicer_to_server(API(), server) 

75 auth_pb2_grpc.add_AuthServicer_to_server(Auth(), server) 

76 blocking_pb2_grpc.add_BlockingServicer_to_server(Blocking(), server) 

77 bugs_pb2_grpc.add_BugsServicer_to_server(Bugs(), server) 

78 communities_pb2_grpc.add_CommunitiesServicer_to_server(Communities(), server) 

79 conversations_pb2_grpc.add_ConversationsServicer_to_server(Conversations(), server) 

80 discussions_pb2_grpc.add_DiscussionsServicer_to_server(Discussions(), server) 

81 donations_pb2_grpc.add_DonationsServicer_to_server(Donations(), server) 

82 events_pb2_grpc.add_EventsServicer_to_server(Events(), server) 

83 gis_pb2_grpc.add_GISServicer_to_server(GIS(), server) 

84 groups_pb2_grpc.add_GroupsServicer_to_server(Groups(), server) 

85 jail_pb2_grpc.add_JailServicer_to_server(Jail(), server) 

86 notifications_pb2_grpc.add_NotificationsServicer_to_server(Notifications(), server) 

87 pages_pb2_grpc.add_PagesServicer_to_server(Pages(), server) 

88 references_pb2_grpc.add_ReferencesServicer_to_server(References(), server) 

89 reporting_pb2_grpc.add_ReportingServicer_to_server(Reporting(), server) 

90 requests_pb2_grpc.add_RequestsServicer_to_server(Requests(), server) 

91 resources_pb2_grpc.add_ResourcesServicer_to_server(Resources(), server) 

92 search_pb2_grpc.add_SearchServicer_to_server(Search(), server) 

93 stripe_pb2_grpc.add_StripeServicer_to_server(Stripe(), server) 

94 threads_pb2_grpc.add_ThreadsServicer_to_server(Threads(), server) 

95 return server 

96 

97 

98def create_media_server(port, threads=8): 

99 media_server = grpc.server( 

100 futures.ThreadPoolExecutor(threads), 

101 interceptors=[TracingInterceptor(), get_media_auth_interceptor(config["MEDIA_SERVER_BEARER_TOKEN"])], 

102 ) 

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

104 media_pb2_grpc.add_MediaServicer_to_server(Media(), media_server) 

105 return media_server