1 module hunt.framework.provider.GrpcServiceProvider;
2 
3 import hunt.framework.provider.ServiceProvider;
4 import hunt.framework.config;
5 import hunt.http.HttpVersion;
6 import hunt.logging;
7 import grpc;
8 
9 import poodinis;
10 
11 import std.algorithm;
12 import std.array;
13 import std.format;
14 import std.string;
15 
16 
17 /**
18  * 
19  */
20 class GrpcServiceProvider : ServiceProvider {
21 
22     private bool isGrpcServerEnabled = false;
23 
24     override void register() {
25         container().register!GrpcServer.initializedBy({
26             ApplicationConfig appConfig = container().resolve!ApplicationConfig();
27             GrpcServerConf serverConf = appConfig.grpc.server;
28             isGrpcServerEnabled = serverConf.enabled;
29             if(isGrpcServerEnabled) {
30                 import hunt.http.server.HttpServerOptions;
31 
32                 auto httpServerOptions = new HttpServerOptions();
33                 httpServerOptions.setSecureConnectionEnabled(false);
34                 httpServerOptions.setFlowControlStrategy("simple");
35                 httpServerOptions.getTcpConfiguration().workerThreadSize = serverConf.workerThreads;
36                 //httpServerOptions.getTcpConfiguration().setTimeout(60 * 1000);
37                 httpServerOptions.setProtocol(HttpVersion.HTTP_2.asString());
38 
39                 GrpcServer server = new GrpcServer(httpServerOptions);
40                 server.listen(serverConf.host, serverConf.port);
41                 infof("gRPC server started at %s:%d.", serverConf.host, serverConf.port);
42                 return server;
43             } else {
44                 // version(HUNT_DEBUG) warning("The GrpcService is disabled.");
45                 throw new Exception("The GrpcService is disabled.");
46             }
47 
48         }).singleInstance();
49 
50         container().register!GrpcService.initializedBy({
51             ApplicationConfig appConfig = container().resolve!ApplicationConfig();
52             return new GrpcService(appConfig.grpc.clientChannels);
53 
54         }).singleInstance();        
55 
56     }
57 
58     override void boot() {
59         if(isGrpcServerEnabled) {
60             GrpcServer grpcServer = container().resolve!GrpcServer();
61             grpcServer.start();
62         }
63     }
64 }
65 
66 
67 /**
68  * 
69  */
70 class GrpcService {
71     
72     private GrpcClientConf[string] _clientOptions;
73 
74     this(GrpcClientConf[] clientOptions) {
75         foreach(ref GrpcClientConf conf; clientOptions) {
76             _clientOptions[conf.name] = conf;
77         }
78     }
79 
80 
81     GrpcServer server() {
82         return serviceContainer().resolve!GrpcServer();
83     }
84 
85     Channel getChannel(string name) {
86         GrpcClientConf* itemPtr = name in _clientOptions;
87         if(itemPtr is null) {
88             throw new Exception(format("No options found for %s", name));
89         }
90         return new Channel(itemPtr.host, itemPtr.port);        
91     }
92 }