go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/common/env.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import "go.chromium.org/luci/server"
    18  
    19  // Env describes where CV runs at.
    20  type Env struct {
    21  	// LogicalHostname is CV hostname referred to in configs.
    22  	//
    23  	// On GAE, this is something like "luci-change-verifier-dev.appspot.com"
    24  	// and it is part of the HTTPAddressBase.
    25  	//
    26  	// Under local development, this is usually set to GAE-looking hostname, while
    27  	// keeping HTTPAddressBase a real localhost URL.
    28  	LogicalHostname string
    29  
    30  	// HTTPAddressBase can be used to generate URLs to this CV service.
    31  	//
    32  	// Doesn't have a trailing slash.
    33  	//
    34  	// For example,
    35  	//   * "https://luci-change-verifier-dev.appspot.com"
    36  	//   * "http://localhost:8080"
    37  	HTTPAddressBase string
    38  
    39  	// IsGAEDev is true if this is a -dev GAE environment.
    40  	//
    41  	// Deprecated. Do not use in new code. It should only be used during migration
    42  	// from CQDaemon which doesn't have equivalent -dev environment.
    43  	IsGAEDev bool
    44  
    45  	// GAEInfo is populated if LUCI CV runs on GAE.
    46  	GAEInfo struct {
    47  		// CloudProject is the name of the Google Cloud Project LUCI CV runs in.
    48  		CloudProject string
    49  		// ServiceName is the name of the micro-service in the GAE app.
    50  		ServiceName string
    51  		// InstanceID is the ID of the instance that runs LUCI CV.
    52  		InstanceID string
    53  	}
    54  }
    55  
    56  // MakeEnv creates a new `Env` from server options.
    57  func MakeEnv(opts server.Options) *Env {
    58  	env := &Env{
    59  		LogicalHostname: opts.CloudProject + ".appspot.com",
    60  		IsGAEDev:        opts.CloudProject == "luci-change-verifier-dev",
    61  		GAEInfo: struct {
    62  			CloudProject string
    63  			ServiceName  string
    64  			InstanceID   string
    65  		}{
    66  			CloudProject: opts.CloudProject,
    67  			// TODO(yiwzhang): have a more reliable way to get the GAE service name.
    68  			ServiceName: opts.TsMonJobName,
    69  			InstanceID:  opts.Hostname,
    70  		},
    71  	}
    72  	env.HTTPAddressBase = "https://" + env.LogicalHostname
    73  	if !opts.Prod {
    74  		// Local development.
    75  		env.HTTPAddressBase = "http://" + opts.HTTPAddr
    76  	}
    77  	return env
    78  }