From 26512c2b2952116934945af79adc3516c8f842c0 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 5 Jan 2022 01:04:27 +0100 Subject: [PATCH] Add yandex metrica tag --- cmd/renderer/main.go | 2 +- pkg/config/config.go | 7 +++++++ pkg/templates/analytics.go.tmpl | 18 ++++++++++++++++++ pkg/templates/funcs.go | 8 ++++++++ pkg/templates/head.go.tmpl | 2 ++ pkg/templates/templates.go | 1 + 6 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 pkg/templates/analytics.go.tmpl diff --git a/cmd/renderer/main.go b/cmd/renderer/main.go index 46419a7..71bb1e2 100644 --- a/cmd/renderer/main.go +++ b/cmd/renderer/main.go @@ -52,7 +52,7 @@ func main() { logrus.Fatal(err) } - templateFuncs := &templates.Funcs{Version: Version} + templateFuncs := &templates.Funcs{Version: Version, Site: config.Site} err = templates.Load(templateFuncs) if err != nil { logrus.Fatal(err) diff --git a/pkg/config/config.go b/pkg/config/config.go index 68f1f8d..b13c52e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -23,6 +23,10 @@ type Content struct { PostsPerPage int `mapstructure:"postsPerPage"` } +type Site struct { + YandexKey string `mapstructure:"yandexKey"` +} + // Config contains application configuration type Config struct { Verbose string `mapstructure:"verbose"` @@ -30,6 +34,7 @@ type Config struct { Addr string `mapstructure:"addr"` Unix string `mapstructure:"unix"` Content Content `mapstructure:"content"` + Site Site `mapstructure:"site"` } // initialize default values on app-start @@ -50,6 +55,8 @@ func init() { pflag.String("content.pinned", "contact", "pinned page slug") pflag.Int("content.postsPerPage", 5, "amount of posts per page") + pflag.String("site.yandexKey", "", "yandex analytics key") + pflag.Parse() viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/pkg/templates/analytics.go.tmpl b/pkg/templates/analytics.go.tmpl new file mode 100644 index 0000000..fd17f60 --- /dev/null +++ b/pkg/templates/analytics.go.tmpl @@ -0,0 +1,18 @@ +{{ with $yk := getYaKey }} +{{ if $yk }} + + + + +{{ end }} +{{ end }} \ No newline at end of file diff --git a/pkg/templates/funcs.go b/pkg/templates/funcs.go index 41d1927..4c4254f 100644 --- a/pkg/templates/funcs.go +++ b/pkg/templates/funcs.go @@ -4,10 +4,13 @@ import ( "html/template" "net/url" "sync" + + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config" ) type Funcs struct { Version string + Site config.Site compiledJSAppURL string @@ -54,6 +57,10 @@ func (f *Funcs) getBlogURL() string { return URLBlog } +func (f *Funcs) getYaKey() string { + return f.Site.YandexKey +} + // Use returns a func map with template helpers functions func (f *Funcs) Use() template.FuncMap { return template.FuncMap{ @@ -62,5 +69,6 @@ func (f *Funcs) Use() template.FuncMap { "getJSAppURL": f.getJSAppURL, "getIndexURL": f.getIndexURL, "getBlogURL": f.getBlogURL, + "getYaKey": f.getYaKey, } } diff --git a/pkg/templates/head.go.tmpl b/pkg/templates/head.go.tmpl index 4c805e2..63e5564 100644 --- a/pkg/templates/head.go.tmpl +++ b/pkg/templates/head.go.tmpl @@ -6,4 +6,6 @@ {{ .Title }} + + {{ template "analytics.go.tmpl" . }} \ No newline at end of file diff --git a/pkg/templates/templates.go b/pkg/templates/templates.go index 90c27d6..78d863c 100644 --- a/pkg/templates/templates.go +++ b/pkg/templates/templates.go @@ -18,6 +18,7 @@ import ( //go:embed index.go.tmpl //go:embed menu.go.tmpl //go:embed post.go.tmpl +//go:embed analytics.go.tmpl var content embed.FS // List of compiled go-templates