summaryrefslogtreecommitdiff
path: root/core/core.go
blob: 70f9cb7d4854b58f227b77893a2987239f423598 (plain)
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
// Copyright (C) 2021 nytpu
// SPDX-License-Identifier: AGPL-3.0-or-later
// For more license details, see LICENSE or <https://www.gnu.org/licenses/agpl-3.0.html>.

// This file incorporates code from makeworld's amfora:
// https://github.com/makeworld-the-better-one/amfora/blob/master/subscriptions/structs.go
// Original source is Copyright 2020 makeworld and is licensed under the
// terms of the GNU General Public License Version 3.0.
// https://www.gnu.org/licenses/gpl-3.0-standalone.html

// package core implements the structs and other necessary resources for storing
// and managing feeds and pages
package core

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/url"
	"os"
	"path/filepath"
	"sync"
)

// Global instance of FullData
var Data = FullData{
	FeedsMu: &sync.RWMutex{},
	PagesMu: &sync.RWMutex{},
	// Maps are created in init()
}

// have to do this instead of the automatically called init() because
// initialization stuff requires parsing of command line args first
func Init(dataPath string) error {
	f, err := os.Open(filepath.Join(dataPath, "comitium.json"))
	if err == nil {
		// File exists and could be opened
		fi, err := f.Stat()
		if err == nil && fi.Size() > 0 {
			// File is not empty
			jsonBytes, err := ioutil.ReadAll(f)
			f.Close()
			if err != nil {
				return fmt.Errorf("read comitium.json error: %w", err)
			}
			err = json.Unmarshal(jsonBytes, &Data)
			if err != nil {
				return fmt.Errorf("comitium.json is corrupted: %w", err)
			}
		}
		f.Close()
	} else if !os.IsNotExist(err) {
		// There's an error opening the file, but it's not bc is doesn't exist
		return fmt.Errorf("open comitium.json error: %w", err)
	}

	if Data.Feeds == nil {
		Data.Feeds = make(map[url.URL]*Feed)
	}
	if Data.Pages == nil {
		Data.Pages = make(map[url.URL]*Page)
	}
	return nil
}