diff --git a/api/api.go b/api/api.go index 1f7b7a5..b817a01 100644 --- a/api/api.go +++ b/api/api.go @@ -32,17 +32,35 @@ func New(store *db.Store, password string, notify Notifier) *Server { } func (s *Server) Handler() http.Handler { - if s.password == "" { - return s.mux + var handler http.Handler = s.mux + + // Wrap with auth middleware if password is set + if s.password != "" { + authHandler := handler + handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + auth := r.Header.Get("Authorization") + if !strings.HasPrefix(auth, "Bearer ") || auth[7:] != s.password { + writeError(w, http.StatusUnauthorized, "invalid or missing authorization") + return + } + authHandler.ServeHTTP(w, r) + }) } - // Wrap with auth middleware + + // Wrap with CORS middleware (allow all origins) + corsHandler := handler return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - auth := r.Header.Get("Authorization") - if !strings.HasPrefix(auth, "Bearer ") || auth[7:] != s.password { - writeError(w, http.StatusUnauthorized, "invalid or missing authorization") + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") + + // Handle preflight requests + if r.Method == "OPTIONS" { + w.WriteHeader(http.StatusNoContent) return } - s.mux.ServeHTTP(w, r) + + corsHandler.ServeHTTP(w, r) }) } diff --git a/index.html b/index.html new file mode 100644 index 0000000..3bb6347 --- /dev/null +++ b/index.html @@ -0,0 +1,1004 @@ + + + + + + Woke - Alarm Clock + + + +
+ +
+
+
+
+ + +
+
+
Alarms
+ +
+
+
Loading alarms...
+
+
+
+ + + + + +
+
+
+
+
+
+ + +
+
+
+ + +
+
+
+ + + + + + + diff --git a/woke b/woke index 6c4299c..5fcb728 100755 Binary files a/woke and b/woke differ