Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,34 @@ goblog generate posts/ output/
goblog serve posts/
```

### `generate` flags
### Global flags

These flags apply to both `generate` and `serve` and may be passed before or after the subcommand name.

| Flag | Short | Default | Description |
|---|---|---|---|
| `--raw` | `-r` | `false` | Output raw HTML without template wrapping |
| `--template-dir` | `-t` | built-in | Path to a custom template directory |
| `--root-path` | `-p` | `/` | Blog root path for subdirectory deployment |
| `--disable-tags` | `-T` | `false` | Disable tag tracking and tag page generation |
| `--disable-reading-time` | | `false` | Disable reading time estimation on posts |
| `--root-path` | `-p` | `/` | Blog root path for subdirectory deployment |
| `--template-dir` | `-t` | built-in | Path to a custom template directory |
| `--base-url` | | _(none)_ | Scheme + host of the site (e.g. `https://example.com`); required to generate RSS/Atom feeds. Must not include a path — use `--root-path` for subdirectory deployments |
| `--disable-feeds` | | `false` | Disable RSS and Atom feed generation |
| `--feed-limit` | | `10` | Maximum number of posts to include in each feed (`0` = unlimited) |

### `generate` flags

| Flag | Short | Default | Description |
|---|---|---|---|
| `--raw` | `-r` | `false` | Output raw HTML without template wrapping |

### `serve` flags

When `--base-url` is set, the server also exposes the generated feeds at `{root-path}rss.xml`, `{root-path}atom.xml`, and per-tag feeds at `{root-path}tags/{tag}.rss.xml` / `{root-path}tags/{tag}.atom.xml`.

| Flag | Short | Default | Description |
|---|---|---|---|
| `--port` | `-P` | `8080` | TCP port to listen on |
| `--host` | `-H` | all interfaces | Host address to bind to |
| `--disable-tags` | `-T` | `false` | Disable tag tracking and tag page generation |
| `--disable-reading-time` | | `false` | Disable reading time estimation on posts |
| `--root-path` | `-p` | `/` | Blog root path for subdirectory deployment |
| `--template-dir` | `-t` | built-in | Path to a custom template directory |
| `--watch` | `-w` | `false` | Watch the posts directory and regenerate on changes |
| `--cache-control` | | `1h` | Max-age TTL for the `Cache-Control` header (`0` disables) |
| `--health-checks` | | `false` | Expose `/healthz/live`, `/healthz/ready`, and `/healthz/startup` endpoints (no auth required); server binds before loading content so probes observe startup state |
Expand Down
5 changes: 3 additions & 2 deletions cmd/goblog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log/slog"
"os"

