github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/gate/server/gate_server.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package server 20 21 import ( 22 "context" 23 "time" 24 25 "go.uber.org/fx" 26 27 "github.com/e154/smart-home/common/events" 28 "github.com/e154/smart-home/common/logger" 29 "github.com/e154/smart-home/models" 30 "github.com/e154/smart-home/system/bus" 31 "github.com/e154/smart-home/system/gate/server/wsp" 32 ) 33 34 var ( 35 log = logger.MustGetLogger("gate") 36 ) 37 38 // GateServer ... 39 type GateServer struct { 40 eventBus bus.Bus 41 cfg Config 42 proxy *wsp.Server 43 server *Server 44 gateConfig *models.GateConfig 45 } 46 47 // NewGateServer ... 48 func NewGateServer(lc fx.Lifecycle, 49 eventBus bus.Bus, 50 gateConfig *models.GateConfig) (gate *GateServer) { 51 52 gate = &GateServer{ 53 eventBus: eventBus, 54 gateConfig: gateConfig, 55 } 56 57 lc.Append(fx.Hook{ 58 OnStart: func(ctx context.Context) error { 59 return gate.Start(ctx) 60 61 }, 62 OnStop: func(ctx context.Context) error { 63 return gate.Shutdown(ctx) 64 }, 65 }) 66 67 return 68 } 69 70 // Start ... 71 func (g *GateServer) Start(ctx context.Context) (err error) { 72 73 config := &wsp.Config{ 74 Timeout: time.Duration(g.gateConfig.ProxyTimeout) * time.Second, 75 IdleTimeout: time.Duration(g.gateConfig.ProxyIdleTimeout) * time.Second, 76 SecretKey: g.gateConfig.ProxySecretKey, 77 } 78 g.proxy = wsp.NewServer(config) 79 g.proxy.Start() 80 81 cfg := &Config{ 82 HttpPort: g.gateConfig.ApiHttpPort, 83 HttpsPort: g.gateConfig.ApiHttpsPort, 84 Debug: g.gateConfig.ApiDebug, 85 Pprof: g.gateConfig.Pprof, 86 Gzip: g.gateConfig.ApiGzip, 87 Https: g.gateConfig.Https, 88 Domain: g.gateConfig.Domain, 89 } 90 g.server = NewServer(cfg, g.proxy) 91 g.server.Start() 92 93 log.Info("Started ...") 94 95 g.eventBus.Publish("system/services/gate_server", events.EventServiceStarted{Service: "GateServer"}) 96 97 return 98 } 99 100 // Shutdown ... 101 func (g *GateServer) Shutdown(ctx context.Context) (err error) { 102 103 log.Info("Shutdown ...") 104 105 g.server.Shutdown(ctx) 106 107 g.proxy.Shutdown() 108 109 g.eventBus.Publish("system/services/gate_server", events.EventServiceStopped{Service: "GateServer"}) 110 return 111 }