github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/deck/templates.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"html/template"
    21  	"net/http"
    22  	"path"
    23  
    24  	"github.com/gorilla/csrf"
    25  	"github.com/sirupsen/logrus"
    26  	"sigs.k8s.io/prow/pkg/config"
    27  	"sigs.k8s.io/prow/pkg/version"
    28  )
    29  
    30  // This stuff is used in the templates.
    31  type baseTemplateSettings struct {
    32  	MobileFriendly bool
    33  	DarkMode       bool
    34  	PageName       string
    35  	Arguments      interface{}
    36  }
    37  
    38  func makeBaseTemplateSettings(mobileFriendly bool, darkMode bool, pageName string, arguments interface{}) baseTemplateSettings {
    39  	return baseTemplateSettings{mobileFriendly, darkMode, pageName, arguments}
    40  }
    41  
    42  func getConcreteBrandingFunction(cfg config.Getter) func() config.Branding {
    43  	return func() config.Branding {
    44  		if branding := cfg().Deck.Branding; branding != nil {
    45  			return *branding
    46  		}
    47  		return config.Branding{}
    48  	}
    49  }
    50  
    51  type baseTemplateSections struct {
    52  	PR   bool
    53  	Tide bool
    54  }
    55  
    56  func getConcreteSectionFunction(o options) func() baseTemplateSections {
    57  	return func() baseTemplateSections {
    58  		return baseTemplateSections{
    59  			PR:   o.oauthURL != "" || o.pregeneratedData != "",
    60  			Tide: o.tideURL != "" || o.pregeneratedData != "",
    61  		}
    62  	}
    63  }
    64  
    65  func prepareBaseTemplate(o options, cfg config.Getter, csrfToken string, t *template.Template) (*template.Template, error) {
    66  	return t.Funcs(map[string]interface{}{
    67  		"settings":         makeBaseTemplateSettings,
    68  		"branding":         getConcreteBrandingFunction(cfg),
    69  		"sections":         getConcreteSectionFunction(o),
    70  		"mobileFriendly":   func() bool { return true },
    71  		"mobileUnfriendly": func() bool { return false },
    72  		"darkMode":         func() bool { return true },
    73  		"lightMode":        func() bool { return false },
    74  		"deckVersion":      func() string { return version.Version },
    75  		"googleAnalytics":  func() string { return cfg().Deck.GoogleAnalytics },
    76  		"csrfToken":        func() string { return csrfToken },
    77  	}).ParseFiles(path.Join(o.templateFilesLocation, "base.html"))
    78  }
    79  
    80  func handleSimpleTemplate(o options, cfg config.Getter, templateName string, param interface{}) http.HandlerFunc {
    81  	return func(w http.ResponseWriter, r *http.Request) {
    82  		t := template.New(templateName) // the name matters, and must match the filename.
    83  		if _, err := prepareBaseTemplate(o, cfg, csrf.Token(r), t); err != nil {
    84  			logrus.WithError(err).Error("error preparing base template")
    85  			http.Error(w, "error preparing base template", http.StatusInternalServerError)
    86  			return
    87  		}
    88  		w.Header().Add("Content-Type", "text/html; charset=utf-8")
    89  		if _, err := t.ParseFiles(path.Join(o.templateFilesLocation, templateName)); err != nil {
    90  			logrus.WithError(err).Error("error parsing template " + templateName)
    91  			http.Error(w, "error parsing template", http.StatusInternalServerError)
    92  			return
    93  		}
    94  
    95  		if err := t.Execute(w, param); err != nil {
    96  			logrus.WithError(err).Error("error executing template " + templateName)
    97  			http.Error(w, "error executing template", http.StatusInternalServerError)
    98  			return
    99  		}
   100  	}
   101  }