diff options
| author | Fuwn <[email protected]> | 2021-07-17 10:58:26 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-07-17 10:58:26 +0000 |
| commit | c0cda289d92e75daa599cb7513681103e0a2db57 (patch) | |
| tree | 3617b5e7918ff9aa642374ae5f5e5c15842d6edd | |
| parent | fix(templates): add blog quick link (diff) | |
| download | space-c0cda289d92e75daa599cb7513681103e0a2db57.tar.xz space-c0cda289d92e75daa599cb7513681103e0a2db57.zip | |
feat(route): blog handler for multi-blog support
| -rw-r--r-- | handler.go | 4 | ||||
| -rw-r--r-- | route.go | 46 |
2 files changed, 35 insertions, 15 deletions
@@ -15,7 +15,9 @@ 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") + + createBlogRoute("/tech", "pages/blog", "Tech") + createBlogHandler("/blog") } func errors() { @@ -12,6 +12,8 @@ import ( "github.com/pitr/gig" ) +var blogs = make(map[string]string) + func createRoute(route string, template string, content string) { // hostInformation, _ := host.Info() @@ -25,17 +27,7 @@ func createRoute(route string, template string, content string) { }) }) - // Legacy support? - endString := ".gmi" - if route[len(route)-1:] == "/" { - endString = "index.gmi" - } - g.Handle(route+endString, func(c gig.Context) error { - return c.NoContent(gig.StatusRedirectPermanent, route) - }) - g.Handle(route+"/", func(c gig.Context) error { - return c.NoContent(gig.StatusRedirectPermanent, route) - }) + legacySupport(route) } func createErrorRoute(route string, template string, content string, err string) { @@ -56,10 +48,31 @@ func createFileRoute(route string, file string) { }) } -func createBlogRoute(baseRoute string, postPath string) { +func createBlogHandler(route string) { + g.Handle(route, func(c gig.Context) error { + blogList := "# BLOG LIST (" + fmt.Sprintf("%d", len(blogs)) + ")\n\n" + for blog, name := range blogs { + blogList += fmt.Sprintf("=> %s %s\n", blog, name) + } + blogList = utilities.TrimLastChar(blogList) + + return c.Render("default.gmi", IndexTemplate{ + Content: blogList, + Quote: utilities.GetRandomQuote(), + Hits: database.Get(route) + 1, + Copyright: utilities.GetCopyright(), + }) + }) + + legacySupport(route) +} + +func createBlogRoute(baseRoute string, postPath string, name string) { + baseRoute = "/blog" + baseRoute + contents, _ := contentFilesystem.ReadDir("content/" + postPath) - files := "# BLOG POSTS (" + fmt.Sprintf("%d", (len(contents))) + ")\n\n" + files := fmt.Sprintf("# %s (%d)\n\n", strings.ToUpper(name), len(contents)) for _, file := range contents { fileNameNoExt := strings.Replace(file.Name(), ".gmi", "", -1) @@ -68,6 +81,8 @@ func createBlogRoute(baseRoute string, postPath string) { } files = utilities.TrimLastChar(files) + blogs[baseRoute] = name + g.Handle(baseRoute, func(c gig.Context) error { return c.Render("default.gmi", IndexTemplate{ Content: files, @@ -76,7 +91,10 @@ func createBlogRoute(baseRoute string, postPath string) { Copyright: utilities.GetCopyright(), }) }) - // Legacy support? + legacySupport(baseRoute) +} + +func legacySupport(baseRoute string) { endString := ".gmi" if baseRoute[len(baseRoute)-1:] == "/" { endString = "index.gmi" |