go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/frontend/configs_js_file.go (about)

     1  // Copyright 2023 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 frontend
    16  
    17  import (
    18  	"text/template"
    19  
    20  	"google.golang.org/protobuf/encoding/protojson"
    21  
    22  	"go.chromium.org/luci/common/logging"
    23  	"go.chromium.org/luci/milo/internal/config"
    24  	configpb "go.chromium.org/luci/milo/proto/config"
    25  	"go.chromium.org/luci/server/router"
    26  )
    27  
    28  // configsJSHandler serves /configs.js used by the browser-side.
    29  func (s *HTTPService) configsJSHandler(c *router.Context) error {
    30  	tmpl, err := template.ParseFiles("frontend/templates/configs.template.js")
    31  	if err != nil {
    32  		logging.Errorf(c.Request.Context(), "Failed to load configs.template.js: %s", err)
    33  		return err
    34  	}
    35  
    36  	settings := config.GetSettings(c.Request.Context())
    37  	// Reassign to make exposing props explicit.
    38  	settings = &configpb.Settings{
    39  		Swarming:       settings.Swarming,
    40  		Buildbucket:    settings.Buildbucket,
    41  		Resultdb:       settings.Resultdb,
    42  		LuciAnalysis:   settings.LuciAnalysis,
    43  		LuciBisection:  settings.LuciBisection,
    44  		SheriffOMatic:  settings.SheriffOMatic,
    45  		LuciTreeStatus: settings.LuciTreeStatus,
    46  		LuciNotify:     settings.LuciNotify,
    47  	}
    48  
    49  	header := c.Writer.Header()
    50  	header.Set("content-type", "application/javascript")
    51  
    52  	// We don't need to cache the configs file because it is fetched and re-served
    53  	// by the service worker.
    54  	header.Set("cache-control", "no-cache")
    55  	err = tmpl.Execute(c.Writer, map[string]any{
    56  		"Version":      s.Server.Options.ImageVersion(),
    57  		"SettingsJSON": protojson.Format(settings),
    58  	})
    59  
    60  	if err != nil {
    61  		logging.Errorf(c.Request.Context(), "Failed to execute configs.template.js: %s", err)
    62  		return err
    63  	}
    64  
    65  	return nil
    66  }