aboutsummaryrefslogtreecommitdiff
path: root/cmd/server/app.go
blob: 9a00628032334498c2b28b15a85a2d75ac5de406 (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
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/gomodule/redigo/redis"
	"gopkg.in/yaml.v3"
	"riedstra.dev/mitch/go-website/page"
)

var FeedPrefixDefault = ".feeds"

type App struct {
	redisPool *redis.Pool
	RedisKey  string

	ReIndexPath       string
	StaticDirectory   string
	BaseTemplate      string
	TemplateDirectory string
	DocumentSplit     string
	Suffix            string

	// Related to user authentication
	auth *Auth

	// 	Related to the Atom feed
	Title       string
	Description string // aka, "subtitle"
	Author      Author
	SiteURL     string
	FeedId      string //nolint
	Updated     page.PageTime
	FeedPrefix  string
}

func newApp() *App {
	return &App{}
}

func loadConf(fn string) (*App, error) {
	fh, err := os.Open(fn)
	if err != nil {
		return nil, fmt.Errorf("loading config: %w", err)
	}

	dec := yaml.NewDecoder(fh)

	app := newApp()

	err = dec.Decode(app)
	if err != nil {
		return nil, fmt.Errorf("decoding yaml: %w", err)
	}

	if app.StaticDirectory == "" {
		app.StaticDirectory = "static"
	}

	if app.FeedPrefix == "" {
		app.FeedPrefix = FeedPrefixDefault
	}

	if app.BaseTemplate != "" {
		page.BaseTemplate = app.BaseTemplate
	}

	if app.TemplateDirectory != "" {
		page.TemplateDirectory = app.TemplateDirectory
	}

	if app.DocumentSplit != "" {
		page.DocumentSplit = app.DocumentSplit
	}

	if app.Suffix != "" {
		page.Suffix = app.Suffix
	}

	page.Global = map[string]interface{}{
		"App": app,
	}

	return app, nil
}

// ClearRedis is a little helper function that allows us to easily clear
// the redis cache at runtime.
func (a *App) ClearRedis() error {
	if a.redisPool == nil {
		return nil
	}

	client := a.redisPool.Get()
	defer client.Close()

	_, err := client.Do("DEL", a.RedisKey)
	if err != nil {
		log.Println("Encountered error clearing redis cache: ", err)
	}

	return fmt.Errorf("clearing redis: %w", err)
}