aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 80c83d8d8862624e669021af4a96d58162712647 (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
package main

import (
	"embed"
	"encoding/json"
	"flag"
	"github.com/BurntSushi/toml"
	"github.com/anhgelus/golatt"
	"log/slog"
	"net/http"
	"os"
	"strings"
)

var (
	//go:embed templates
	templates embed.FS
	//go:embed dist
	assets embed.FS
)

var (
	domain     string
	configPath string
	dev        bool
)

func init() {
	flag.StringVar(&domain, "domain", "", "domain to use")
	flag.StringVar(&configPath, "config", "", "config to use")
	flag.BoolVar(&dev, "dev", false, "dev mode enabled")
}

func main() {
	flag.Parse()
	if domain == "" {
		domain = os.Getenv("NOW_DOMAIN")
		if domain == "" {
			slog.Error("Domain not set. Set it with --domain value or with the env NOW_DOMAIN")
			return
		}
	}
	if configPath == "" {
		configPath = os.Getenv("NOW_DATA")
		if configPath == "" {
			slog.Error("Config not set. Set it with --config relative path or with the env NOW_DATA")
			return
		}
	}
	b, err := os.ReadFile(configPath)
	if err != nil {
		panic(err)
	}

	var cfg Config
	if strings.HasSuffix(configPath, ".json") {
		err = json.Unmarshal(b, &cfg)
	} else if strings.HasSuffix(configPath, ".toml") {
		err = toml.Unmarshal(b, &cfg)
	} else {
		panic("config file must be .json or .toml")
	}
	if err != nil {
		panic(err)
	}
	customPages, err := cfg.LoadCustomPages()
	if err != nil {
		panic(err)
	}

	var g *golatt.Golatt
	if dev {
		g = golatt.New(golatt.UsableEmbedFS("templates", templates), os.DirFS("public"), os.DirFS("dist"))
	} else {
		g = golatt.New(
			golatt.UsableEmbedFS("templates", templates),
			os.DirFS("public"),
			golatt.UsableEmbedFS("dist", assets),
		)
	}
	g.DefaultSeoData = &golatt.SeoData{
		Image:       cfg.Image,
		Description: cfg.Description,
		Domain:      domain,
	}
	g.Templates = append(g.Templates, "base/*.gohtml")

	g.NewTemplate("index", "/", cfg.Person.Name, "", "", &cfg).Handle()
	g.NewTemplate("legal",
		"/legal",
		"Legal things",
		"",
		"Legal information about "+cfg.Person.Name+"'s Now page",
		&cfg).
		Handle()
	g.NewTemplate("tags",
		"/tags",
		"Tags",
		"",
		"Tags of "+cfg.Person.Name+"'s Now page",
		&cfg).
		Handle()

	for _, cp := range customPages {
		slog.Info("Creating custom page...", "title", cp.Title, "uri", cp.URI)
		g.NewTemplate("custom_page",
			cp.URI,
			cp.Title,
			cp.Image,
			cp.Description,
			cp).
			Handle()
	}

	g.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
	}

	if dev {
		slog.Info("Starting on http://localhost:8000/")
		g.StartServer(":8000")
	} else {
		g.StartServer(":80")
	}
}