aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--content/pages/blog/hello-world.gmi5
-rw-r--r--handler.go1
-rw-r--r--pkg/utilities/utilities.go9
-rw-r--r--route.go33
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
diff --git a/handler.go b/handler.go
index 54a7b8a..cd6bb3d 100644
--- a/handler.go
+++ b/handler.go
@@ -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]
+}
diff --git a/route.go b/route.go
index f6abef7..abda084 100644
--- a/route.go
+++ b/route.go
@@ -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)
+ })
+}