-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
82 lines (72 loc) · 2.38 KB
/
main.go
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
package main
import (
"context"
"errors"
"net"
"google.golang.org/grpc"
"github.com/monnoroch/go-inject"
"github.com/monnoroch/go-inject/auto"
"github.com/monnoroch/go-inject/examples/weather/ai"
"github.com/monnoroch/go-inject/examples/weather/blockchain"
"github.com/monnoroch/go-inject/examples/weather/constant"
grpcinject "github.com/monnoroch/go-inject/examples/weather/grpc"
proto "github.com/monnoroch/go-inject/examples/weather/proto"
)
/// The main server type for defining request handlers.
type Server struct {
AiClient ai.AiClient
BlockchainClient blockchain.BlockchainClient
}
/// Handler for the WeatherPrediction.Predict RPCs.
func (self *Server) Predict(
ctx context.Context,
request *proto.SpaceTimeLocation,
) (*proto.Weather, error) {
if !self.BlockchainClient.Pay(ctx, request.GetUserId()) {
return &proto.Weather{}, errors.New("no money -- no weather!")
}
weather := self.AiClient.AskForWeather(
ctx,
request.GetLocation(),
request.GetTimestamp(),
)
return &proto.Weather{Weather: weather}, nil
}
/// Annotation used by the weather prediction server module.
type WeatherPrediction struct{}
/// A module for providing a configured weather prediction server.
type weatherPredictionServerModule struct{}
func (_ weatherPredictionServerModule) ProvideGrpcServer(
grpcServer *grpc.Server, _ grpcinject.GrpcServer,
weatherPredictionServer *Server, _ autoinject.Auto,
) (*grpc.Server, WeatherPrediction) {
proto.RegisterWeatherPredictionServer(
grpcServer,
weatherPredictionServer,
)
return grpcServer, WeatherPrediction{}
}
func WeatherPredictionServerModule() inject.Module {
return inject.CombineModules(
weatherPredictionServerModule{},
constant.ConstantModule("ai-service:80", ai.AiService{}),
constant.ConstantModule("blockchain-service:80", blockchain.BlockchainService{}),
constant.ConstantModule(true, blockchain.BlockchainService{}),
autoinject.AutoInjectModule(new(*Server)),
)
}
func main() {
injector, _ := inject.InjectorOf(
grpcinject.GrpcServerModule{},
grpcinject.GrpcClientModule(ai.AiService{}),
grpcinject.GrpcClientModule(blockchain.BlockchainService{}),
ai.AiServiceClientModule(),
blockchain.BlockchainServiceClientModule(),
WeatherPredictionServerModule(),
)
server := injector.MustGet(
new(*grpc.Server), WeatherPrediction{},
).(*grpc.Server)
listener, _ := net.Listen("tcp", ":80")
server.Serve(listener)
}