github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/tests/requires/endpoints.go (about) 1 package requires 2 3 import ( 4 "context" 5 "fmt" 6 "net/url" 7 "reflect" 8 "time" 9 10 "github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/clients/applet_mgr" 11 base "github.com/machinefi/w3bstream/pkg/depends/base/types" 12 "github.com/machinefi/w3bstream/pkg/depends/conf/filesystem/local" 13 "github.com/machinefi/w3bstream/pkg/depends/conf/http" 14 confid "github.com/machinefi/w3bstream/pkg/depends/conf/id" 15 confjwt "github.com/machinefi/w3bstream/pkg/depends/conf/jwt" 16 conflog "github.com/machinefi/w3bstream/pkg/depends/conf/log" 17 confmq "github.com/machinefi/w3bstream/pkg/depends/conf/mq" 18 "github.com/machinefi/w3bstream/pkg/depends/conf/mqtt" 19 "github.com/machinefi/w3bstream/pkg/depends/conf/postgres" 20 "github.com/machinefi/w3bstream/pkg/depends/conf/redis" 21 "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/client" 22 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx" 23 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/migration" 24 "github.com/machinefi/w3bstream/pkg/depends/x/contextx" 25 "github.com/machinefi/w3bstream/pkg/depends/x/misc/retry" 26 "github.com/machinefi/w3bstream/pkg/depends/x/ptrx" 27 "github.com/machinefi/w3bstream/pkg/models" 28 "github.com/machinefi/w3bstream/pkg/modules/operator/pool" 29 "github.com/machinefi/w3bstream/pkg/modules/vm/wasmapi" 30 "github.com/machinefi/w3bstream/pkg/types" 31 "github.com/machinefi/w3bstream/pkg/types/wasm/kvdb" 32 ) 33 34 // Client for request APIs 35 func Client(transports ...client.HttpTransport) *applet_mgr.Client { 36 if _client == nil { 37 _client = &client.Client{ 38 Protocol: "http", 39 Host: "localhost", 40 Port: uint16(_server.Port), 41 Timeout: time.Hour, 42 } 43 _client.SetDefault() 44 } 45 46 _client.Transports = append(_client.Transports, transports...) 47 return applet_mgr.NewClient(_client) 48 } 49 50 // AuthClient client with jwt token 51 func AuthClient(transports ...client.HttpTransport) *applet_mgr.Client { 52 return Client(NewAuthPatchRT()) 53 } 54 55 // ClientEvent for post wasm event APIs through http transport 56 func ClientEvent(transports ...client.HttpTransport) *applet_mgr.Client { 57 if _clientEvent == nil { 58 _clientEvent = &client.Client{ 59 Protocol: "http", 60 Host: "localhost", 61 Port: uint16(_serverEvent.Port), 62 Timeout: time.Hour, 63 } 64 _clientEvent.SetDefault() 65 } 66 _clientEvent.Transports = append(_clientEvent.Transports, transports...) 67 return applet_mgr.NewClient(_clientEvent) 68 } 69 70 // Database executor for access database for testing 71 func Databases() { 72 ep := &postgres.Endpoint{ 73 Master: base.Endpoint{ 74 Scheme: "postgresql", 75 Hostname: "localhost", 76 Port: 15432, 77 Base: "w3bstream", 78 Username: "root", 79 Password: "test_passwd", 80 Param: url.Values{"sslmode": []string{"disable"}}, 81 }, 82 Retry: &retry.Retry{ 83 Repeats: 3, 84 Interval: *base.AsDuration(10 * time.Second), 85 }, 86 } 87 88 migrate := func(d *sqlx.Database) (*postgres.Endpoint, sqlx.DBExecutor, error) { 89 ep := *ep 90 ep.Database = d 91 if err := ep.Init(); err != nil { 92 return nil, nil, err 93 } 94 if err := migration.Migrate(ep.WithContext(context.Background()), nil); err != nil { 95 return nil, nil, err 96 } 97 return &ep, &ep, nil 98 } 99 100 var err error 101 if _dbMgr == nil { 102 if _, _dbMgr, err = migrate(models.DB); err != nil { 103 panic(err) 104 } 105 } 106 if _dbMonitor == nil { 107 if _, _dbMonitor, err = migrate(models.MonitorDB); err != nil { 108 panic(err) 109 } 110 } 111 _dbWasmEp = &types.WasmDBConfig{ 112 Endpoint: ep.Master, 113 MaxConnection: 2, 114 } 115 } 116 117 func Mqtt() { 118 if _broker != nil { 119 return 120 } 121 _broker = &mqtt.Broker{ 122 Server: base.Endpoint{ 123 Scheme: "mqtt", 124 Hostname: "localhost", 125 Port: 11883, 126 }, 127 Retry: retry.Retry{ 128 Repeats: 3, 129 Interval: *base.AsDuration(10 * time.Second), 130 }, 131 } 132 _broker.SetDefault() 133 if err := _broker.Init(); err != nil { 134 panic(err) 135 } 136 } 137 138 func Server() { 139 if _server == nil { 140 _server = &http.Server{ 141 Port: 18888, 142 Debug: ptrx.Ptr(true), 143 } 144 _server.SetDefault() 145 } 146 } 147 148 func ServerEvent() { 149 if _serverEvent == nil { 150 _serverEvent = &http.Server{ 151 Port: 18889, 152 Debug: ptrx.Ptr(true), 153 } 154 _serverEvent.SetDefault() 155 } 156 } 157 158 func Context() context.Context { 159 return _ctx 160 } 161 162 var ( 163 _server *http.Server 164 _serverEvent *http.Server 165 _client *client.Client 166 _clientEvent *client.Client 167 _broker *mqtt.Broker 168 _dbMgr sqlx.DBExecutor 169 _dbMonitor sqlx.DBExecutor 170 _dbWasmEp *types.WasmDBConfig 171 _injection contextx.WithContext 172 _ctx context.Context 173 ) 174 175 func init() { 176 Databases() 177 Mqtt() 178 Server() 179 ServerEvent() 180 Client() 181 ClientEvent() 182 183 _jwt := &confjwt.Jwt{ 184 Issuer: "w3bstream_test", 185 SignKey: "xxxx", 186 } 187 _uploadConfig := &types.UploadConfig{} 188 _fsop := &local.LocalFileSystem{} 189 _redis := &redis.Redis{Port: 16379} 190 191 for _, c := range []interface{}{_jwt, _uploadConfig, _fsop, _redis} { 192 if canSetDefault, ok := c.(base.DefaultSetter); ok { 193 canSetDefault.SetDefault() 194 } 195 switch v := c.(type) { 196 case base.Initializer: 197 v.Init() 198 case base.ValidatedInitializer: 199 if err := v.Init(); err != nil { 200 panic(fmt.Sprintf("%v init failed", reflect.TypeOf(v))) 201 } 202 } 203 } 204 205 //_tasks := mem_mq.New(0) 206 //_workers := mq.NewTaskWorker(_tasks, mq.WithWorkerCount(3), mq.WithChannel("apis_tests")) 207 // tb := mq.NewTaskBoard(_tasks) 208 209 _taskMgr := &confmq.Config{} 210 _taskMgr.SetDefault() 211 _ = _taskMgr.Init() 212 213 _ethClients := &types.ETHClientConfig{ 214 Endpoints: `{"4689": "https://babel-api.mainnet.iotex.io", "4690": "https://babel-api.testnet.iotex.io"}`, 215 } 216 _ethClients.Init() 217 218 _chainConf := &types.ChainConfig{ 219 Configs: `[{"chainID":4690,"name":"iotex-testnet","endpoint":"https://babel-api.testnet.iotex.io"}]`, 220 } 221 _chainConf.Init() 222 223 // TODO instance 224 _risc0Conf := &types.Risc0Config{ 225 Endpoint: "127.0.0.1:3000", 226 CreateProofPath: "/ws/api/prove_file", 227 } 228 229 redisKvDB := kvdb.NewRedisDB(_redis, "unittest") 230 operatorPool := pool.NewPool(_dbMgr) 231 232 sfIDGenerator := confid.MustNewSFIDGenerator() 233 234 wasmApiServer, err := wasmapi.NewServer(_redis, _dbMgr, redisKvDB, _chainConf, _taskMgr, operatorPool, sfIDGenerator, _risc0Conf) 235 if err != nil { 236 conflog.Std().Fatal(err) 237 } 238 239 _injection = contextx.WithContextCompose( 240 types.WithMgrDBExecutorContext(_dbMgr), 241 types.WithMonitorDBExecutorContext(_dbMonitor), 242 types.WithWasmDBConfigContext(_dbWasmEp), 243 types.WithLoggerContext(conflog.Std()), 244 types.WithMqttBrokerContext(_broker), 245 conflog.WithLoggerContext(conflog.Std()), 246 confid.WithSFIDGeneratorContext(sfIDGenerator), 247 confjwt.WithConfContext(_jwt), 248 types.WithUploadConfigContext(_uploadConfig), 249 types.WithFileSystemOpContext(_fsop), 250 types.WithRedisEndpointContext(_redis), 251 kvdb.WithRedisDBKeyContext(redisKvDB), 252 confmq.WithMqContext(_taskMgr), 253 types.WithETHClientConfigContext(_ethClients), 254 types.WithChainConfigContext(_chainConf), 255 types.WithWasmApiServerContext(wasmApiServer), 256 types.WithProxyClientContext(&client.Client{}), 257 types.WithOperatorPoolContext(operatorPool), 258 ) 259 260 _ctx = _injection(context.Background()) 261 }