๐พ Archived View for source.community โบ Fuwn โบ space โบ blob โบ main โบ route.go captured on 2023-06-14 at 14:15:27. Gemini links have been rewritten to link to archived content
โฌ ๏ธ Previous capture (2023-01-29)
โก๏ธ Next capture (2024-02-05)
-=-=-=-=-=-=-
. ,-. ,-. . . ,-. ,-. ,-. ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . . `-. | | | | | | |-' | | | | | | | | | | | | | | | | | `-' `-' `-^ ' `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-| /| `-'
git clone https://source.community/Fuwn/space.git
View raw contents of /route.go (main)
โโโโโฎ 1โ // Copyright (C) 2021-2021 Fuwn 2โ // SPDX-License-Identifier: GPL-3.0-only 3โ 4โ package main 5โ 6โ import ( 7โ "fmt" 8โ "strings" 9โ 10โ "github.com/fuwn/space/pkg/database" 11โ "github.com/fuwn/space/pkg/utilities" 12โ "github.com/pitr/gig" 13โ ) 14โ 15โ var blogs = make(map[string]string) 16โ 17โ const ( 18โ dateShow = iota 19โ dateNoShow 20โ noDateNoShow 21โ ) 22โ 23โ func createRoute(route string, template string, content string) { 24โ // hostInformation, _ := host.Info() 25โ 26โ g.Handle(route, func(c gig.Context) error { 27โ return c.Render(template, IndexTemplate{ 28โ Content: utilities.TrimLastChar(GetContent(content)), 29โ Quote: utilities.GetRandomQuote(), 30โ Hits: database.Get(route), 31โ /* SystemInfo: fmt.Sprintf( 32โ "Host: %s %s, Uptime: %d seconds, Routes: %d", 33โ strings.Title(hostInformation.Platform), 34โ strings.Title(hostInformation.OS), 35โ int64(time.Since(startTime).Seconds()), 36โ len(g.Routes()), 37โ ), */ 38โ Copyright: utilities.GetCopyright(), 39โ }) 40โ }) 41โ 42โ legacySupport(route) 43โ } 44โ 45โ func createErrorRoute(route string, template string, content string, err string) { 46โ g.Handle(route, func(c gig.Context) error { 47โ return c.Render(template, ErrorTemplate{ 48โ Error: err, 49โ Content: GetContent(content), 50โ Quote: utilities.GetRandomQuote(), 51โ Hits: database.Get(route), 52โ Copyright: utilities.GetCopyright(), 53โ }) 54โ }) 55โ } 56โ 57โ func createFileRoute(route string, file string) { 58โ g.Handle(route, func(c gig.Context) error { 59โ return c.Text(GetContent(file)) 60โ }) 61โ } 62โ 63โ func createBlogHandler(route string) { 64โ g.Handle(route, func(c gig.Context) error { 65โ blogList := "# BLOG LIST (" + fmt.Sprintf("%d", len(blogs)) + ")\n\n" 66โ for blog, name := range blogs { 67โ blogList += fmt.Sprintf("=> %s %s\n", blog, name) 68โ } 69โ blogList = utilities.TrimLastChar(blogList) 70โ 71โ return c.Render("default.gmi", IndexTemplate{ 72โ Content: blogList, 73โ Quote: utilities.GetRandomQuote(), 74โ Hits: database.Get(route), 75โ Copyright: utilities.GetCopyright(), 76โ }) 77โ }) 78โ 79โ legacySupport(route) 80โ } 81โ 82โ func createBlogRoute(baseRoute string, postPath string, name string, reverse bool, showDate int) { 83โ baseRoute = "/blog" + baseRoute 84โ 85โ contents, _ := contentFilesystem.ReadDir("content/" + postPath) 86โ 87โ files := fmt.Sprintf("# %s (%d)\n\n", strings.ToUpper(name), len(contents)-1) 88โ 89โ var description string 90โ 91โ if reverse { 92โ // Reverse contents so that the oldest file is at the bottom 93โ // 94โ // https://stackoverflow.com/a/19239850 95โ for i, j := 0, len(contents)-1; i < j; i, j = i+1, j-1 { 96โ contents[i], contents[j] = contents[j], contents[i] 97โ } 98โ } 99โ 100โ // Could be useful later: 101โ // https://golangcode.com/sorting-an-array-of-numeric-items/ 102โ for _, file := range contents { 103โ if file.Name() == "0description.gmi" { 104โ description = GetContent("pages" + baseRoute + "/" + file.Name()) 105โ files += description + "\n" 106โ continue 107โ } 108โ 109โ // Temporary, until it causes problems... 110โ fileNameNoExt := strings.ReplaceAll(file.Name(), ".gmi", "") 111โ 112โ var postTitle string 113โ switch showDate { 114โ case dateShow: 115โ postTitle = fmt.Sprintf("%s %s", fileNameNoExt[0:10], strings.Title(fileNameNoExt[11:])) 116โ 117โ case dateNoShow: 118โ postTitle = strings.Title(fileNameNoExt[11:]) 119โ 120โ case noDateNoShow: 121โ postTitle = fileNameNoExt 122โ fileNameNoExt = strings.ToLower(fileNameNoExt) 123โ } 124โ 125โ files += fmt.Sprintf("=> %s %s\n", baseRoute+"/"+fileNameNoExt, postTitle) 126โ createRoute(baseRoute+"/"+fileNameNoExt, "default.gmi", "pages"+baseRoute+"/"+file.Name()) 127โ } 128โ 129โ files = utilities.TrimLastChar(files) 130โ 131โ blogs[baseRoute] = name 132โ 133โ g.Handle(baseRoute, func(c gig.Context) error { 134โ return c.Render("default.gmi", IndexTemplate{ 135โ Content: files, 136โ Quote: utilities.GetRandomQuote(), 137โ Hits: database.Get(baseRoute), 138โ Copyright: utilities.GetCopyright(), 139โ }) 140โ }) 141โ legacySupport(baseRoute) 142โ } 143โ 144โ func legacySupport(baseRoute string) { 145โ endString := ".gmi" 146โ if baseRoute[len(baseRoute)-1:] == "/" { 147โ endString = "index.gmi" 148โ } 149โ g.Handle(baseRoute+endString, func(c gig.Context) error { 150โ return c.NoContent(gig.StatusRedirectPermanent, baseRoute) 151โ }) 152โ g.Handle(baseRoute+"/", func(c gig.Context) error { 153โ return c.NoContent(gig.StatusRedirectPermanent, baseRoute) 154โ }) 155โ } 156โ 157โ func createRedirectRoute(route string, redirectRoute string, mask bool) { 158โ g.Handle(route, func(c gig.Context) error { 159โ if mask { 160โ return c.NoContent(gig.StatusRedirectPermanent, redirectRoute+c.URL().Path[2:]) // - /x 161โ } else { 162โ return c.NoContent(gig.StatusRedirectPermanent, redirectRoute) 163โ } 164โ }) 165โ } โโโโโฏ
ยท ยท ยท
ยฉ 2023 source.community