func MainHandler(w http.ResponseWriter, r *http.Request, c config.Configuration, auditData validator.AuditData, basePath string) {
	jsonData, err := json.Marshal(auditData.GetSummary())
	if err != nil {
		http.Error(w, "Error serializing audit data", 500)
		return
	}
	filteredAuditData := auditData
	namespaces := r.URL.Query()["ns"]
	if len(namespaces) > 0 {
		stripUnselectedNamespaces(&filteredAuditData, namespaces)
	}
	data := templateData{
		BasePath:          basePath,
		AuditData:         auditData,
		FilteredAuditData: filteredAuditData,
		JSON:              template.JS(jsonData),
		Config:            c,
	}
	tmpl, err := GetBaseTemplate("main")
	if err != nil {
		logrus.Printf("Error getting template data %v", err)
		http.Error(w, "Error getting template data", 500)
		return
	}
	writeTemplate(tmpl, &data, w)
}
MainHandler gets template data and renders the dashboard with it.
package dashboard
import (
	"bytes"
	"encoding/json"
	"html/template"
	"net/http"
	"net/url"
	"path"
	"strings"
	"github.com/fairwindsops/polaris/pkg/config"
	"github.com/fairwindsops/polaris/pkg/kube"
	"github.com/fairwindsops/polaris/pkg/validator"
	packr "github.com/gobuffalo/packr/v2"
	"github.com/gorilla/mux"
	"github.com/sirupsen/logrus"
)
const (
	MainTemplateName = "main.gohtml"
	HeadTemplateName = "head.gohtml"
	NavbarTemplateName = "navbar.gohtml"
	PreambleTemplateName = "preamble.gohtml"
	DashboardTemplateName = "dashboard.gohtml"
	FooterTemplateName = "footer.gohtml"
)
var (
	templateBox = (*packr.Box)(nil)
	assetBox    = (*packr.Box)(nil)
	markdownBox = (*packr.Box)(nil)
)
func GetAssetBox() *packr.Box {
	if assetBox == (*packr.Box)(nil) {
		assetBox = packr.New("Assets", "assets")
	}
	return assetBox
}
func GetTemplateBox() *packr.Box {
	if templateBox == (*packr.Box)(nil) {
		templateBox = packr.New("Templates", "templates")
	}
	return templateBox
}
type templateData struct {
	BasePath          string
	Config            config.Configuration
	AuditData         validator.AuditData
	FilteredAuditData validator.AuditData
	JSON              template.JS
}
func GetBaseTemplate(name string) (*template.Template, error) {
	tmpl := template.New(name).Funcs(template.FuncMap{
		"getWarningWidth": getWarningWidth,
		"getSuccessWidth": getSuccessWidth,
		"getWeatherIcon":  getWeatherIcon,
		"getWeatherText":  getWeatherText,
		"getGrade":        getGrade,
		"getIcon":         getIcon,
		"getResultClass":  getResultClass,
		"getCategoryLink": getCategoryLink,
		"getCategoryInfo": getCategoryInfo,
	})
	templateFileNames := []string{
		DashboardTemplateName,
		HeadTemplateName,
		NavbarTemplateName,
		PreambleTemplateName,
		FooterTemplateName,
		MainTemplateName,
	}
	return parseTemplateFiles(tmpl, templateFileNames)
}
func parseTemplateFiles(tmpl *template.Template, templateFileNames []string) (*template.Template, error) {
	templateBox := GetTemplateBox()
	for _, fname := range templateFileNames {
		templateFile, err := templateBox.Find(fname)
		if err != nil {
			return nil, err
		}
		tmpl, err = tmpl.Parse(string(templateFile))
		if err != nil {
			return nil, err
		}
	}
	return tmpl, nil
}
func writeTemplate(tmpl *template.Template, data *templateData, w http.ResponseWriter) {
	buf := &bytes.Buffer{}
	err := tmpl.Execute(buf, data)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	buf.WriteTo(w)
}
func getConfigForQuery(base config.Configuration, query url.Values) config.Configuration {
	c := base
	switch query.Get("disallowExemptions") {
	case "true":
		c.DisallowExemptions = true
		c.DisallowConfigExemptions = true
		c.DisallowAnnotationExemptions = true
	default:
		c.DisallowExemptions = false
		c.DisallowConfigExemptions = false
		c.DisallowAnnotationExemptions = false
	}
	return c
}
func stripUnselectedNamespaces(data *validator.AuditData, selectedNamespaces []string) {
	newResults := []validator.Result{}
	for _, res := range data.Results {
		if stringInSlice(res.Namespace, selectedNamespaces) {
			newResults = append(newResults, res)
		}
	}
	data.Results = newResults
}
func GetRouter(c config.Configuration, auditPath string, port int, basePath string, auditData *validator.AuditData) *mux.Router {
	router := mux.NewRouter().PathPrefix(basePath).Subrouter()
	fileServer := http.FileServer(GetAssetBox())
	router.PathPrefix("/static/").Handler(http.StripPrefix(path.Join(basePath, "/static/"), fileServer))
	router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
	})
	router.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
		favicon, err := GetAssetBox().Find("favicon-32x32.png")
		if err != nil {
			logrus.Errorf("Error getting favicon: %v", err)
			http.Error(w, "Error getting favicon", http.StatusInternalServerError)
			return
		}
		w.Write(favicon)
	})
	router.HandleFunc("/results.json", func(w http.ResponseWriter, r *http.Request) {
		adjustedConf := getConfigForQuery(c, r.URL.Query())
		if auditData == nil {
			k, err := kube.CreateResourceProvider(r.Context(), auditPath, "", c)
			if err != nil {
				logrus.Errorf("Error fetching Kubernetes resources %v", err)
				http.Error(w, "Error fetching Kubernetes resources", http.StatusInternalServerError)
				return
			}
			var auditDataObj validator.AuditData
			auditDataObj, err = validator.RunAudit(adjustedConf, k)
			if err != nil {
				http.Error(w, "Error Fetching Deployments", http.StatusInternalServerError)
				return
			}
			auditData = &auditDataObj
		}
		JSONHandler(w, r, auditData)
	})
	router.HandleFunc("/details/{category}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		category := vars["category"]
		category = strings.Replace(category, ".md", "", -1)
	})
	router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/" && r.URL.Path != basePath {
			http.NotFound(w, r)
			return
		}
		adjustedConf := getConfigForQuery(c, r.URL.Query())
		if auditData == nil {
			k, err := kube.CreateResourceProvider(r.Context(), auditPath, "", c)
			if err != nil {
				logrus.Errorf("Error fetching Kubernetes resources %v", err)
				http.Error(w, "Error fetching Kubernetes resources", http.StatusInternalServerError)
				return
			}
			var auditData validator.AuditData
			auditData, err = validator.RunAudit(adjustedConf, k)
			if err != nil {
				logrus.Errorf("Error getting audit data: %v", err)
				http.Error(w, "Error running audit", 500)
				return
			}
			MainHandler(w, r, adjustedConf, auditData, basePath)
		} else {
			MainHandler(w, r, adjustedConf, *auditData, basePath)
		}
	})
	return router
}