code.vegaprotocol.io/vega@v0.79.0/blockexplorer/api/portal.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package api 17 18 import ( 19 "fmt" 20 "net" 21 22 "code.vegaprotocol.io/vega/logging" 23 24 "github.com/soheilhy/cmux" 25 ) 26 27 type Portal struct { 28 Config 29 log *logging.Logger 30 mux cmux.CMux 31 portalListener net.Listener 32 grpcListener net.Listener 33 gatewayListener net.Listener 34 } 35 36 func NewPortal(config Config, log *logging.Logger) *Portal { 37 log = log.Named(portalNamedLogger) 38 39 address := net.JoinHostPort(config.ListenAddress, fmt.Sprintf("%v", config.ListenPort)) 40 portalListener, err := net.Listen("tcp", address) 41 if err != nil { 42 log.Fatal("unable to start portal listener", logging.String("address", address), logging.Error(err)) 43 } 44 45 mux := cmux.New(portalListener) 46 grpcListener := mux.Match(cmux.HTTP2()) 47 gatewayListener := mux.Match(cmux.HTTP1Fast()) 48 49 portal := Portal{ 50 Config: config, 51 log: log, 52 mux: mux, 53 portalListener: portalListener, 54 grpcListener: grpcListener, 55 gatewayListener: gatewayListener, 56 } 57 return &portal 58 } 59 60 func (p *Portal) Serve() error { 61 p.log.Info("Starting portal") 62 return p.mux.Serve() 63 } 64 65 func (p *Portal) GatewayListener() net.Listener { 66 return p.gatewayListener 67 } 68 69 func (p *Portal) GRPCListener() net.Listener { 70 return p.grpcListener 71 } 72 73 func (p *Portal) Stop() { 74 p.log.Info("Stopping portal") 75 _ = p.gatewayListener.Close() 76 _ = p.grpcListener.Close() 77 _ = p.portalListener.Close() 78 }