"github.com/harrydayexe/GoBlog/v2/internal/cliflags"
"github.com/harrydayexe/GoBlog/v2/internal/generator"
loggermod "github.com/harrydayexe/GoBlog/v2/internal/logger"
"github.com/harrydayexe/GoBlog/v2/internal/server"
Expand Down Expand Up @@ -45,7 +46,7 @@ func main() {
&generator.GeneratorCommand,
&server.ServeCommand,
},
Flags: []cli.Flag{
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Expand All @@ -54,7 +55,7 @@ func main() {
Count: &verbosity,
},
},
},
}, cliflags.Shared()...),
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
var level slog.Level
switch verbosity {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/alecthomas/chroma/v2 v2.22.0
github.com/fatih/color v1.18.0
github.com/fsnotify/fsnotify v1.10.1
github.com/gorilla/feeds v1.2.0
github.com/harrydayexe/GoWebUtilities v1.5.1
github.com/urfave/cli/v3 v3.6.1
github.com/yuin/goldmark v1.7.16
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho=
github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo=
github.com/gorilla/feeds v1.2.0 h1:O6pBiXJ5JHhPvqy53NsjKOThq+dNFm8+DFrxBEdzSCc=
github.com/gorilla/feeds v1.2.0/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y=
github.com/harrydayexe/GoWebUtilities v1.5.1 h1:1E/5IEBHdG8SfIwF2tqJhxypsk9VH1TIb1ofMmI6ZOg=
github.com/harrydayexe/GoWebUtilities v1.5.1/go.mod h1:msGwhqkUbSAhhIR+uFnLE08OhBCwuJW0BQ5kUnBvI6I=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
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/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
Expand Down
1 change: 1 addition & 0 deletions integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/feeds v1.2.0 // indirect
github.com/harrydayexe/GoWebUtilities v1.5.1 // indirect
github.com/klauspost/compress v1.18.5 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/feeds v1.2.0 h1:O6pBiXJ5JHhPvqy53NsjKOThq+dNFm8+DFrxBEdzSCc=
github.com/gorilla/feeds v1.2.0/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y=
github.com/harrydayexe/GoWebUtilities v1.5.1 h1:1E/5IEBHdG8SfIwF2tqJhxypsk9VH1TIb1ofMmI6ZOg=
github.com/harrydayexe/GoWebUtilities v1.5.1/go.mod h1:msGwhqkUbSAhhIR+uFnLE08OhBCwuJW0BQ5kUnBvI6I=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
Expand Down
80 changes: 80 additions & 0 deletions internal/cliflags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

// Package cliflags defines the CLI flag names and flag definitions that are
// shared between the generate and serve subcommands. Keeping them here avoids
// duplication and import cycles: both the root goblog command (which registers
// the flags on the top-level cli.Command) and the subcommand action packages
// (which read flag values) can import this package.
package cliflags

import "github.com/urfave/cli/v3"

// TemplateDirFlagName is the CLI flag name for setting a custom template directory.
const TemplateDirFlagName = "template-dir"

// BlogRootFlagName is the CLI flag name for setting the blog root path.
const BlogRootFlagName = "root-path"

// DisableTagsFlagName is the CLI flag name for disabling tag page generation.
const DisableTagsFlagName = "disable-tags"

// DisableReadingTimeFlagName is the CLI flag name for disabling reading time estimation.
const DisableReadingTimeFlagName = "disable-reading-time"

// BaseURLFlagName is the CLI flag name for setting the site's absolute base URL
// (required for RSS/Atom feed generation).
const BaseURLFlagName = "base-url"

// DisableFeedsFlagName is the CLI flag name for disabling RSS and Atom feed generation.
const DisableFeedsFlagName = "disable-feeds"

// FeedLimitFlagName is the CLI flag name for setting the maximum number of posts
// in each feed.
const FeedLimitFlagName = "feed-limit"

// Shared returns the CLI flag definitions that apply to both the generate and
// serve subcommands. They are registered on the top-level goblog command so that
// urfave/cli v3's default persistence makes them available in both subcommand
// actions via c.String/Bool/Int.
func Shared() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: TemplateDirFlagName,
Aliases: []string{"t"},
Usage: "directory of templates to use when rendering",
},
&cli.StringFlag{
Name: BlogRootFlagName,
Aliases: []string{"p"},
Usage: "root path of the blog, defaults to '/'",
Value: "/",
},
&cli.BoolFlag{
Name: DisableTagsFlagName,
Aliases: []string{"T"},
Usage: "disable tag tracking and tag page generation",
Value: false,
},
&cli.BoolFlag{
Name: DisableReadingTimeFlagName,
Usage: "disable reading time estimation on posts",
Value: false,
},
&cli.StringFlag{
Name: BaseURLFlagName,
Usage: "absolute base URL of the site (e.g. https://example.com); required for RSS/Atom feed generation",
},
&cli.BoolFlag{
Name: DisableFeedsFlagName,
Usage: "disable RSS and Atom feed generation",
Value: false,
},
&cli.IntFlag{
Name: FeedLimitFlagName,
Usage: "maximum number of posts to include in each feed (0 = unlimited)",
Value: 10,
},
}
}
22 changes: 0 additions & 22 deletions internal/generator/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,5 @@ var GeneratorCommand cli.Command = cli.Command{
Usage: "output raw HTML without template wrapper (skips tag pages and template rendering)",
Value: false,
},
&cli.StringFlag{
Name: TemplateDirFlagName,
Aliases: []string{"t"},
Usage: "directory of templates to use when rendering",
},
&cli.StringFlag{
Name: BlogRootFlagName,
Aliases: []string{"p"},
Usage: "root path of the blog, defaults to '/'",
Value: "/",
},
&cli.BoolFlag{
Name: DisableTagsFlagName,
Aliases: []string{"T"},
Usage: "disable tag tracking and tag page generation",
Value: false,
},
&cli.BoolFlag{
Name: DisableReadingTimeFlagName,
Usage: "disable reading time estimation on posts",
Value: false,
},
},
}
12 changes: 0 additions & 12 deletions internal/generator/flagConsts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,3 @@ const InputPostsDirArgName = "input-posts"

// OutputDirArgName is the CLI argument name for the output directory.
const OutputDirArgName = "output-dir"

// TemplateDirArgName is the CLI flag name for setting a template directory
const TemplateDirFlagName = "template-dir"

// BlogRootFlagName is the CLI flag name for setting a blog root path
const BlogRootFlagName = "root-path"

// DisableTagsFlagName is the CLI flag name for disabling tag page generation.
const DisableTagsFlagName = "disable-tags"

// DisableReadingTimeFlagName is the CLI flag name for disabling reading time estimation.
const DisableReadingTimeFlagName = "disable-reading-time"
19 changes: 15 additions & 4 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"

"github.com/harrydayexe/GoBlog/v2/internal/cliflags"
"github.com/harrydayexe/GoBlog/v2/internal/utilities"
"github.com/harrydayexe/GoBlog/v2/pkg/config"
"github.com/harrydayexe/GoBlog/v2/pkg/generator"
Expand Down Expand Up @@ -51,15 +52,25 @@ func NewGeneratorCommand(ctx context.Context, c *cli.Command) error {
opts = append(opts, config.WithRawOutput())
}

