From feb3973869e9e476e22be595b5022334d9eaa858 Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Tue, 30 May 2023 19:06:14 -0600 Subject: [PATCH] add webmanifest for public app, make timezone configurable to fix schedule hours comparison --- config.template.yaml | 1 + internal/server/admin.html | 1 - internal/server/index.html | 8 ++++- internal/server/server.go | 31 +++++++++++------ internal/server/static/manifest.webmanifest | 37 ++++++++++++++++++++- internal/user/user.go | 2 +- 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/config.template.yaml b/config.template.yaml index 7f6dbef..cebb954 100644 --- a/config.template.yaml +++ b/config.template.yaml @@ -1,4 +1,5 @@ name: Casa de alguien +timezone: America/Mexico_City adapter: kind: dry-run diff --git a/internal/server/admin.html b/internal/server/admin.html index ebedc3a..6892a36 100644 --- a/internal/server/admin.html +++ b/internal/server/admin.html @@ -12,7 +12,6 @@ - diff --git a/internal/server/index.html b/internal/server/index.html index 7d30757..cb3c8e9 100644 --- a/internal/server/index.html +++ b/internal/server/index.html @@ -9,7 +9,13 @@ - + + + + + + +
diff --git a/internal/server/server.go b/internal/server/server.go index 78cb24e..d0d004a 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -48,12 +48,12 @@ type HTTPConfig struct { } type Config struct { - Name string `yaml:"name"` - Adapter map[string]any `yaml:"adapter"` - HTTP *HTTPConfig `yaml:"http"` - WebPush *push.Config `yaml:"push"` - - DB string `yaml:"db"` + Name string `yaml:"name"` + Adapter map[string]any `yaml:"adapter"` + HTTP *HTTPConfig `yaml:"http"` + WebPush *push.Config `yaml:"push"` + Timezone string `yaml:"timezone"` + DB string `yaml:"db"` } func ConfigDefaults(dbPath string) *Config { @@ -146,7 +146,7 @@ func notifyAdmins(message string) { for _, sub := range subs { err := push.Notify(message, sub) if err != nil { - logrus.Errorf("could not push notification to subscription %s: %s", sub.ID, err) + logrus.Errorf("could not push notification to subscription %s: %s", sub.ID(), err) } } } @@ -155,14 +155,14 @@ func rex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { var err error u := user.FromContext(r) - defer func() { - _, sqlErr := _db.Collection("log").Insert(newAuditLog(r, err)) + defer func(req *http.Request, err error) { + _, sqlErr := _db.Collection("log").Insert(newAuditLog(req, err)) if sqlErr != nil { logrus.Errorf("could not record error log: %s", sqlErr) } - }() + }(r, err) - err = u.IsAllowed(time.Now()) + err = u.IsAllowed(time.Now().In(TZ)) if err != nil { logrus.Errorf("Denying rex to %s: %s", u.Name, err) http.Error(w, "Access denied", http.StatusForbidden) @@ -182,6 +182,7 @@ func rex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { } var _db db.Session +var TZ *time.Location = time.UTC func Initialize(config *Config) (http.Handler, error) { devMode := os.Getenv("ENV") == "dev" @@ -190,6 +191,14 @@ func Initialize(config *Config) (http.Handler, error) { allowCORS(nil)(w, r, nil) }) + if config.Timezone != "" { + mtz, err := time.LoadLocation(config.Timezone) + if err != nil { + return nil, fmt.Errorf("Unknown timezone %s", config.Timezone) + } + TZ = mtz + } + db := sqlite.ConnectionURL{ Database: config.DB, Options: map[string]string{ diff --git a/internal/server/static/manifest.webmanifest b/internal/server/static/manifest.webmanifest index 45df982..0135dff 100644 --- a/internal/server/static/manifest.webmanifest +++ b/internal/server/static/manifest.webmanifest @@ -4,9 +4,44 @@ "display": "standalone", "icons": [ { - "src": "static/icon@192.png", + "src": "static/icon/72.png", + "sizes": "72x72", + "type": "image/png" + }, + { + "src": "static/icon/96.png", + "sizes": "96x96", + "type": "image/png" + }, + { + "src": "static/icon/128.png", + "sizes": "128x128", + "type": "image/png" + }, + { + "src": "static/icon/144.png", + "sizes": "144x144", + "type": "image/png" + }, + { + "src": "static/icon/152.png", + "sizes": "152x152", + "type": "image/png" + }, + { + "src": "static/icon/192.png", "sizes": "192x192", "type": "image/png" + }, + { + "src": "static/icon/384.png", + "sizes": "384x384", + "type": "image/png" + }, + { + "src": "static/icon/512.png", + "sizes": "512x512", + "type": "image/png" } ], "name": "Puerta Nidito", diff --git a/internal/user/user.go b/internal/user/user.go index 12e2393..0053a07 100644 --- a/internal/user/user.go +++ b/internal/user/user.go @@ -126,7 +126,7 @@ func (user *User) IsAllowed(t time.Time) error { return fmt.Errorf("usuario expirado, avĂ­sale a Roberto") } - if user.Schedule != nil && !user.Schedule.AllowedAt(time.Now()) { + if user.Schedule != nil && !user.Schedule.AllowedAt(t) { return fmt.Errorf("accesso denegado, intente nuevamente en otro momento") }