Bugfix: Adding handlers
Bugfix: In case of trying to add multiple handlers to one path, the system panics and stops. Now, the case was covered and an error message appears, but the systems still works.
This commit is contained in:
		
							parent
							
								
									e1465c2fd0
								
							
						
					
					
						commit
						326eb9ba7a
					
				@ -1,15 +1,36 @@
 | 
			
		||||
package Handlers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/SommerEngineering/Ocean/Log"
 | 
			
		||||
	LM "github.com/SommerEngineering/Ocean/Log/Meta"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Function to add a new public handler.
 | 
			
		||||
func AddPublicHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) {
 | 
			
		||||
	// In case of an error, catch the error:
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if err := recover(); err != nil {
 | 
			
		||||
			Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameSTATE, fmt.Sprintf("Was not able to add a public handler, because the path '%s' is already in use. %s", pattern, err))
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	// Add the handler:
 | 
			
		||||
	muxPublic.HandleFunc(pattern, handler)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to add a new private handler.
 | 
			
		||||
func AddAdminHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) {
 | 
			
		||||
	// In case of an error, catch the error:
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if err := recover(); err != nil {
 | 
			
		||||
			Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameSTATE, fmt.Sprintf("Was not able to add a private admin handler, because the path '%s' is already in use. %s", pattern, err))
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	// Add the handler:
 | 
			
		||||
	muxAdmin.HandleFunc(pattern, handler)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user