diff options
| author | Jan Wolff <janw@mailbox.org> | 2025-09-12 09:34:01 +0200 |
|---|---|---|
| committer | Jan Wolff <janw@mailbox.org> | 2025-09-12 09:34:01 +0200 |
| commit | 283c31564a9d5dab4b8b71d7498886b0cd20a999 (patch) | |
| tree | bc15867a389f25b1855042b5af20efba96c68dbb /controller/app.go | |
initial commit
Diffstat (limited to 'controller/app.go')
| -rw-r--r-- | controller/app.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/controller/app.go b/controller/app.go new file mode 100644 index 0000000..f145ce2 --- /dev/null +++ b/controller/app.go @@ -0,0 +1,39 @@ +package controller + +import ( + "embed" + "html/template" + "net/http" + + "drop.janw.name/config" + "drop.janw.name/storage" +) + +//go:embed template/* +var templateFs embed.FS + +type App struct { + storage *storage.Storage + tmpl *template.Template + config config.Configuration +} + +func NewApp(configFilename string) *App { + return &App{ + storage: storage.NewStorage(), + tmpl: template.Must(template.ParseFS(templateFs, "template/*.html")), + config: config.Must(config.Open(configFilename)), + } +} + +func (app App) requireAuth(res http.ResponseWriter, req *http.Request) bool { + username, password, ok := req.BasicAuth() + if ok && username == app.config.Authentication.Username && password == app.config.Authentication.Password { + return true + } + + res.Header().Add("WWW-Authenticate", "Basic realm=\"Authentication Required\", charset=\"UTF-8\"") + res.WriteHeader(http.StatusUnauthorized) + + return false +} |