if c.Bool(DisableTagsFlagName) {
if c.Bool(cliflags.DisableTagsFlagName) {
opts = append(opts, config.WithDisableTags())
}

if c.Bool(DisableReadingTimeFlagName) {
if c.Bool(cliflags.DisableReadingTimeFlagName) {
opts = append(opts, config.WithDisableReadingTime())
}

templateDirPath := c.String(TemplateDirFlagName)
if baseURL := c.String(cliflags.BaseURLFlagName); baseURL != "" {
opts = append(opts, config.WithBaseURL(baseURL))
}

if c.Bool(cliflags.DisableFeedsFlagName) {
opts = append(opts, config.WithDisableFeeds())
}

opts = append(opts, config.WithFeedPostLimit(c.Int(cliflags.FeedLimitFlagName)))

templateDirPath := c.String(cliflags.TemplateDirFlagName)
var templateDir fs.FS
if templateDirPath == "" {
slog.Default().DebugContext(ctx, "Using default templates")
Expand All @@ -74,7 +85,7 @@ func NewGeneratorCommand(ctx context.Context, c *cli.Command) error {
templateDir = os.DirFS(templateDirPath)
}

blogRootString := c.String(BlogRootFlagName)
blogRootString := c.String(cliflags.BlogRootFlagName)
if blogRootString != "" {
blogRootClean := filepath.Clean(blogRootString)
blogRoot := strings.TrimPrefix(blogRootClean, ".")
Expand Down
22 changes: 0 additions & 22 deletions internal/server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,6 @@ var ServeCommand cli.Command = cli.Command{
Aliases: []string{"H"},
Usage: "host address to bind to",
},
&cli.StringFlag{
Name: TemplateDirFlagName,
Aliases: []string{"t"},
Usage: "directory of templates to use when rendering",
},
&cli.StringFlag{
Name: BlogRootFlagName,
Aliases: []string{"p"},
Usage: "root path of the blog, defaults to '/'",
Value: "/",
},
&cli.BoolFlag{
Name: DisableTagsFlagName,
Aliases: []string{"T"},
Usage: "disable tag tracking and tag page generation",
Value: false,
},
&cli.BoolFlag{
Name: DisableReadingTimeFlagName,
Usage: "disable reading time estimation on posts",
Value: false,
},
&cli.BoolFlag{
Name: WatchFlagName,
Aliases: []string{"w"},
Expand Down
54 changes: 54 additions & 0 deletions internal/server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,57 @@ This is a new post.
}
t.Error("new post not available after 5s of watching")
}

// TestRunServe_ServesFeedsWithBaseURL verifies that the server exposes
// /rss.xml and /atom.xml when the generator is configured with a base URL.
func TestRunServe_ServesFeedsWithBaseURL(t *testing.T) {
t.Parallel()

cfg := config.ServerConfig{
Server: []config.BaseServerOption{config.WithPort(0)},
Gen: []config.GeneratorOption{
config.WithBaseURL("https://example.com"),
},
}

srv, err := pkgserver.New(discardLogger(), testFS(), cfg)
if err != nil {
t.Fatalf("server.New() error = %v", err)
}

for _, path := range []string{"/rss.xml", "/atom.xml"} {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, path, nil)
srv.ServeHTTP(rec, req)

if rec.Code != http.StatusOK {
t.Errorf("GET %s status = %d, want %d", path, rec.Code, http.StatusOK)
}
}
}

// TestRunServe_FeedsNotAvailableWithoutBaseURL verifies that feed routes return
// 404 when no base URL is configured (feeds are not generated).
func TestRunServe_FeedsNotAvailableWithoutBaseURL(t *testing.T) {
t.Parallel()

cfg := config.ServerConfig{
Server: []config.BaseServerOption{config.WithPort(0)},
// No WithBaseURL → generator skips feed generation.
}

srv, err := pkgserver.New(discardLogger(), testFS(), cfg)
if err != nil {
t.Fatalf("server.New() error = %v", err)
}

for _, path := range []string{"/rss.xml", "/atom.xml"} {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, path, nil)
srv.ServeHTTP(rec, req)

if rec.Code != http.StatusNotFound {
t.Errorf("GET %s (no base-url) status = %d, want %d", path, rec.Code, http.StatusNotFound)
}
}
}
12 changes: 0 additions & 12 deletions internal/server/flagConsts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ const PortFlagName = "port"
// HostFlagName is the CLI flag name for setting the HTTP bind address.
const HostFlagName = "host"

// TemplateDirFlagName is the CLI flag name for setting a template directory.
const TemplateDirFlagName = "template-dir"

// BlogRootFlagName is the CLI flag name for setting a blog root path.
const BlogRootFlagName = "root-path"

// DisableTagsFlagName is the CLI flag name for disabling tag page generation.
const DisableTagsFlagName = "disable-tags"

// DisableReadingTimeFlagName is the CLI flag name for disabling reading time estimation.
const DisableReadingTimeFlagName = "disable-reading-time"

// WatchFlagName is the CLI flag name for enabling filesystem watching.
const WatchFlagName = "watch"

Expand Down
Loading
Loading