// NewTemplatePlugin creates a new TemplatePlugin. func NewTemplatePlugin(cfg TemplatePluginConfig) (*TemplatePlugin, error) { templateBaseName := filepath.Base(cfg.TemplatePath) masterTemplate, err := template.New("config").ParseFiles(cfg.TemplatePath) if err != nil { return nil, err } templates := map[string]*template.Template{} for _, template := range masterTemplate.Templates() { if template.Name() == templateBaseName { continue } templates[template.Name()] = template } peerKey := "" if cfg.PeerService != nil { peerKey = peerEndpointsKey(*cfg.PeerService) } templateRouterCfg := templateRouterCfg{ dir: cfg.WorkingDir, templates: templates, reloadScriptPath: cfg.ReloadScriptPath, defaultCertificate: cfg.DefaultCertificate, statsUser: cfg.StatsUsername, statsPassword: cfg.StatsPassword, statsPort: cfg.StatsPort, peerEndpointsKey: peerKey, } router, err := newTemplateRouter(templateRouterCfg) return newDefaultTemplatePlugin(router), err }
// NewTemplatePlugin creates a new TemplatePlugin. func NewTemplatePlugin(cfg TemplatePluginConfig) (*TemplatePlugin, error) { templateBaseName := filepath.Base(cfg.TemplatePath) masterTemplate := template.Must(template.New("config").ParseFiles(cfg.TemplatePath)) templates := map[string]*template.Template{} for _, template := range masterTemplate.Templates() { if template.Name() == templateBaseName { continue } templates[template.Name()] = template } templateRouterCfg := templateRouterCfg{ templates: templates, reloadScriptPath: cfg.ReloadScriptPath, defaultCertificate: cfg.DefaultCertificate, statsUser: cfg.StatsUsername, statsPassword: cfg.StatsPassword, statsPort: cfg.StatsPort, peerEndpointsKey: peerEndpointsKey(cfg.PeerService), } router, err := newTemplateRouter(templateRouterCfg) return &TemplatePlugin{router}, err }
func (t *Repository) addFile(name, data string, allowOverride bool) error { fileName := name name = swag.ToJSONName(strings.TrimSuffix(name, ".gotmpl")) templ, err := template.New(name).Funcs(t.funcs).Parse(data) if err != nil { return err } // check if any protected templates are defined if !allowOverride { for _, template := range templ.Templates() { if protectedTemplates[template.Name()] { return fmt.Errorf("Cannot overwrite protected template %s", template.Name()) } } } // Add each defined tempalte into the cache for _, template := range templ.Templates() { t.files[template.Name()] = fileName t.templates[template.Name()] = template.Lookup(template.Name()) } return nil }
// NewTemplatePlugin creates a new TemplatePlugin. func NewTemplatePlugin(cfg TemplatePluginConfig, lookupSvc ServiceLookup) (*TemplatePlugin, error) { templateBaseName := filepath.Base(cfg.TemplatePath) globalFuncs := template.FuncMap{ "endpointsForAlias": endpointsForAlias, //returns the list of valid endpoints "env": env, //tries to get an environment variable if it can't return a default "matchPattern": matchPattern, //anchors provided regular expression and evaluates against given string "isInteger": isInteger, //determines if a given variable is an integer "matchValues": matchValues, //compares a given string to a list of allowed strings "genSubdomainWildcardRegexp": genSubdomainWildcardRegexp, //generates a regular expression matching the subdomain for hosts (and paths) with a wildcard policy "genCertificateHostName": genCertificateHostName, //generates host name to use for serving/matching certificates } masterTemplate, err := template.New("config").Funcs(globalFuncs).ParseFiles(cfg.TemplatePath) if err != nil { return nil, err } templates := map[string]*template.Template{} for _, template := range masterTemplate.Templates() { if template.Name() == templateBaseName { continue } templates[template.Name()] = template } peerKey := "" if cfg.PeerService != nil { peerKey = peerEndpointsKey(*cfg.PeerService) } templateRouterCfg := templateRouterCfg{ dir: cfg.WorkingDir, templates: templates, reloadScriptPath: cfg.ReloadScriptPath, reloadInterval: cfg.ReloadInterval, defaultCertificate: cfg.DefaultCertificate, defaultCertificatePath: cfg.DefaultCertificatePath, defaultCertificateDir: cfg.DefaultCertificateDir, statsUser: cfg.StatsUsername, statsPassword: cfg.StatsPassword, statsPort: cfg.StatsPort, allowWildcardRoutes: cfg.AllowWildcardRoutes, peerEndpointsKey: peerKey, bindPortsAfterSync: cfg.BindPortsAfterSync, } router, err := newTemplateRouter(templateRouterCfg) return newDefaultTemplatePlugin(router, cfg.IncludeUDP, lookupSvc), err }
// NewTemplatePlugin creates a new TemplatePlugin. func NewTemplatePlugin(cfg TemplatePluginConfig) (*TemplatePlugin, error) { templateBaseName := filepath.Base(cfg.TemplatePath) globalFuncs := template.FuncMap{ "endpointsForAlias": endpointsForAlias, "env": env, "matchString": matchString, "isInteger": isInteger, "matchValues": matchValues, } masterTemplate, err := template.New("config").Funcs(globalFuncs).ParseFiles(cfg.TemplatePath) if err != nil { return nil, err } templates := map[string]*template.Template{} for _, template := range masterTemplate.Templates() { if template.Name() == templateBaseName { continue } templates[template.Name()] = template } peerKey := "" if cfg.PeerService != nil { peerKey = peerEndpointsKey(*cfg.PeerService) } templateRouterCfg := templateRouterCfg{ dir: cfg.WorkingDir, templates: templates, reloadScriptPath: cfg.ReloadScriptPath, reloadInterval: cfg.ReloadInterval, defaultCertificate: cfg.DefaultCertificate, defaultCertificatePath: cfg.DefaultCertificatePath, statsUser: cfg.StatsUsername, statsPassword: cfg.StatsPassword, statsPort: cfg.StatsPort, peerEndpointsKey: peerKey, } router, err := newTemplateRouter(templateRouterCfg) return newDefaultTemplatePlugin(router, cfg.IncludeUDP), err }
// NewTemplatePlugin creates a new TemplatePlugin. func NewTemplatePlugin(cfg TemplatePluginConfig) (*TemplatePlugin, error) { templateBaseName := filepath.Base(cfg.TemplatePath) globalFuncs := template.FuncMap{ "endpointsForAlias": endpointsForAlias, //returns the list of valid endpoints "env": env, //tries to get an environment variable if it can't return a default "matchPattern": matchPattern, //anchors provided regular expression and evaluates against given string "isInteger": isInteger, //determines if a given variable is an integer "matchValues": matchValues, //compares a given string to a list of allowed strings } masterTemplate, err := template.New("config").Funcs(globalFuncs).ParseFiles(cfg.TemplatePath) if err != nil { return nil, err } templates := map[string]*template.Template{} for _, template := range masterTemplate.Templates() { if template.Name() == templateBaseName { continue } templates[template.Name()] = template } peerKey := "" if cfg.PeerService != nil { peerKey = peerEndpointsKey(*cfg.PeerService) } templateRouterCfg := templateRouterCfg{ dir: cfg.WorkingDir, templates: templates, reloadScriptPath: cfg.ReloadScriptPath, reloadInterval: cfg.ReloadInterval, defaultCertificate: cfg.DefaultCertificate, defaultCertificatePath: cfg.DefaultCertificatePath, statsUser: cfg.StatsUsername, statsPassword: cfg.StatsPassword, statsPort: cfg.StatsPort, peerEndpointsKey: peerKey, } router, err := newTemplateRouter(templateRouterCfg) return newDefaultTemplatePlugin(router, cfg.IncludeUDP), err }