From c2b58f21c85bd7d5a001c421bce432a500cebb2a Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 9 Feb 2022 13:01:41 +0100 Subject: [PATCH] Move templates to an internal package --- .dockerignore | 1 + Dockerfile | 3 +++ cmd/renderer/main.go | 5 +++-- go.mod | 3 +++ pkg/routes/index.go | 3 ++- pkg/routes/output.go | 2 +- pkg/routes/routes.go | 3 ++- {pkg/templates => templates}/analytics.go.tmpl | 0 {pkg/templates => templates}/blog.go.tmpl | 0 {pkg/templates => templates}/error.go.tmpl | 0 {pkg/templates => templates}/funcs.go | 7 +++---- templates/go.mod | 7 +++++++ templates/go.sum | 10 ++++++++++ {pkg/templates => templates}/head.go.tmpl | 0 {pkg/templates => templates}/index.go.tmpl | 0 {pkg/templates => templates}/menu.go.tmpl | 0 {pkg/templates => templates}/post.go.tmpl | 0 {pkg/templates => templates}/templates.go | 4 ++-- {pkg/templates => templates}/urls.go | 0 19 files changed, 37 insertions(+), 11 deletions(-) rename {pkg/templates => templates}/analytics.go.tmpl (100%) rename {pkg/templates => templates}/blog.go.tmpl (100%) rename {pkg/templates => templates}/error.go.tmpl (100%) rename {pkg/templates => templates}/funcs.go (90%) create mode 100644 templates/go.mod create mode 100644 templates/go.sum rename {pkg/templates => templates}/head.go.tmpl (100%) rename {pkg/templates => templates}/index.go.tmpl (100%) rename {pkg/templates => templates}/menu.go.tmpl (100%) rename {pkg/templates => templates}/post.go.tmpl (100%) rename {pkg/templates => templates}/templates.go (96%) rename {pkg/templates => templates}/urls.go (100%) diff --git a/.dockerignore b/.dockerignore index 5de0ae6..165ce64 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,6 +2,7 @@ !cmd !nginx !pkg +!templates !web !go.mod !go.sum diff --git a/Dockerfile b/Dockerfile index bb09166..eefbd25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,8 @@ RUN dnf install -yq golang COPY go.mod . COPY go.sum . +COPY templates/go.mod templates/go.mod +COPY templates/go.sum templates/go.sum RUN --mount=type=cache,id=gopath,target=${GOPATH} \ go mod \ @@ -18,6 +20,7 @@ ARG APP_VERSION=containerized COPY cmd cmd COPY pkg pkg +COPY templates templates RUN --mount=type=cache,id=gopath,target=${GOPATH} \ go build \ diff --git a/cmd/renderer/main.go b/cmd/renderer/main.go index 71bb1e2..c3d9910 100644 --- a/cmd/renderer/main.go +++ b/cmd/renderer/main.go @@ -9,10 +9,11 @@ import ( "github.com/spf13/viper" "github.com/valyala/fasthttp" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/templates" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/v4api/httpclient" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/routes" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates" ) var Version = "nightly" @@ -52,7 +53,7 @@ func main() { logrus.Fatal(err) } - templateFuncs := &templates.Funcs{Version: Version, Site: config.Site} + templateFuncs := &templates.Funcs{Version: Version, YandexKey: config.Site.YandexKey} err = templates.Load(templateFuncs) if err != nil { logrus.Fatal(err) diff --git a/go.mod b/go.mod index ddb1f71..99bfffc 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module code.tokarch.uk/mainnika/nikita-tokarch-uk go 1.17 +replace code.tokarch.uk/mainnika/nikita-tokarch-uk/templates => ./templates + require ( + code.tokarch.uk/mainnika/nikita-tokarch-uk/templates v0.0.0-00010101000000-000000000000 github.com/jackwhelpton/fasthttp-routing/v2 v2.0.0 github.com/mailru/easyjson v0.7.7 github.com/sirupsen/logrus v1.8.1 diff --git a/pkg/routes/index.go b/pkg/routes/index.go index a77f83e..b1c4e21 100644 --- a/pkg/routes/index.go +++ b/pkg/routes/index.go @@ -6,9 +6,10 @@ import ( routing "github.com/jackwhelpton/fasthttp-routing/v2" "github.com/valyala/fasthttp" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/templates" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates" ) // relativeRedirectBytes makes a relative redirect by using http Location header diff --git a/pkg/routes/output.go b/pkg/routes/output.go index 567c255..7f382cc 100644 --- a/pkg/routes/output.go +++ b/pkg/routes/output.go @@ -6,7 +6,7 @@ import ( routing "github.com/jackwhelpton/fasthttp-routing/v2" "github.com/valyala/fasthttp" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/templates" ) var _ routing.DataWriter = (*TemplateWriter)(nil) diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 5922302..4fc63b0 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -6,9 +6,10 @@ import ( routing "github.com/jackwhelpton/fasthttp-routing/v2" "github.com/valyala/fasthttp" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/templates" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates" ) // Routes is the main handler that contains all routes handlers diff --git a/pkg/templates/analytics.go.tmpl b/templates/analytics.go.tmpl similarity index 100% rename from pkg/templates/analytics.go.tmpl rename to templates/analytics.go.tmpl diff --git a/pkg/templates/blog.go.tmpl b/templates/blog.go.tmpl similarity index 100% rename from pkg/templates/blog.go.tmpl rename to templates/blog.go.tmpl diff --git a/pkg/templates/error.go.tmpl b/templates/error.go.tmpl similarity index 100% rename from pkg/templates/error.go.tmpl rename to templates/error.go.tmpl diff --git a/pkg/templates/funcs.go b/templates/funcs.go similarity index 90% rename from pkg/templates/funcs.go rename to templates/funcs.go index 4c4254f..e41b13d 100644 --- a/pkg/templates/funcs.go +++ b/templates/funcs.go @@ -4,13 +4,12 @@ import ( "html/template" "net/url" "sync" - - "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config" ) type Funcs struct { Version string - Site config.Site + + YandexKey string compiledJSAppURL string @@ -58,7 +57,7 @@ func (f *Funcs) getBlogURL() string { } func (f *Funcs) getYaKey() string { - return f.Site.YandexKey + return f.YandexKey } // Use returns a func map with template helpers functions diff --git a/templates/go.mod b/templates/go.mod new file mode 100644 index 0000000..a3d7a59 --- /dev/null +++ b/templates/go.mod @@ -0,0 +1,7 @@ +module code.tokarch.uk/mainnika/nikita-tokarch-uk/templates + +go 1.17 + +require github.com/sirupsen/logrus v1.8.1 + +require golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect diff --git a/templates/go.sum b/templates/go.sum new file mode 100644 index 0000000..59bd790 --- /dev/null +++ b/templates/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/pkg/templates/head.go.tmpl b/templates/head.go.tmpl similarity index 100% rename from pkg/templates/head.go.tmpl rename to templates/head.go.tmpl diff --git a/pkg/templates/index.go.tmpl b/templates/index.go.tmpl similarity index 100% rename from pkg/templates/index.go.tmpl rename to templates/index.go.tmpl diff --git a/pkg/templates/menu.go.tmpl b/templates/menu.go.tmpl similarity index 100% rename from pkg/templates/menu.go.tmpl rename to templates/menu.go.tmpl diff --git a/pkg/templates/post.go.tmpl b/templates/post.go.tmpl similarity index 100% rename from pkg/templates/post.go.tmpl rename to templates/post.go.tmpl diff --git a/pkg/templates/templates.go b/templates/templates.go similarity index 96% rename from pkg/templates/templates.go rename to templates/templates.go index 78d863c..3e24888 100644 --- a/pkg/templates/templates.go +++ b/templates/templates.go @@ -21,10 +21,10 @@ import ( //go:embed analytics.go.tmpl var content embed.FS -// List of compiled go-templates +// Templates is a container of compiled templates var Templates *template.Template = template.New("") -// Load embeded templates +// Load embedded templates func Load(funcs *Funcs) (err error) { Templates.Funcs(funcs.Use()) diff --git a/pkg/templates/urls.go b/templates/urls.go similarity index 100% rename from pkg/templates/urls.go rename to templates/urls.go