ilya-kozyrev commented on a change in pull request #15654: URL: https://github.com/apache/beam/pull/15654#discussion_r723902282
########## File path: playground/backend/internal/environment/environment_service.go ########## @@ -0,0 +1,98 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package environment + +import ( + pb "beam.apache.org/playground/backend/internal/api" + "google.golang.org/grpc/grpclog" + "os" + "strconv" +) + +const ( + serverIpKey = "SERVER_IP" + serverPortKey = "SERVER_PORT" + beamSdkKey = "BEAM_SDK" + defaultIp = "localhost" + defaultPort = 8080 + defaultSdk = pb.Sdk_SDK_JAVA +) + +// Service operates with environment structures: ServerEnvs, LogWriters, BeamEnvs +type Service struct { + ServerEnvs ServerEnvs + LogWriters LogWriters + BeamSdkEnvs BeamEnvs +} + +// NewService is a constructor for Service. +// Default values: +// LogWriters: by default using os.Stdout +// ServerEnvs: by default using defaultIp and defaultPort from constants +// BeamEnvs: by default using pb.Sdk_SDK_JAVA +func NewService(writers *LogWriters) *Service { + if writers == nil { + writers = NewLogWriters(os.Stdout, os.Stdout, os.Stderr) + } + svc := Service{LogWriters: *writers} + svc.ServerEnvs = getServerEnvsFromOsEnvs() + svc.BeamSdkEnvs = getSdkEnvsFromOsEnvs() + + return &svc +} + +// getServerEnvsFromOsEnvs lookups in os environment variables and takes value for ip and port. If not exists - using default +func getServerEnvsFromOsEnvs() ServerEnvs { + ip := defaultIp + port := defaultPort + if value, present := os.LookupEnv(serverIpKey); present { + ip = value + } + + if value, present := os.LookupEnv(serverPortKey); present { + if converted, err := strconv.Atoi(value); err == nil { + port = converted + } else { + grpclog.Errorf("couldn't convert provided port. Using default %s\n", defaultPort) + } + } + + return *NewServerEnvs(ip, port) +} + +// getServerEnvsFromOsEnvs lookups in os environment variables and takes value for Apache Beam SDK. If not exists - using default +func getSdkEnvsFromOsEnvs() BeamEnvs { Review comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
