diff --git a/Dockerfile b/Dockerfile index 48fe088..098f2a1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,7 @@ RUN --mount=type=cache,id=gopath,target=${GOPATH} \ go build \ -o nikita-tokarch-uk-frontend \ -ldflags "-X main.Version=${APP_VERSION}" \ - code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend + code.tokarch.uk/mainnika/nikita-tokarch-uk/cmd/renderer FROM registry.access.redhat.com/ubi8/ubi as js-builder diff --git a/frontend/main.go b/cmd/renderer/main.go similarity index 80% rename from frontend/main.go rename to cmd/renderer/main.go index 8eb3a00..43e93e7 100644 --- a/frontend/main.go +++ b/cmd/renderer/main.go @@ -9,11 +9,10 @@ import ( "github.com/spf13/viper" "github.com/valyala/fasthttp" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/config" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/ghost" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/renderer" - - _ "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/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" @@ -53,7 +52,7 @@ func main() { logrus.Fatal(err) } - ghostClient := &ghost.HTTPClient{ + ghostClient := &httpclient.HTTPClient{ Addr: config.Content.Backend.Addr, Secured: config.Content.Backend.Secured, Headers: config.Content.Backend.Headers, @@ -61,7 +60,7 @@ func main() { QueryTimeout: time.Second, } - rendererHandler := &renderer.Renderer{ + apiRoutes := &routes.Routes{ GhostClient: ghostClient, ContentConfig: config.Content, Base: config.Base, @@ -69,7 +68,7 @@ func main() { httpServer := fasthttp.Server{ Logger: logrus.StandardLogger(), - Handler: rendererHandler.Handler, + Handler: apiRoutes.Handler, Name: frontendServerIdentity, GetOnly: true, } diff --git a/frontend/ghost/ghost.go b/frontend/ghost/ghost.go deleted file mode 100644 index feaa79a..0000000 --- a/frontend/ghost/ghost.go +++ /dev/null @@ -1,13 +0,0 @@ -package ghost - -//go:generate $GOPATH/bin/easyjson -pkg -no_std_marshalers - -// Client is the ghost backend client -type Client interface { - // GetPosts returns blog posts according to query params - GetPosts(queryParams ...Modifier) (posts *Posts, err error) - // GetPostBySlug returns a single post by its slug title and query params - GetPostBySlug(slug string, queryParams ...Modifier) (posts *Posts, err error) - // GetPageBySlug returns a single page by its slug title and query params - GetPageBySlug(slug string, queryParams ...Modifier) (pages *Pages, err error) -} diff --git a/frontend/config/config.go b/pkg/config/config.go similarity index 100% rename from frontend/config/config.go rename to pkg/config/config.go diff --git a/frontend/content/blog.go b/pkg/content/blog.go similarity index 76% rename from frontend/content/blog.go rename to pkg/content/blog.go index ee325a7..441adf9 100644 --- a/frontend/content/blog.go +++ b/pkg/content/blog.go @@ -3,15 +3,15 @@ package content import ( "fmt" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/ghost" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/data" ) // Blog content data type Blog struct { _ interface{} `template:"blog.go.tmpl"` - ghost.Meta - Pinned []ghost.Post - Posts []ghost.Post + data.Meta + Pinned []data.Post + Posts []data.Post } // Title returns blog content title diff --git a/frontend/content/error.go b/pkg/content/error.go similarity index 100% rename from frontend/content/error.go rename to pkg/content/error.go diff --git a/frontend/content/index.go b/pkg/content/index.go similarity index 75% rename from frontend/content/index.go rename to pkg/content/index.go index ed64707..12688d1 100644 --- a/frontend/content/index.go +++ b/pkg/content/index.go @@ -1,15 +1,13 @@ package content -import ( - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/ghost" -) +import "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/data" // Index content data type Index struct { _ interface{} `template:"index.go.tmpl"` - ghost.Meta - Pinned []ghost.Post - Posts []ghost.Post + data.Meta + Pinned []data.Post + Posts []data.Post } // Title returns index title diff --git a/frontend/ghost/data.go b/pkg/ghost/data/data.go similarity index 92% rename from frontend/ghost/data.go rename to pkg/ghost/data/data.go index 87808b5..ec55ec6 100644 --- a/frontend/ghost/data.go +++ b/pkg/ghost/data/data.go @@ -1,4 +1,6 @@ -package ghost +package data + +//go:generate $GOPATH/bin/easyjson -pkg -no_std_marshalers import "html/template" diff --git a/frontend/ghost/ghost_easyjson.go b/pkg/ghost/data/data_easyjson.go similarity index 78% rename from frontend/ghost/ghost_easyjson.go rename to pkg/ghost/data/data_easyjson.go index 5faa3bc..8bd5108 100644 --- a/frontend/ghost/ghost_easyjson.go +++ b/pkg/ghost/data/data_easyjson.go @@ -1,6 +1,6 @@ // Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. -package ghost +package data import ( json "encoding/json" @@ -18,7 +18,7 @@ var ( _ easyjson.Marshaler ) -func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost(in *jlexer.Lexer, out *Posts) { +func easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData(in *jlexer.Lexer, out *Posts) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -72,7 +72,7 @@ func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost(in in.Consumed() } } -func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost(out *jwriter.Writer, in Posts) { +func easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData(out *jwriter.Writer, in Posts) { out.RawByte('{') first := true _ = first @@ -102,14 +102,14 @@ func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost(out // MarshalEasyJSON supports easyjson.Marshaler interface func (v Posts) MarshalEasyJSON(w *jwriter.Writer) { - easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost(w, v) + easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData(w, v) } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *Posts) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost(l, v) + easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData(l, v) } -func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost1(in *jlexer.Lexer, out *Post) { +func easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData1(in *jlexer.Lexer, out *Post) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -148,7 +148,7 @@ func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost1(in in.Consumed() } } -func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost1(out *jwriter.Writer, in Post) { +func easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData1(out *jwriter.Writer, in Post) { out.RawByte('{') first := true _ = first @@ -182,14 +182,14 @@ func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost1(ou // MarshalEasyJSON supports easyjson.Marshaler interface func (v Post) MarshalEasyJSON(w *jwriter.Writer) { - easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost1(w, v) + easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData1(w, v) } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *Post) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost1(l, v) + easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData1(l, v) } -func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost2(in *jlexer.Lexer, out *Pagination) { +func easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData2(in *jlexer.Lexer, out *Pagination) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -226,7 +226,7 @@ func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost2(in in.Consumed() } } -func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost2(out *jwriter.Writer, in Pagination) { +func easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData2(out *jwriter.Writer, in Pagination) { out.RawByte('{') first := true _ = first @@ -255,14 +255,14 @@ func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost2(ou // MarshalEasyJSON supports easyjson.Marshaler interface func (v Pagination) MarshalEasyJSON(w *jwriter.Writer) { - easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost2(w, v) + easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData2(w, v) } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *Pagination) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost2(l, v) + easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData2(l, v) } -func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost3(in *jlexer.Lexer, out *Pages) { +func easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData3(in *jlexer.Lexer, out *Pages) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -316,7 +316,7 @@ func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost3(in in.Consumed() } } -func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost3(out *jwriter.Writer, in Pages) { +func easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData3(out *jwriter.Writer, in Pages) { out.RawByte('{') first := true _ = first @@ -346,14 +346,14 @@ func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost3(ou // MarshalEasyJSON supports easyjson.Marshaler interface func (v Pages) MarshalEasyJSON(w *jwriter.Writer) { - easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost3(w, v) + easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData3(w, v) } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *Pages) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost3(l, v) + easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData3(l, v) } -func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost4(in *jlexer.Lexer, out *Meta) { +func easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData4(in *jlexer.Lexer, out *Meta) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -384,7 +384,7 @@ func easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost4(in in.Consumed() } } -func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost4(out *jwriter.Writer, in Meta) { +func easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData4(out *jwriter.Writer, in Meta) { out.RawByte('{') first := true _ = first @@ -398,10 +398,10 @@ func easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost4(ou // MarshalEasyJSON supports easyjson.Marshaler interface func (v Meta) MarshalEasyJSON(w *jwriter.Writer) { - easyjson72852e1bEncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost4(w, v) + easyjson794297d0EncodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData4(w, v) } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *Meta) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson72852e1bDecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhost4(l, v) + easyjson794297d0DecodeCodeTokarchUkMainnikaNikitaTokarchUkFrontendGhostData4(l, v) } diff --git a/pkg/ghost/ghost.go b/pkg/ghost/ghost.go new file mode 100644 index 0000000..84195d6 --- /dev/null +++ b/pkg/ghost/ghost.go @@ -0,0 +1,16 @@ +package ghost + +import ( + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/data" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params" +) + +// Client is the ghost backend client +type Client interface { + // GetPosts returns blog posts according to query params + GetPosts(queryParams ...params.Modifier) (posts *data.Posts, err error) + // GetPostBySlug returns a single post by its slug title and query params + GetPostBySlug(slug string, queryParams ...params.Modifier) (posts *data.Posts, err error) + // GetPageBySlug returns a single page by its slug title and query params + GetPageBySlug(slug string, queryParams ...params.Modifier) (pages *data.Pages, err error) +} diff --git a/frontend/ghost/params.go b/pkg/ghost/params/params.go similarity index 97% rename from frontend/ghost/params.go rename to pkg/ghost/params/params.go index 672ec90..ffb9994 100644 --- a/frontend/ghost/params.go +++ b/pkg/ghost/params/params.go @@ -1,4 +1,4 @@ -package ghost +package params // Params are generics query argument type Params struct { diff --git a/frontend/ghost/client.go b/pkg/ghost/v4api/httpclient/client.go similarity index 73% rename from frontend/ghost/client.go rename to pkg/ghost/v4api/httpclient/client.go index 6a286a8..a175854 100644 --- a/frontend/ghost/client.go +++ b/pkg/ghost/v4api/httpclient/client.go @@ -1,4 +1,4 @@ -package ghost +package httpclient import ( "fmt" @@ -8,9 +8,13 @@ import ( "github.com/mailru/easyjson" "github.com/valyala/fasthttp" + + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/data" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params" ) -var _ Client = (*HTTPClient)(nil) +var _ ghost.Client = (*HTTPClient)(nil) // Ghost content data URIs: const ( @@ -46,7 +50,7 @@ func (g *HTTPClient) setupClient() { } // doQuery does the method and unmarshals the result into the easyjson Unmarshaler -func (g *HTTPClient) doQuery(method string, v easyjson.Unmarshaler, params Params) (err error) { +func (g *HTTPClient) doQuery(path string, v easyjson.Unmarshaler, params params.Params) (err error) { g.setupClientOnce.Do(g.setupClient) @@ -57,7 +61,7 @@ func (g *HTTPClient) doQuery(method string, v easyjson.Unmarshaler, params Param fasthttp.ReleaseRequest(req) }() - g.setupRequest(method, req) + g.setupRequest(path, req) g.applyParams(params, req) err = g.client.DoTimeout(req, res, g.QueryTimeout) @@ -103,7 +107,7 @@ func (g *HTTPClient) setupRequest(path string, req *fasthttp.Request) { } // applyParams function additionally configure the http request using params -func (g *HTTPClient) applyParams(p Params, req *fasthttp.Request) (err error) { +func (g *HTTPClient) applyParams(p params.Params, req *fasthttp.Request) (err error) { uri := req.URI() @@ -121,10 +125,10 @@ func (g *HTTPClient) applyParams(p Params, req *fasthttp.Request) (err error) { } // GetPageBySlug returns the only one page using slug filter -func (g *HTTPClient) GetPageBySlug(slug string) (pages *Pages, err error) { +func (g *HTTPClient) GetPageBySlug(slug string, queryModifiers ...params.Modifier) (pages *data.Pages, err error) { - pages = &Pages{} - defaultParams := Params{} + pages = &data.Pages{} + defaultParams := params.Params{} method := fmt.Sprintf(ghostAPIGetPageBySlug, slug) err = g.doQuery(method, pages, defaultParams) @@ -136,11 +140,11 @@ func (g *HTTPClient) GetPageBySlug(slug string) (pages *Pages, err error) { } // GetPosts returns posts -func (g *HTTPClient) GetPosts(queryModifiers ...Modifier) (posts *Posts, err error) { +func (g *HTTPClient) GetPosts(queryModifiers ...params.Modifier) (posts *data.Posts, err error) { - posts = &Posts{} - defaultParams := Params{} - combinedParams := Modifiers(queryModifiers).Apply(defaultParams) + posts = &data.Posts{} + defaultParams := params.Params{} + combinedParams := params.Modifiers(queryModifiers).Apply(defaultParams) err = g.doQuery(ghostAPIGetPosts, posts, combinedParams) if err != nil { @@ -151,11 +155,11 @@ func (g *HTTPClient) GetPosts(queryModifiers ...Modifier) (posts *Posts, err err } // GetPostBySlug returns the only one post using slug filter -func (g *HTTPClient) GetPostBySlug(slug string, queryModifiers ...Modifier) (posts *Posts, err error) { +func (g *HTTPClient) GetPostBySlug(slug string, queryModifiers ...params.Modifier) (posts *data.Posts, err error) { - posts = &Posts{} - defaultParams := Params{} - combinedParams := Modifiers(queryModifiers).Apply(defaultParams) + posts = &data.Posts{} + defaultParams := params.Params{} + combinedParams := params.Modifiers(queryModifiers).Apply(defaultParams) method := fmt.Sprintf(ghostAPIGetPostBySlug, slug) err = g.doQuery(method, posts, combinedParams) diff --git a/frontend/renderer/blog.go b/pkg/routes/blog.go similarity index 53% rename from frontend/renderer/blog.go rename to pkg/routes/blog.go index f2d417d..dd26a2a 100644 --- a/frontend/renderer/blog.go +++ b/pkg/routes/blog.go @@ -1,18 +1,22 @@ -package renderer +package routes import ( - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/content" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/ghost" routing "github.com/jackwhelpton/fasthttp-routing/v2" + + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content" + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params" ) // blog handler renders blog data -func (r *Renderer) blog(c *routing.Context) (err error) { +func (r *Routes) blog(c *routing.Context) (err error) { postsPerPage := r.ContentConfig.PostsPerPage currentPage := c.QueryArgs().GetUintOrZero("page") - latestPosts, err := r.GhostClient.GetPosts(ghost.QueryLimit(postsPerPage), ghost.QueryPage(currentPage)) + latestPosts, err := r.GhostClient.GetPosts( + params.WithLimit(postsPerPage), + params.WithPage(currentPage), + ) if err != nil { return } diff --git a/frontend/renderer/error.go b/pkg/routes/error.go similarity index 78% rename from frontend/renderer/error.go rename to pkg/routes/error.go index 651b57a..57297e2 100644 --- a/frontend/renderer/error.go +++ b/pkg/routes/error.go @@ -1,16 +1,17 @@ -package renderer +package routes import ( "fmt" "net/http" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/content" routing "github.com/jackwhelpton/fasthttp-routing/v2" "github.com/sirupsen/logrus" + + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content" ) // errorNotFound renders http error-404 template -func (r *Renderer) errorNotFound(c *routing.Context) (err error) { +func (r *Routes) errorNotFound(c *routing.Context) (err error) { errorContent := content.Error{Message: "not found"} @@ -18,7 +19,7 @@ func (r *Renderer) errorNotFound(c *routing.Context) (err error) { } // useErrorHandler is the middleware that catch handlers errors and render error template -func (r *Renderer) useErrorHandler(c *routing.Context) (err error) { +func (r *Routes) useErrorHandler(c *routing.Context) (err error) { worker := func() (err error) { diff --git a/frontend/renderer/index.go b/pkg/routes/index.go similarity index 61% rename from frontend/renderer/index.go rename to pkg/routes/index.go index 3de7767..c3f654b 100644 --- a/frontend/renderer/index.go +++ b/pkg/routes/index.go @@ -1,16 +1,17 @@ -package renderer +package routes import ( "net/http" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/content" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/ghost" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/templates" routing "github.com/jackwhelpton/fasthttp-routing/v2" + + "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" ) // rootRedirect redirects the root url to the index using http redirect -func (r *Renderer) rootRedirect(c *routing.Context) (err error) { +func (r *Routes) rootRedirect(c *routing.Context) (err error) { c.Redirect(templates.URLIndex, http.StatusFound) @@ -18,7 +19,7 @@ func (r *Renderer) rootRedirect(c *routing.Context) (err error) { } // index handler renders index data -func (r *Renderer) index(c *routing.Context) (err error) { +func (r *Routes) index(c *routing.Context) (err error) { pinnedPageSlug := r.ContentConfig.Pinned postsPerPage := r.ContentConfig.PostsPerPage @@ -28,7 +29,7 @@ func (r *Renderer) index(c *routing.Context) (err error) { return } - latestPosts, err := r.GhostClient.GetPosts(ghost.QueryLimit(postsPerPage)) + latestPosts, err := r.GhostClient.GetPosts(params.WithLimit(postsPerPage)) if err != nil { return } diff --git a/frontend/renderer/output.go b/pkg/routes/output.go similarity index 85% rename from frontend/renderer/output.go rename to pkg/routes/output.go index d9caa33..4e61b25 100644 --- a/frontend/renderer/output.go +++ b/pkg/routes/output.go @@ -1,11 +1,12 @@ -package renderer +package routes import ( "io" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/templates" routing "github.com/jackwhelpton/fasthttp-routing/v2" "github.com/valyala/fasthttp" + + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates" ) var _ routing.DataWriter = (*TemplateWriter)(nil) @@ -30,7 +31,7 @@ func (tw *TemplateWriter) Write(w io.Writer, content interface{}) error { } // useTemplateWriter is the routing middleware to set the default data writer -func (r *Renderer) useTemplateWriter(c *routing.Context) (err error) { +func (r *Routes) useTemplateWriter(c *routing.Context) (err error) { c.SetDataWriter(staticWriter) diff --git a/frontend/renderer/renderer.go b/pkg/routes/routes.go similarity index 66% rename from frontend/renderer/renderer.go rename to pkg/routes/routes.go index d21fce9..f2a8a3b 100644 --- a/frontend/renderer/renderer.go +++ b/pkg/routes/routes.go @@ -1,4 +1,4 @@ -package renderer +package routes import ( "sync" @@ -6,13 +6,13 @@ import ( routing "github.com/jackwhelpton/fasthttp-routing/v2" "github.com/valyala/fasthttp" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/config" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/ghost" - "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/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" ) -// Renderer is the main handler that contains all routes handlers -type Renderer struct { +// Routes is the main handler that contains all routes handlers +type Routes struct { GhostClient ghost.Client ContentConfig config.Content @@ -25,13 +25,13 @@ type Renderer struct { } // Handler invokes the lazy once-initializer and then does the request -func (r *Renderer) Handler(ctx *fasthttp.RequestCtx) { +func (r *Routes) Handler(ctx *fasthttp.RequestCtx) { r.initOnce.Do(r.init) r.handler(ctx) } // init has the renderer initialization -func (r *Renderer) init() { +func (r *Routes) init() { router := routing.New() diff --git a/frontend/templates/blog.go.tmpl b/pkg/templates/blog.go.tmpl similarity index 100% rename from frontend/templates/blog.go.tmpl rename to pkg/templates/blog.go.tmpl diff --git a/frontend/templates/error.go.tmpl b/pkg/templates/error.go.tmpl similarity index 100% rename from frontend/templates/error.go.tmpl rename to pkg/templates/error.go.tmpl diff --git a/frontend/templates/funcs.go b/pkg/templates/funcs.go similarity index 100% rename from frontend/templates/funcs.go rename to pkg/templates/funcs.go diff --git a/frontend/templates/head.go.tmpl b/pkg/templates/head.go.tmpl similarity index 100% rename from frontend/templates/head.go.tmpl rename to pkg/templates/head.go.tmpl diff --git a/frontend/templates/index.go.tmpl b/pkg/templates/index.go.tmpl similarity index 100% rename from frontend/templates/index.go.tmpl rename to pkg/templates/index.go.tmpl diff --git a/frontend/templates/menu.go.tmpl b/pkg/templates/menu.go.tmpl similarity index 100% rename from frontend/templates/menu.go.tmpl rename to pkg/templates/menu.go.tmpl diff --git a/frontend/templates/post.go.tmpl b/pkg/templates/post.go.tmpl similarity index 100% rename from frontend/templates/post.go.tmpl rename to pkg/templates/post.go.tmpl diff --git a/frontend/templates/templates.go b/pkg/templates/templates.go similarity index 100% rename from frontend/templates/templates.go rename to pkg/templates/templates.go diff --git a/frontend/templates/urls.go b/pkg/templates/urls.go similarity index 100% rename from frontend/templates/urls.go rename to pkg/templates/urls.go