aboutsummaryrefslogtreecommitdiff
path: root/route.go
blob: 4d5aacc5c28346f1cef2e8d28af5948ff447e2d7 (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
// Copyright (C) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

package main

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

func createRoute(route string, template string, content string) {
	g.Handle(route, func(c gig.Context) error {
		return c.Render(template, IndexTemplate{
			Content:   GetContent(content),
			Quote:     utilities.GetRandomQuote(),
			Hits:      database.Get(route) + 1,
			Copyright: utilities.GetCopyright(),
		})
	})

	// 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)
	})
}

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

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