From 8106c22dbdb96c9cc5e9c588950e1d7baa051663 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 24 May 2018 16:18:28 +0200 Subject: [PATCH] implemented method check in 'config' package --- internal/config/public.go | 35 +++++++++++++++++++++++++++++++++++ internal/config/types.go | 2 ++ 2 files changed, 37 insertions(+) diff --git a/internal/config/public.go b/internal/config/public.go index dc4ba37..add429f 100644 --- a/internal/config/public.go +++ b/internal/config/public.go @@ -3,6 +3,7 @@ package config import ( "encoding/json" "os" + "strings" ) // Load builds a structured representation of the @@ -38,3 +39,37 @@ func Load(path string) (*Controller, error) { return receiver, nil } + +// IsMethodAvailable returns whether a given +// method is available (case insensitive) +func IsMethodAvailable(method string) bool { + for _, m := range AvailableMethods { + if strings.ToUpper(method) == m { + return true + } + } + + return false +} + +// HasMethod returns whether the controller has a given +// method (case insensitive) +func (c Controller) HasMethod(method string) bool { + method = strings.ToUpper(method) + + switch method { + + case "GET": + return c.GET != nil + case "POST": + return c.POST != nil + case "PUT": + return c.PUT != nil + case "DELETE": + return c.DELETE != nil + default: + return false + + } + +} diff --git a/internal/config/types.go b/internal/config/types.go index 21fef94..524d477 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -25,3 +25,5 @@ type Controller struct { Children map[string]*Controller `json:"/"` } + +var AvailableMethods = []string{"GET", "POST", "PUT", "DELETE"}