github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/global/config.go (about)

     1  package global
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"time"
     7  
     8  	_ "github.com/machinefi/w3bstream/cmd/srv-applet-mgr/types"
     9  	"github.com/machinefi/w3bstream/pkg/depends/base/consts"
    10  	confapp "github.com/machinefi/w3bstream/pkg/depends/conf/app"
    11  	"github.com/machinefi/w3bstream/pkg/depends/conf/filesystem"
    12  	"github.com/machinefi/w3bstream/pkg/depends/conf/filesystem/amazonS3"
    13  	"github.com/machinefi/w3bstream/pkg/depends/conf/filesystem/local"
    14  	confhttp "github.com/machinefi/w3bstream/pkg/depends/conf/http"
    15  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
    16  	confjwt "github.com/machinefi/w3bstream/pkg/depends/conf/jwt"
    17  	conflog "github.com/machinefi/w3bstream/pkg/depends/conf/log"
    18  	conflogger "github.com/machinefi/w3bstream/pkg/depends/conf/logger"
    19  	confmq "github.com/machinefi/w3bstream/pkg/depends/conf/mq"
    20  	confmqtt "github.com/machinefi/w3bstream/pkg/depends/conf/mqtt"
    21  	confpostgres "github.com/machinefi/w3bstream/pkg/depends/conf/postgres"
    22  	confrate "github.com/machinefi/w3bstream/pkg/depends/conf/rate_limit"
    23  	confredis "github.com/machinefi/w3bstream/pkg/depends/conf/redis"
    24  	conftracer "github.com/machinefi/w3bstream/pkg/depends/conf/tracer"
    25  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/client"
    26  	"github.com/machinefi/w3bstream/pkg/depends/kit/kit"
    27  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/migration"
    28  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    29  	"github.com/machinefi/w3bstream/pkg/enums"
    30  	"github.com/machinefi/w3bstream/pkg/models"
    31  	"github.com/machinefi/w3bstream/pkg/modules/operator/pool"
    32  	"github.com/machinefi/w3bstream/pkg/modules/vm/wasmapi"
    33  	"github.com/machinefi/w3bstream/pkg/types"
    34  	"github.com/machinefi/w3bstream/pkg/types/wasm/kvdb"
    35  )
    36  
    37  var (
    38  	App         *confapp.Ctx
    39  	WithContext contextx.WithContext
    40  	Context     context.Context
    41  
    42  	proxy *client.Client // proxy client for forward mqtt event
    43  
    44  	db        = &confpostgres.Endpoint{Database: models.DB}
    45  	monitordb = &confpostgres.Endpoint{Database: models.MonitorDB}
    46  
    47  	ServerMgr   = &confhttp.Server{}
    48  	ServerEvent = &confhttp.Server{} // serverEvent support event http transport
    49  	TaskMgr     = &confmq.Config{}
    50  
    51  	fs  filesystem.FileSystemOp
    52  	std = conflog.Std().(conflog.LevelSetter).SetLevel(conflog.InfoLevel)
    53  )
    54  
    55  func init() {
    56  	// TODO config struct should be defined outside this method and impl it's Init() interface{}
    57  	// TODO split this init too long
    58  	config := &struct {
    59  		Postgres           *confpostgres.Endpoint
    60  		MonitorDB          *confpostgres.Endpoint
    61  		MqttBroker         *confmqtt.Broker
    62  		Redis              *confredis.Redis
    63  		NewLogger          *conflogger.Config
    64  		Tracer             *conftracer.Config
    65  		Server             *confhttp.Server
    66  		Jwt                *confjwt.Jwt
    67  		Logger             *conflog.Log
    68  		UploadConf         *types.UploadConfig
    69  		EthClient          *types.ETHClientConfig
    70  		ChainConfig        *types.ChainConfig
    71  		WhiteList          *types.EthAddressWhiteList
    72  		ServerEvent        *confhttp.Server
    73  		FileSystem         *types.FileSystem
    74  		AmazonS3           *amazonS3.AmazonS3
    75  		LocalFS            *local.LocalFileSystem
    76  		WasmDBConfig       *types.WasmDBConfig
    77  		RateLimit          *confrate.RateLimit
    78  		MetricsCenter      *types.MetricsCenterConfig
    79  		RobotNotifier      *types.RobotNotifierConfig
    80  		Risc0Config        *types.Risc0Config
    81  		Mq                 *confmq.Config
    82  		MaxWasmFuel        uint64
    83  		ProjectFilter      *types.ProjectFilter
    84  		EnableTrafficLimit bool
    85  	}{
    86  		Postgres:           db,
    87  		MonitorDB:          monitordb,
    88  		MqttBroker:         &confmqtt.Broker{},
    89  		Redis:              &confredis.Redis{},
    90  		NewLogger:          &conflogger.Config{},
    91  		Tracer:             &conftracer.Config{},
    92  		Server:             ServerMgr,
    93  		Jwt:                &confjwt.Jwt{},
    94  		Logger:             &conflog.Log{},
    95  		UploadConf:         &types.UploadConfig{},
    96  		EthClient:          &types.ETHClientConfig{},
    97  		ChainConfig:        &types.ChainConfig{},
    98  		WhiteList:          &types.EthAddressWhiteList{},
    99  		ServerEvent:        ServerEvent,
   100  		FileSystem:         &types.FileSystem{},
   101  		AmazonS3:           &amazonS3.AmazonS3{},
   102  		LocalFS:            &local.LocalFileSystem{},
   103  		WasmDBConfig:       &types.WasmDBConfig{},
   104  		RateLimit:          &confrate.RateLimit{},
   105  		MetricsCenter:      &types.MetricsCenterConfig{},
   106  		RobotNotifier:      &types.RobotNotifierConfig{},
   107  		Risc0Config:        &types.Risc0Config{},
   108  		Mq:                 TaskMgr,
   109  		MaxWasmFuel:        1024 * 1024 * 1024,
   110  		ProjectFilter:      &types.ProjectFilter{},
   111  		EnableTrafficLimit: false,
   112  	}
   113  
   114  	name := os.Getenv(consts.EnvProjectName)
   115  	if name == "" {
   116  		name = "srv-applet-mgr"
   117  	}
   118  	_ = os.Setenv(consts.EnvProjectName, name)
   119  
   120  	group := os.Getenv(consts.EnvResourceGroup)
   121  	if group == "" {
   122  		group = "srv-applet-mgr"
   123  	}
   124  	_ = os.Setenv(consts.EnvResourceGroup, group)
   125  
   126  	App = confapp.New(
   127  		confapp.WithName(name),
   128  		confapp.WithRoot(".."),
   129  		confapp.WithLogger(conflogger.Std()),
   130  	)
   131  	App.Conf(config /*, worker*/)
   132  
   133  	if config.FileSystem.Type == enums.FILE_SYSTEM_MODE__S3 &&
   134  		!config.AmazonS3.IsZero() {
   135  		fs = config.AmazonS3
   136  	} else {
   137  		fs = config.LocalFS
   138  	}
   139  
   140  	if config.RobotNotifier.IsZero() {
   141  		config.RobotNotifier = nil
   142  	}
   143  
   144  	confhttp.RegisterCheckerBy(config)
   145  
   146  	proxy = &client.Client{Port: uint16(ServerEvent.Port), Timeout: 10 * time.Second}
   147  	proxy.SetDefault()
   148  
   149  	redisKvDB := kvdb.NewRedisDB(config.Redis, "")
   150  	operatorPool := pool.NewPool(config.Postgres)
   151  
   152  	sfIDGenerator := confid.MustNewSFIDGenerator()
   153  
   154  	wasmApiServer, err := wasmapi.NewServer(config.Redis, config.Postgres, redisKvDB, config.ChainConfig,
   155  		config.Mq, operatorPool, sfIDGenerator, config.Risc0Config)
   156  	if err != nil {
   157  		std.Fatal(err)
   158  	}
   159  
   160  	WithContext = contextx.WithContextCompose(
   161  		types.WithMgrDBExecutorContext(config.Postgres),
   162  		types.WithMonitorDBExecutorContext(config.MonitorDB),
   163  		types.WithRedisEndpointContext(config.Redis),
   164  		types.WithLoggerContext(std),
   165  		conflog.WithLoggerContext(std),
   166  		types.WithUploadConfigContext(config.UploadConf),
   167  		types.WithMqttBrokerContext(config.MqttBroker),
   168  		confid.WithSFIDGeneratorContext(sfIDGenerator),
   169  		confjwt.WithConfContext(config.Jwt),
   170  		confmq.WithMqContext(config.Mq),
   171  		types.WithETHClientConfigContext(config.EthClient),
   172  		types.WithChainConfigContext(config.ChainConfig),
   173  		types.WithEthAddressWhiteListContext(config.WhiteList),
   174  		types.WithFileSystemOpContext(fs),
   175  		types.WithProxyClientContext(proxy),
   176  		types.WithWasmDBConfigContext(config.WasmDBConfig),
   177  		confrate.WithRateLimitKeyContext(config.RateLimit),
   178  		kvdb.WithRedisDBKeyContext(redisKvDB),
   179  		types.WithMetricsCenterConfigContext(config.MetricsCenter),
   180  		types.WithRobotNotifierConfigContext(config.RobotNotifier),
   181  		types.WithWasmApiServerContext(wasmApiServer),
   182  		types.WithOperatorPoolContext(operatorPool),
   183  		types.WithMaxWasmConsumeFuelContext(config.MaxWasmFuel),
   184  		types.WithProjectFilterContext(config.ProjectFilter),
   185  		types.WithEnableTrafficLimitContext(config.EnableTrafficLimit),
   186  	)
   187  	Context = WithContext(context.Background())
   188  }
   189  
   190  func Server() kit.Transport {
   191  	return ServerMgr.WithContextInjector(WithContext).WithName("srv-applet-mgr")
   192  }
   193  
   194  func TaskServer() kit.Transport {
   195  	return TaskMgr.WithContextInjector(WithContext)
   196  }
   197  
   198  func EventServer() kit.Transport {
   199  	return ServerEvent.WithContextInjector(WithContext).WithName("srv-event")
   200  }
   201  
   202  func DebugServer() kit.Transport {
   203  	srv := &confhttp.Server{
   204  		Protocol: "http",
   205  		Addr:     "",
   206  		Port:     10001,
   207  		Spec:     "./openapi.json",
   208  	}
   209  	return srv.WithContextInjector(WithContext).WithName("srv-debug")
   210  }
   211  
   212  func Migrate() {
   213  	ctx, l := conflogger.NewSpanContext(context.Background(), "global.Migrate")
   214  	defer l.End()
   215  
   216  	output, err := os.OpenFile("migrate.sql", os.O_CREATE|os.O_RDWR, 0666)
   217  	if err != nil {
   218  		panic(err)
   219  	}
   220  	defer output.Close()
   221  
   222  	if err := migration.Migrate(db.WithContext(ctx), output); err != nil {
   223  		l.Error(err)
   224  		panic(err)
   225  	}
   226  
   227  	if err := migration.Migrate(monitordb.WithContext(ctx), output); err != nil {
   228  		l.Error(err)
   229  		panic(err)
   230  	}
   231  }