aboutsummaryrefslogtreecommitdiff
path: root/route.go
blob: bfdd649371f298cfedf375269ba477421b96128c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (C) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

package main

import (
	"fmt"
	"strings"

	"github.com/fuwn/space/pkg/database"
	"github.com/fuwn/space/pkg/utilities"
	"github.com/pitr/gig"
)

var blogs = make(map[string]string)

const (
	dateShow = iota
	dateNoShow
	noDateNoShow
)

func createRoute(route string, template string, content string) {
	// hostInformation, _ := host.Info()

	g.Handle(route, func(c gig.Context) error {
		return c.Render(template, IndexTemplate{
			Content: utilities.TrimLastChar(GetContent(content)),
			Quote:   utilities.GetRandomQuote(),
			Hits:    database.Get(route),
			/* SystemInfo: fmt.Sprintf(
				"Host: %s %s, Uptime: %d seconds, Routes: %d",
				strings.Title(hostInformation.Platform),
				strings.Title(hostInformation.OS),
				int64(time.Since(startTime).Seconds()),
				len(g.Routes()),
			), */
			Copyright: utilities.GetCopyright(),
		})
	})

	legacySupport(route)
}

func createErrorRoute(route string, template string, content string, err string) {
	g.Handle(route, func(c gig.Context) error {
		return c.Render(template, ErrorTemplate{
			Error:     err,
			Content:   GetContent(content),
			Quote:     utilities.GetRandomQuote(),
			Hits:      database.Get(route),
			Copyright: utilities.GetCopyright(),
		})
	})
}

func createFileRoute(route string, file string) {
	g.Handle(route, func(c gig.Context) error {
		return c.Text(GetContent(file))
	})
}

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),
			Copyright: utilities.GetCopyright(),
		})
	})

	legacySupport(route)
}

func createBlogRoute(baseRoute string, postPath string, name string, reverse bool, showDate int) {
	baseRoute = "/blog" + baseRoute

	contents, _ := contentFilesystem.ReadDir("content/" + postPath)

	files := fmt.Sprintf("# %s (%d)\n\n", strings.ToUpper(name), len(contents)-1)

	var description string

	if reverse {
		// Reverse contents so that the oldest file is at the bottom
		//
		// https://stackoverflow.com/a/19239850
		for i, j := 0, len(contents)-1; i < j; i, j = i+1, j-1 {
			contents[i], contents[j] = contents[j], contents[i]
		}
	}

	// Could be useful later:
	// https://golangcode.com/sorting-an-array-of-numeric-items/
	for _, file := range contents {
		if file.Name() == "0description.gmi" {
			description = GetContent("pages" + baseRoute + "/" + file.Name())
			files += description + "\n"
			continue
		}

		// Temporary, until it causes problems...
		fileNameNoExt := strings.ReplaceAll(file.Name(), ".gmi", "")

		var postTitle string
		switch showDate {
		case dateShow:
			postTitle = fmt.Sprintf("%s %s", fileNameNoExt[0:10], strings.Title(fileNameNoExt[11:]))

		case dateNoShow:
			postTitle = strings.Title(fileNameNoExt[11:])

		case noDateNoShow:
			postTitle = fileNameNoExt
			fileNameNoExt = strings.ToLower(fileNameNoExt)
		}

		files += fmt.Sprintf("=> %s %s\n", baseRoute+"/"+fileNameNoExt, postTitle)
		createRoute(baseRoute+"/"+fileNameNoExt, "default.gmi", "pages"+baseRoute+"/"+file.Name())
	}

	files = utilities.TrimLastChar(files)

	blogs[baseRoute] = name

	g.Handle(baseRoute, func(c gig.Context) error {
		return c.Render("default.gmi", IndexTemplate{
			Content:   files,
			Quote:     utilities.GetRandomQuote(),
			Hits:      database.Get(baseRoute),
			Copyright: utilities.GetCopyright(),
		})
	})
	legacySupport(baseRoute)
}

func legacySupport(baseRoute string) {
	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)
	})
	g.Handle(baseRoute+"/", func(c gig.Context) error {
		return c.NoContent(gig.StatusRedirectPermanent, baseRoute)
	})
}

func createRedirectRoute(route string, redirectRoute string, mask bool) {
	g.Handle(route, func(c gig.Context) error {
		if mask {
			return c.NoContent(gig.StatusRedirectPermanent, redirectRoute+c.URL().Path[2:]) // - /x
		} else {
			return c.NoContent(gig.StatusRedirectPermanent, redirectRoute)
		}
	})
}