-
Notifications
You must be signed in to change notification settings - Fork 20
/
monet.go
356 lines (292 loc) · 8.24 KB
/
monet.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"embed"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"net/http"
"os"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/jmoiron/monet/admin"
"github.com/jmoiron/monet/app"
"github.com/jmoiron/monet/auth"
"github.com/jmoiron/monet/blog"
"github.com/jmoiron/monet/conf"
"github.com/jmoiron/monet/db"
"github.com/jmoiron/monet/db/monarch"
"github.com/jmoiron/monet/mtr"
"github.com/jmoiron/monet/pages"
"github.com/jmoiron/monet/pkg/hotswap"
"github.com/jmoiron/monet/pkg/passwd"
"github.com/jmoiron/monet/stream"
"github.com/jmoiron/sqlx"
"github.com/spf13/pflag"
"github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3"
)
const (
cfgEnvVar = "MONET_CONFIG_PATH"
monetVersion = "0.0.1-std"
staticPath = "static/"
)
type options struct {
ConfigPath string
Debug bool
Version bool
AddUser string
LoadPosts string
LoadEvents string
LoadPages string
ShowMigration bool
Downgrade string
}
var logLevel = new(slog.LevelVar)
//go:embed static/*
var static embed.FS
//go:embed templates
var templates embed.FS
func must(err error, msg string, args ...any) {
if err != nil {
args = append(args, "err", err)
slog.Error(msg, args...)
os.Exit(-1)
}
}
func die[T any](v T, err error) func(string, ...any) T {
return func(msg string, args ...any) T {
if err != nil {
args = append(args, "err", err)
slog.Error(msg, args...)
os.Exit(-1)
}
return v
}
}
func main() {
slog.SetDefault(slog.New(
slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}),
))
var opts options
parseOpts(&opts)
if opts.Version {
v, _, t := sqlite3.Version()
fmt.Printf("Monet v%s\n", monetVersion)
fmt.Printf("Built w/ Sqlite3 version %v (%s)\n", v, strings.Split(t, " ")[0])
return
}
static := die(fs.Sub(static, "static"))("initializing static fs")
staticAlt := os.DirFS("static")
config := die(loadConfig(opts.ConfigPath))("loading config")
if config.Debug {
slog.Info("debug enabled")
logLevel.Set(slog.LevelDebug)
}
dbh := die(sqlx.Connect("sqlite3", config.DatabaseURI))("uri", config.DatabaseURI)
if opts.ShowMigration {
showMigration(dbh)
return
}
if len(opts.Downgrade) > 0 {
downgradeApp(dbh, opts.Downgrade)
return
}
// the authApp is sort of special; we want its session middleware to be at the top
// of our stack, so we want to keep a handle on it
var (
authApp = auth.NewApp(config, dbh)
adminApp = admin.NewApp(dbh, authApp.Sessions).WithBaseURL("/admin/")
blogApp = blog.NewApp(dbh).WithBaseURL("/blog/")
streamApp = stream.NewApp(dbh).WithBaseURL("/stream/")
pagesApp = pages.NewApp(dbh)
)
// pages should be last as it binds to /*
apps := []app.App{authApp, adminApp, blogApp, streamApp, pagesApp}
reg := mtr.NewRegistry()
reg.AddBaseFS("base", "templates/base.html", templates)
reg.AddPathFS("templates/index.html", templates)
for _, app := range apps {
must(app.Migrate(), "could not migrate app", "name", app.Name())
app.Register(reg)
}
adminApp.Collect(apps...)
if runUtil(&opts, dbh) {
return
}
must(reg.Build(), "could not build templates")
// set up the router
r := chi.NewRouter()
// add sessions, config, & db
r.Use(authApp.Sessions.AddSessionMiddleware)
r.Use(conf.Default().AddConfigMiddleware)
r.Use(db.AddDbMiddleware(dbh))
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(mtr.AddRegistryMiddleware(reg))
for _, app := range apps {
app.Bind(r)
}
r.Get("/", index)
/*
if config.Debug && false {
fs.WalkDir(static, ".", func(path string, d fs.DirEntry, err error) error {
slog.Debug("static file", "path", path)
return nil
})
}
*/
swp := hotswap.NewSwapper(static)
swp.Add(staticAlt)
if config.Debug {
swp.Swap()
}
reg.DefaultCtx["debug"] = config.Debug
stack := []func(http.Handler) http.Handler{
middleware.Compress(5),
// add a modest cache expiry for static files
middleware.SetHeader("Cache-Control", "max-age=120"),
}
if config.Debug {
// do not cache when in debug mode
stack[1] = middleware.NoCache
}
r.With(stack...).Handle("/favicon.ico", http.FileServer(http.FS(swp)))
r.With(stack...).Handle("/static/*", http.FileServer(http.FS(swp)))
slog.Info("Running with config", "config", config.String())
slog.Info("Listening on", "addr", config.ListenAddr)
if err := http.ListenAndServe(config.ListenAddr, r); err != nil {
slog.Error("error listening", "err", err)
}
}
func index(w http.ResponseWriter, r *http.Request) {
reg := mtr.RegistryFromContext(r.Context())
db := db.DbFromContext(r.Context())
postalService := blog.NewPostService(db)
streamService := stream.NewEventService(db)
posts, err := postalService.Select("WHERE published > 0 ORDER BY published_at DESC LIMIT 6")
if err != nil {
app.Http500("loading posts", w, err)
return
}
events, err := streamService.Select("ORDER BY timestamp DESC LIMIT 5")
if err != nil {
app.Http500("loading events", w, err)
return
}
err = reg.RenderWithBase(w, "base", "templates/index.html", mtr.Ctx{
"title": "jmoiron plays the blues",
"post": posts[0],
"posts": posts[1:],
"events": events,
})
if err != nil {
slog.Error("rendering index", "err", err)
}
}
func addUser(dbh db.DB, username string) error {
p1, err := passwd.GetPassword(fmt.Sprintf("enter password for user \"%s\"", username))
if err != nil {
return err
}
p2, err := passwd.GetPassword("repeat password:")
if err != nil {
return err
}
if p1 != p2 {
return errors.New("Error: passwords do not match")
}
return auth.NewUserService(dbh).CreateUser(username, p1)
}
func showMigration(db db.DB) {
m, err := monarch.NewManager(db)
if err != nil {
slog.Error("initializing monarch manager", "err", err)
return
}
latest, err := m.LatestVersions()
if err != nil {
slog.Error("Error fetching versions", "err", err)
return
}
for _, v := range latest {
fmt.Printf("app=%s version=%d applied-at=%s\n", v.Name, v.Version, v.AppliedAt)
}
}
func downgradeApp(db db.DB, app string) {
m, err := monarch.NewManager(db)
if err != nil {
slog.Error("initializing monarch manager", "err", err)
return
}
if err := m.Downgrade(app); err != nil {
slog.Error("error downgrading app", "app", app, "err", err)
}
}
type loader interface {
Load(io.Reader) error
}
func loadPath(l loader, path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return l.Load(f)
}
func loadPages(dbh db.DB, path string) error {
return loadPath(pages.NewLoader(dbh), path)
}
func loadEvents(dbh db.DB, path string) error {
return loadPath(stream.NewLoader(dbh), path)
}
func loadPosts(dbh db.DB, path string) error {
return loadPath(blog.NewLoader(dbh), path)
}
func loadConfig(path string) (*conf.Config, error) {
cfg := conf.Default()
if len(path) > 0 {
err := cfg.FromPath(path)
return cfg, err
}
return cfg, nil
}
func parseOpts(opts *options) {
pflag.StringVarP(&opts.ConfigPath, "config", "c", os.Getenv(cfgEnvVar), "path to a json config file")
pflag.BoolVarP(&opts.Debug, "debug", "d", false, "enable debug mode")
pflag.BoolVarP(&opts.Version, "version", "v", false, "show version info")
pflag.StringVar(&opts.AddUser, "add-user", "", "add a user (will be prompted for pw)")
pflag.StringVar(&opts.LoadPosts, "load-posts", "", "load posts from json")
pflag.StringVar(&opts.LoadEvents, "load-events", "", "load events from json")
pflag.StringVar(&opts.LoadPages, "load-pages", "", "load pages from json")
pflag.BoolVar(&opts.ShowMigration, "migrations", false, "show migration state for each application")
pflag.StringVar(&opts.Downgrade, "downgrade", "", "downgrade an app by one migration version")
pflag.Parse()
}
func runUtil(opts *options, db db.DB) bool {
switch {
case len(opts.AddUser) > 0:
if err := addUser(db, opts.AddUser); err != nil {
fmt.Printf("Error: %s\n", err)
}
case len(opts.LoadPosts) > 0:
if err := loadPosts(db, opts.LoadPosts); err != nil {
fmt.Printf("Error: %s\n", err)
}
case len(opts.LoadEvents) > 0:
if err := loadEvents(db, opts.LoadEvents); err != nil {
fmt.Printf("Error: %s\n", err)
}
case len(opts.LoadPages) > 0:
if err := loadPages(db, opts.LoadPages); err != nil {
fmt.Printf("ERror: %s\n", err)
}
default:
return false
}
return true
}