From 46c220d4d765b80260c9cfa75cc6886d5f1c7771 Mon Sep 17 00:00:00 2001
From: gjahn <gregor.jahn@bht-berlin.de>
Date: Wed, 20 Dec 2023 00:38:16 +0100
Subject: [PATCH] Add OPTIONS method for state resources & just respond 404 for
 generally undefined state paths

---
 routing/routes.go | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/routing/routes.go b/routing/routes.go
index e69f7d5..3611c33 100644
--- a/routing/routes.go
+++ b/routing/routes.go
@@ -114,6 +114,22 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
     statePathGroup := router.Group( "/state" )
 
 
+    statePathGroup.Options( "/:name", func( c *f.Ctx ) error {
+        name := strings.Clone( c.Params( "name" ) )
+        existingItem, err := store.Fetch( name )
+        if err != nil {
+            return c.SendStatus( http.StatusInternalServerError )
+        }
+
+        if existingItem == nil {
+            return c.SendStatus( http.StatusNotFound )
+        }
+
+        c.Set( "Allow", "OPTIONS, GET, PUT, DELETE, HEAD" )
+        return c.SendStatus( http.StatusNoContent )
+    })
+
+
     statePathGroup.Get( "/:name", func( c *f.Ctx ) error {
         existingItem, err := store.Fetch( c.Params( "name" ) )
         if err != nil {
@@ -212,11 +228,6 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
 
 
     statePathGroup.Use( "*", func( c *f.Ctx ) error {
-        if method := c.Method(); method == "OPTIONS" {
-            c.Set( "Allow", "GET, PUT, DELETE, OPTIONS" )
-            return c.SendStatus( http.StatusNoContent )
-        }
-
         return c.SendStatus( http.StatusNotFound )
     })
 
-- 
GitLab