Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package api |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | schema "git.monogon.dev/source/nexantic.git/core/generated/api" |
| 22 | "git.monogon.dev/source/nexantic.git/core/internal/common" |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 23 | "git.monogon.dev/source/nexantic.git/core/internal/common/service" |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 24 | "git.monogon.dev/source/nexantic.git/core/internal/consensus" |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 25 | "go.uber.org/zap" |
| 26 | "google.golang.org/grpc" |
Leopold Schabel | a4516f9 | 2019-12-04 20:27:05 +0000 | [diff] [blame^] | 27 | "google.golang.org/grpc/reflection" |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 28 | "net" |
| 29 | ) |
| 30 | |
| 31 | type ( |
| 32 | Server struct { |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 33 | *service.BaseService |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 34 | |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 35 | 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 | |
| 48 | func 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 Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 55 | s.BaseService = service.NewBaseService("api", logger, s) |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 56 | |
| 57 | grpcServer := grpc.NewServer() |
| 58 | schema.RegisterClusterManagementServer(grpcServer, s) |
| 59 | schema.RegisterSetupServiceServer(grpcServer, s) |
| 60 | |
Leopold Schabel | a4516f9 | 2019-12-04 20:27:05 +0000 | [diff] [blame^] | 61 | reflection.Register(grpcServer) |
| 62 | |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 63 | s.grpcServer = grpcServer |
| 64 | |
| 65 | return s, nil |
| 66 | } |
| 67 | |
| 68 | func (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 Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 80 | s.Logger.Info("gRPC listening", zap.String("host", listenHost)) |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 81 | |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func (s *Server) OnStop() error { |
| 86 | s.grpcServer.Stop() |
| 87 | |
| 88 | return nil |
| 89 | } |