From 2e4fc5dea8770864be5ff250cedcb31b9d9874b3 Mon Sep 17 00:00:00 2001 From: ali Date: Wed, 12 Apr 2023 03:34:28 +0300 Subject: [PATCH] add shutdown endpoint then add server_test enpoint --- dispatcher/server.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/dispatcher/server.go b/dispatcher/server.go index feba851..bfd6515 100644 --- a/dispatcher/server.go +++ b/dispatcher/server.go @@ -5,6 +5,7 @@ import ( "github.com/fatih/color" "net" "net/http" + "os" ) func HandleCamp(writer http.ResponseWriter, request *http.Request, d *Dispatcher) { @@ -30,7 +31,6 @@ func HandleCamp(writer http.ResponseWriter, request *http.Request, d *Dispatcher return } - // request.RemoteAddr == "" that means its testing, let change it if request.RemoteAddr == "" { request.RemoteAddr = "127.0.0.1:8081" } @@ -111,6 +111,19 @@ func HandleCamp(writer http.ResponseWriter, request *http.Request, d *Dispatcher } } +func HandleSystem(writer http.ResponseWriter, request *http.Request, d *Dispatcher) { + //SHUTDOWN SERVER + if request.Method == "DELETE" { + auth := request.Header.Get("Authorization") + if isAuthorized(auth, d.Cmp.Leader.AuthenticationHash) { + writer.WriteHeader(http.StatusOK) + os.Exit(0) + } else { + http.Error(writer, "Unauthorized", http.StatusUnauthorized) + } + } +} + func isAuthorized(auth string, hash string) bool { if HashOf(auth) == hash { return true @@ -122,6 +135,9 @@ func Start(d *Dispatcher) { http.HandleFunc("/camp", func(writer http.ResponseWriter, request *http.Request) { HandleCamp(writer, request, d) }) + http.HandleFunc("/system", func(writer http.ResponseWriter, request *http.Request) { + HandleSystem(writer, request, d) + }) color.Green("Starting dispatcher server on %s:%s", d.ListeningAddress, d.ListeningPort) err := http.ListenAndServe(d.ListeningAddress+":"+d.ListeningPort, nil) if err != nil {