-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (59 loc) · 1.67 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
package main
import (
"context"
"fmt"
"log"
"net"
"os"
"github.com/golang/protobuf/jsonpb"
"google.golang.org/genproto/googleapis/ads/googleads/v3/services"
"google.golang.org/grpc"
)
const (
port = ":50051"
)
type server struct{}
func (s *server) Search(ctx context.Context, in *services.SearchGoogleAdsRequest) (*services.SearchGoogleAdsResponse, error) {
log.Printf("Received(Service): %v", in)
file, err := os.Open("./search_term_view.json")
if err != nil {
log.Fatalf("failed to read file: %v", err)
}
var response services.SearchGoogleAdsResponse
err = jsonpb.Unmarshal(file, &response)
if err != nil {
log.Fatalf("%s", err)
}
return &response, nil
}
func (s *server) SearchStream(in *services.SearchGoogleAdsStreamRequest, stream services.GoogleAdsService_SearchStreamServer) error {
file, err := os.Open("./search_term_view_stream.json")
if err != nil {
log.Fatalf("failed to read file: %v", err)
}
var response services.SearchGoogleAdsStreamResponse
err = jsonpb.Unmarshal(file, &response)
if err != nil {
log.Fatalf("%s", err)
}
if err = stream.Send(&response); err != nil {
log.Fatalf("%s", err)
}
return nil
}
func (s *server) Mutate(ctx context.Context, in *services.MutateGoogleAdsRequest) (*services.MutateGoogleAdsResponse, error) {
panic(fmt.Sprintf("Mutate of SearchGoogleAdsRequest hasn't been implemented yet."))
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
services.RegisterGoogleAdsServiceServer(s, &server{})
log.Printf("start to serve: %v", port)
err = s.Serve(lis)
if err != nil {
log.Fatalf("failed to serve on port %s", err)
}
}