|
| 1 | +// ------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation and Dapr Contributors. |
| 3 | +// Licensed under the MIT License. |
| 4 | +// ------------------------------------------------------------ |
| 5 | + |
| 6 | +package embedded |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + |
| 12 | + "github.com/dapr/dapr/pkg/acl" |
| 13 | + global_config "github.com/dapr/dapr/pkg/config" |
| 14 | + env "github.com/dapr/dapr/pkg/config/env" |
| 15 | + "github.com/dapr/dapr/pkg/cors" |
| 16 | + "github.com/dapr/dapr/pkg/modes" |
| 17 | + "github.com/dapr/dapr/pkg/operator/client" |
| 18 | + "github.com/dapr/dapr/pkg/runtime" |
| 19 | + "github.com/dapr/dapr/pkg/runtime/security" |
| 20 | + "github.com/dapr/kit/logger" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + placementAddresses = "127.0.0.1" |
| 25 | + controlPlaneAddress = "" |
| 26 | + allowedOrigins = cors.DefaultAllowedOrigins |
| 27 | + mode = modes.StandaloneMode |
| 28 | + config = "config.yaml" |
| 29 | + componentsPath = "./components" |
| 30 | + profilePort = runtime.DefaultProfilePort |
| 31 | + enableProfiling = true |
| 32 | + maxConcurrency = -1 |
| 33 | + enableMTLS = false |
| 34 | + sentryAddress = "" |
| 35 | + appSSL = false |
| 36 | + maxRequestBodySize = 4 |
| 37 | + |
| 38 | + daprHTTPPort = runtime.DefaultDaprHTTPPort |
| 39 | + daprAPIGRPCPort = runtime.DefaultDaprAPIGRPCPort |
| 40 | + daprInternalGRPC = runtime.DefaultDaprAPIGRPCPort + 1 |
| 41 | + appPort = 8000 |
| 42 | +) |
| 43 | + |
| 44 | +var log = logger.NewLogger("dapr.runtime") |
| 45 | + |
| 46 | +type Option func(config *runtime.Config) |
| 47 | + |
| 48 | +func WithAppProtocol(protocol runtime.Protocol, port int) Option { |
| 49 | + return func(config *runtime.Config) { |
| 50 | + config.ApplicationProtocol = protocol |
| 51 | + config.ApplicationPort = port |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func WithDaprHTTPPort(port int) Option { |
| 56 | + return func(config *runtime.Config) { |
| 57 | + config.HTTPPort = port |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func WithDaprGRPCPort(port int) Option { |
| 62 | + return func(config *runtime.Config) { |
| 63 | + config.APIGRPCPort = port |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func WithDaprInternalGRPCPort(port int) Option { |
| 68 | + return func(config *runtime.Config) { |
| 69 | + config.InternalGRPCPort = port |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func WithListenAddresses(addresses []string) Option { |
| 74 | + return func(config *runtime.Config) { |
| 75 | + config.APIListenAddresses = addresses |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func NewRuntime(appID string, opts ...Option) (*runtime.DaprRuntime, error) { |
| 80 | + var err error |
| 81 | + |
| 82 | + runtimeConfig := runtime.NewRuntimeConfig( |
| 83 | + appID, []string{}, controlPlaneAddress, |
| 84 | + allowedOrigins, config, componentsPath, string(runtime.HTTPProtocol), string(mode), |
| 85 | + daprHTTPPort, daprInternalGRPC, daprAPIGRPCPort, []string{"127.0.0.1"}, nil, appPort, profilePort, |
| 86 | + enableProfiling, maxConcurrency, enableMTLS, sentryAddress, appSSL, maxRequestBodySize, "", |
| 87 | + runtime.DefaultReadBufferSize, false) |
| 88 | + |
| 89 | + for _, opt := range opts { |
| 90 | + opt(runtimeConfig) |
| 91 | + } |
| 92 | + |
| 93 | + variables := map[string]string{ |
| 94 | + env.AppID: runtimeConfig.ID, |
| 95 | + env.AppPort: fmt.Sprintf("%d", runtimeConfig.ApplicationPort), |
| 96 | + env.HostAddress: "127.0.0.1", |
| 97 | + env.DaprPort: fmt.Sprintf("%d", runtimeConfig.InternalGRPCPort), |
| 98 | + env.DaprGRPCPort: fmt.Sprintf("%d", runtimeConfig.APIGRPCPort), |
| 99 | + env.DaprHTTPPort: fmt.Sprintf("%d", runtimeConfig.HTTPPort), |
| 100 | + env.DaprProfilePort: fmt.Sprintf("%d", runtimeConfig.ProfilePort), |
| 101 | + } |
| 102 | + |
| 103 | + for key, value := range variables { |
| 104 | + err := os.Setenv(key, value) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + var globalConfig *global_config.Configuration |
| 111 | + var configErr error |
| 112 | + |
| 113 | + if enableMTLS { |
| 114 | + if runtimeConfig.CertChain, err = security.GetCertChain(); err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + var accessControlList *global_config.AccessControlList |
| 120 | + var namespace string |
| 121 | + |
| 122 | + if config != "" { |
| 123 | + switch modes.DaprMode(mode) { |
| 124 | + case modes.KubernetesMode: |
| 125 | + client, conn, clientErr := client.GetOperatorClient(controlPlaneAddress, security.TLSServerName, runtimeConfig.CertChain) |
| 126 | + if clientErr != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + defer conn.Close() |
| 130 | + namespace = os.Getenv("NAMESPACE") |
| 131 | + globalConfig, configErr = global_config.LoadKubernetesConfiguration(config, namespace, client) |
| 132 | + case modes.StandaloneMode: |
| 133 | + globalConfig, _, configErr = global_config.LoadStandaloneConfiguration(config) |
| 134 | + } |
| 135 | + |
| 136 | + if configErr != nil { |
| 137 | + log.Debugf("Config error: %v", configErr) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if configErr != nil { |
| 142 | + return nil, fmt.Errorf("error loading configuration: %w", configErr) |
| 143 | + } |
| 144 | + if globalConfig == nil { |
| 145 | + log.Info("loading default configuration") |
| 146 | + globalConfig = global_config.LoadDefaultConfiguration() |
| 147 | + } |
| 148 | + |
| 149 | + accessControlList, err = acl.ParseAccessControlSpec(globalConfig.Spec.AccessControlSpec, string(runtimeConfig.ApplicationProtocol)) |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + |
| 154 | + return runtime.NewDaprRuntime(runtimeConfig, globalConfig, accessControlList), nil |
| 155 | +} |
0 commit comments