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 }