blob: efd0be5c136ed4cd8e0b074e76dd2309b4d68424 [file] [log] [blame]
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package api
18
19import (
20 "fmt"
21 schema "git.monogon.dev/source/nexantic.git/core/generated/api"
22 "git.monogon.dev/source/nexantic.git/core/internal/common"
Leopold Schabel68c58752019-11-14 21:00:59 +010023 "git.monogon.dev/source/nexantic.git/core/internal/common/service"
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020024 "git.monogon.dev/source/nexantic.git/core/internal/consensus"
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020025 "go.uber.org/zap"
26 "google.golang.org/grpc"
Leopold Schabela4516f92019-12-04 20:27:05 +000027 "google.golang.org/grpc/reflection"
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020028 "net"
29)
30
31type (
32 Server struct {
Leopold Schabel68c58752019-11-14 21:00:59 +010033 *service.BaseService
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020034
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020035 setupService common.SetupService
36 grpcServer *grpc.Server
37
38 consensusService *consensus.Service
39
40 config *Config
41 }
42
43 Config struct {
44 Port uint16
45 }
46)
47
48func NewApiServer(config *Config, logger *zap.Logger, setupService common.SetupService, consensusService *consensus.Service) (*Server, error) {
49 s := &Server{
50 config: config,
51 setupService: setupService,
52 consensusService: consensusService,
53 }
54
Leopold Schabel68c58752019-11-14 21:00:59 +010055 s.BaseService = service.NewBaseService("api", logger, s)
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020056
57 grpcServer := grpc.NewServer()
58 schema.RegisterClusterManagementServer(grpcServer, s)
59 schema.RegisterSetupServiceServer(grpcServer, s)
60
Leopold Schabela4516f92019-12-04 20:27:05 +000061 reflection.Register(grpcServer)
62
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020063 s.grpcServer = grpcServer
64
65 return s, nil
66}
67
68func (s *Server) OnStart() error {
69 listenHost := fmt.Sprintf(":%d", s.config.Port)
70 lis, err := net.Listen("tcp", listenHost)
71 if err != nil {
72 s.Logger.Fatal("failed to listen", zap.Error(err))
73 }
74
75 go func() {
76 err = s.grpcServer.Serve(lis)
77 s.Logger.Error("API server failed", zap.Error(err))
78 }()
79
Leopold Schabel68c58752019-11-14 21:00:59 +010080 s.Logger.Info("gRPC listening", zap.String("host", listenHost))
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020081
82 return nil
83}
84
85func (s *Server) OnStop() error {
86 s.grpcServer.Stop()
87
88 return nil
89}