github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/runtime/controller/server.go (about) 1 package controller 2 3 import ( 4 "context" 5 "github.com/lastbackend/toolkit/pkg/runtime" 6 "github.com/lastbackend/toolkit/pkg/runtime/logger" 7 "github.com/lastbackend/toolkit/pkg/server" 8 "github.com/lastbackend/toolkit/pkg/server/grpc" 9 "github.com/lastbackend/toolkit/pkg/server/http" 10 "go.uber.org/fx" 11 "regexp" 12 ) 13 14 type serverManager struct { 15 runtime.Server 16 17 runtime runtime.Runtime 18 log logger.Logger 19 20 http map[string]server.HTTPServer 21 grpc map[string]server.GRPCServer 22 } 23 24 func (c *serverManager) HTTP() server.HTTPServer { 25 slug := getSlug(c.runtime.Meta().GetName()) 26 return c.http[slug] 27 } 28 29 func (c *serverManager) GRPC() server.GRPCServer { 30 slug := getSlug(c.runtime.Meta().GetName()) 31 return c.grpc[slug] 32 } 33 34 func (c *serverManager) HTTPGet(name string) server.HTTPServer { 35 slug := getSlug(name) 36 return c.http[slug] 37 } 38 39 func (c *serverManager) HTTPNew(name string, options *server.HTTPServerOptions) server.HTTPServer { 40 41 if name == "" { 42 name = c.runtime.Meta().GetSlug() 43 } 44 45 srv := http.NewServer(name, c.runtime, options) 46 slug := getSlug(name) 47 c.http[slug] = srv 48 return c.http[slug] 49 } 50 51 func (c *serverManager) HTTPList() map[string]server.HTTPServer { 52 return c.http 53 } 54 55 func (c *serverManager) GRPCGet(name string) server.GRPCServer { 56 slug := getSlug(name) 57 return c.grpc[slug] 58 } 59 60 func (c *serverManager) GRPCNew(name string, options *server.GRPCServerOptions) server.GRPCServer { 61 if name == "" { 62 name = c.runtime.Meta().GetSlug() 63 } 64 srv := grpc.NewServer(c.runtime, name, options) 65 slug := getSlug(name) 66 c.grpc[slug] = srv 67 return c.grpc[slug] 68 } 69 70 func (c *serverManager) GRPCList() map[string]server.GRPCServer { 71 return c.grpc 72 } 73 74 func (c *serverManager) Provides() []interface{} { 75 76 provides := make([]interface{}, 0) 77 78 for _, s := range c.grpc { 79 service := s.GetService() 80 if service != nil { 81 provides = append(provides, service) 82 83 interceptors := s.GetInterceptors() 84 for _, interceptor := range interceptors { 85 provides = append(provides, fx.Annotate( 86 interceptor, 87 fx.As(new(server.GRPCInterceptor)), 88 fx.ResultTags(`group:"interceptors"`), 89 ), 90 ) 91 } 92 93 } 94 } 95 96 for _, s := range c.http { 97 middlewares := s.GetMiddlewares() 98 for _, middleware := range middlewares { 99 provides = append(provides, fx.Annotate( 100 middleware, 101 fx.As(new(server.HttpServerMiddleware)), 102 fx.ResultTags(`group:"middlewares"`), 103 ), 104 ) 105 } 106 107 } 108 109 return provides 110 } 111 112 func (c *serverManager) Constructors() []interface{} { 113 114 provides := make([]interface{}, 0) 115 116 for _, s := range c.grpc { 117 provides = append(provides, s.GetConstructor()) 118 provides = append(provides, fx.Annotate( 119 s.GetInterceptorsConstructor(), 120 fx.ParamTags(`group:"interceptors"`))) 121 } 122 123 for _, s := range c.http { 124 125 provides = append(provides, fx.Annotate( 126 s.GetConstructor(), 127 fx.ParamTags(`group:"middlewares"`))) 128 } 129 130 return provides 131 } 132 133 func (c *serverManager) Start(ctx context.Context) error { 134 135 for _, s := range c.grpc { 136 if err := s.Start(ctx); err != nil { 137 c.log.V(5).Errorf("GRPC server start failed: %v", err) 138 return err 139 } 140 } 141 142 for _, s := range c.http { 143 if err := s.Start(ctx); err != nil { 144 c.log.V(5).Errorf("HTTP server start failed: %v", err) 145 return err 146 } 147 } 148 149 return nil 150 } 151 152 func (c *serverManager) Stop(ctx context.Context) error { 153 154 for _, s := range c.http { 155 if err := s.Stop(ctx); err != nil { 156 return err 157 } 158 } 159 160 for _, s := range c.grpc { 161 if err := s.Stop(); err != nil { 162 return err 163 } 164 } 165 166 return nil 167 } 168 169 func getSlug(s string) string { 170 return regexp.MustCompile(`[^_a-zA-Z0-9 ]+`).ReplaceAllString(s, "_") 171 } 172 173 func newServerController(_ context.Context, runtime runtime.Runtime) runtime.Server { 174 175 pl := new(serverManager) 176 177 pl.runtime = runtime 178 pl.log = runtime.Log() 179 180 pl.http = make(map[string]server.HTTPServer, 0) 181 pl.grpc = make(map[string]server.GRPCServer, 0) 182 183 return pl 184 }