2 SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
3 Copyright (C) 2020-2022 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 "github.com/go-git/go-git/v5"
34 "github.com/go-git/go-git/v5/plumbing"
35 "github.com/go-git/go-git/v5/plumbing/object"
36 "go.stargrave.org/sgblog"
40 //go:embed gopher-menu.tmpl
41 TmplGopherMenuRaw string
42 //go:embed gopher-entry.tmpl
43 TmplGopherEntryRaw string
45 TmplGopherMenu = template.Must(template.New("gopher-menu").Parse(TmplGopherMenuRaw))
46 TmplGopherEntry = template.Must(template.New("gopher-entry").Parse(TmplGopherEntryRaw))
49 type TableMenuEntry struct {
57 func serveGopher(cfgPath string) {
58 cfg, err := readCfg(cfgPath)
62 if cfg.GopherDomain == "" {
63 log.Fatalln("GopherDomain is not configured")
66 headHash, err := initRepo(cfg)
71 scanner := bufio.NewScanner(io.LimitReader(os.Stdin, 1<<8))
73 log.Fatalln(errors.New("no CRLF found"))
75 selector := scanner.Text()
79 selectorParts := strings.Split(selector, "/")
80 if strings.HasPrefix(selector, "URL:") {
81 selector = selector[len("URL:"):]
82 fmt.Printf(`<!DOCTYPE html>
85 <meta http-equiv="Refresh" content="1; url=%s" />
86 <title>Redirect to non-gopher URL</title>
89 Redirecting to <a href="%s">%s</a>...
92 `, selector, selector, selector)
93 } else if sha1DigestRe.MatchString(selector) {
94 commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
98 err = TmplGopherEntry.Execute(os.Stdout, struct {
109 When: commit.Author.When.Format(sgblog.WhenFmt),
111 Note: string(sgblog.GetNote(repo, notesTree, commit.Hash)),
112 Comments: sgblog.ParseComments(sgblog.GetNote(repo, commentsTree, commit.Hash)),
113 Topics: sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)),
114 Version: sgblog.Version,
115 TitleEscaped: url.PathEscape(fmt.Sprintf(
116 "Re: %s (%s)", msgSplit(commit.Message)[0], commit.Hash,
122 } else if len(selectorParts) > 1 &&
123 selectorParts[len(selectorParts)-2] == "offset" {
124 offset, err := strconv.Atoi(selectorParts[len(selectorParts)-1])
128 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
132 topicsCache, err := getTopicsCache(cfg, repoLog)
136 repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
142 if len(selectorParts) == 3 {
143 topic = selectorParts[0]
145 var commits CommitIterNext
147 for i := 0; i < offset; i++ {
148 if _, err = repoLog.Next(); err != nil {
154 hashes := topicsCache[topic]
156 log.Fatalln(errors.New("no posts with that topic"))
158 if len(hashes) > offset {
159 hashes = hashes[offset:]
161 commits = &HashesIter{hashes}
165 entries := make([]TableMenuEntry, 0, PageEntries)
166 for i := 0; i < PageEntries; i++ {
167 commit, err := commits.Next()
172 lines := msgSplit(commit.Message)
173 entries = append(entries, TableMenuEntry{
176 LinesNum: len(lines) - 2,
177 CommentsNum: len(sgblog.ParseComments(sgblog.GetNote(repo, commentsTree, commit.Hash))),
178 Topics: sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)),
181 offsetPrev := offset - PageEntries
185 err = TmplGopherMenu.Execute(os.Stdout, struct {
192 Entries []TableMenuEntry
199 OffsetPrev: offsetPrev,
200 OffsetNext: offset + PageEntries,
203 Topics: topicsCache.Topics(),
204 Version: sgblog.Version,
210 log.Fatalln(errors.New("unknown selector"))