aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/site/site.go
blob: 37dca5fb6eb7abd0ad0653a1fbcc5c836f787f51 (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
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
package site

import (
	"errors"
	"fmt"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"regexp"
	"time"

	"github.com/LMBishop/scrapbook/pkg/config"
)

const versionRegex = "[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}"
const timeFormat = "2006_01_02_15_04_05"

type Site struct {
	Name       string
	Path       string
	Handler    http.Handler
	SiteConfig *config.SiteConfig
}

func NewSite(name string, dir string, config *config.SiteConfig) *Site {
	var site Site
	site.Name = name
	site.Path = dir
	site.SiteConfig = config
	site.Handler = http.FileServer(siteFS{http.Dir(path.Join(dir, "default"))})
	return &site
}

func CreateNewSite(name string, baseDir string, host string) (*Site, error) {
	dir := path.Join(baseDir, name)
	_, err := os.Stat(dir)
	if err == nil {
		return nil, fmt.Errorf("site with name already exists: %s", name)
	}
	if !errors.Is(err, os.ErrNotExist) {
		return nil, fmt.Errorf("failed to check site uniqueness: %w", err)
	}

	cfg := &config.SiteConfig{
		Host: host,
	}
	site := NewSite(name, dir, cfg)

	err = os.Mkdir(dir, 0o755)
	if err != nil {
		return nil, fmt.Errorf("failed to create directory: %w", err)
	}

	err = config.WriteSiteConfig(path.Join(dir, "site.toml"), cfg)
	if err != nil {
		return nil, fmt.Errorf("failed to write site config: %w", err)
	}

	return site, nil
}

func (s *Site) GetCurrentPath() string {
	return path.Join(s.Path, "default")
}

func (s *Site) GetCurrentVersion() (string, error) {
	dir, err := filepath.EvalSymlinks(path.Join(s.Path, "default"))
	if err != nil {
		return "", err
	}

	return filepath.Base(dir), nil
}

func (s *Site) UpdateVersion(newVersion string) error {
	newVersionPath := path.Join(s.Path, newVersion)

	stat, err := os.Stat(newVersionPath)
	if err != nil {
		return err
	}

	if !stat.IsDir() {
		return fmt.Errorf("not a directory: %s", newVersionPath)
	}

	currentVersionPath := s.GetCurrentPath()

	os.Remove(currentVersionPath)
	return os.Symlink(newVersion, currentVersionPath)
}

func (s *Site) GetAllVersions() ([]string, error) {
	entries, err := os.ReadDir(s.Path)

	if err != nil {
		return nil, err
	}

	var versions []string

	for _, entry := range entries {
		if !entry.IsDir() {
			continue
		}

		match, err := regexp.MatchString(versionRegex, entry.Name())
		if err != nil {
			return nil, err
		}

		if match {
			versions = append(versions, entry.Name())
		}
	}

	return versions, err
}

func (s *Site) CreateNewVersion() (string, error) {
	t := time.Now()
	dirName := t.Format("2006_01_02_15_04_05")
	newVersionDir := path.Join(s.Path, dirName)

	err := os.MkdirAll(newVersionDir, os.FileMode(0o755))
	if err != nil {
		return "", err
	}

	return dirName, nil
}

func (s *Site) EvaluateSiteStatus() string {
	stat, err := os.Stat(s.GetCurrentPath())
	if err != nil || !stat.IsDir() {
		return "inactive"
	}

	return "live"
}

func (s *Site) EvaluateSiteStatusReason() string {
	stat, err := os.Stat(s.GetCurrentPath())
	if err != nil || !stat.IsDir() {
		return "This site is inacessible because no version is active"
	}

	return "This site is live"
}