diff options
| author | Fuwn <[email protected]> | 2021-07-15 22:06:22 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-07-15 22:06:22 +0000 |
| commit | c2fd318cce371e2563c248fe7d942500a52a0be8 (patch) | |
| tree | 265c2c0a54f71d57bb9d16a8694e489fe364ed5f | |
| parent | feat(templates): poc for a sysinfo footer item (diff) | |
| download | space-c2fd318cce371e2563c248fe7d942500a52a0be8.tar.xz space-c2fd318cce371e2563c248fe7d942500a52a0be8.zip | |
feat(route): implement a blogging engine
| -rw-r--r-- | content/pages/blog/hello-world.gmi | 5 | ||||
| -rw-r--r-- | handler.go | 1 | ||||
| -rw-r--r-- | pkg/utilities/utilities.go | 9 | ||||
| -rw-r--r-- | route.go | 33 |
4 files changed, 48 insertions, 0 deletions
diff --git a/content/pages/blog/hello-world.gmi b/content/pages/blog/hello-world.gmi new file mode 100644 index 0000000..4267d0c --- /dev/null +++ b/content/pages/blog/hello-world.gmi @@ -0,0 +1,5 @@ +# HELLO WORLD + +2021. 07. 15. + +Welcome to my first blog post!
\ No newline at end of file @@ -15,6 +15,7 @@ func routes() { createRoute("/interests", "default.gmi", "pages/interests.gmi") createRoute("/contact", "default.gmi", "pages/contact.gmi") createRoute("/gemini", "default.gmi", "pages/gemini.gmi") + createBlogRoute("/blog", "pages/blog") } func errors() { diff --git a/pkg/utilities/utilities.go b/pkg/utilities/utilities.go index 6be4e58..bda3034 100644 --- a/pkg/utilities/utilities.go +++ b/pkg/utilities/utilities.go @@ -7,6 +7,7 @@ import ( "math/rand" "os" "time" + "unicode/utf8" "github.com/spf13/viper" ) @@ -38,3 +39,11 @@ func DoesFilesExist(files []string) []string { return nonExistant } + +func TrimLastChar(s string) string { + r, size := utf8.DecodeLastRuneInString(s) + if r == utf8.RuneError && (size == 0 || size == 1) { + size = 0 + } + return s[:len(s)-size] +} @@ -4,6 +4,9 @@ package main import ( + "fmt" + "strings" + "github.com/fuwn/space/pkg/database" "github.com/fuwn/space/pkg/utilities" "github.com/pitr/gig" @@ -49,3 +52,33 @@ func createFileRoute(route string, file string) { return c.Text(GetContent(file)) }) } + +func createBlogRoute(baseRoute string, postPath string) { + contents, _ := contentFilesystem.ReadDir("content/" + postPath) + + files := "# BLOG POSTS (" + fmt.Sprintf("%d", (len(contents))) + ")\n\n" + for _, file := range contents { + fileNameNoExt := strings.Replace(file.Name(), ".gmi", "", -1) + + files += fmt.Sprintf("=> %s %s\n", baseRoute+"/"+file.Name(), fileNameNoExt) + createRoute(baseRoute+"/"+fileNameNoExt, "default.gmi", "pages"+baseRoute+"/"+file.Name()) + } + files = utilities.TrimLastChar(files) + + g.Handle(baseRoute, func(c gig.Context) error { + return c.Render("default.gmi", IndexTemplate{ + Content: files, + Quote: utilities.GetRandomQuote(), + Hits: database.Get(baseRoute) + 1, + Copyright: utilities.GetCopyright(), + }) + }) + // Legacy support? + endString := ".gmi" + if baseRoute[len(baseRoute)-1:] == "/" { + endString = "index.gmi" + } + g.Handle(baseRoute+endString, func(c gig.Context) error { + return c.NoContent(gig.StatusRedirectPermanent, baseRoute) + }) +} |