Skip to content

Commit

Permalink
add shutdown endpoint then add server_test enpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ZORbit01 committed Apr 12, 2023
1 parent 64dd450 commit 2e4fc5d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion dispatcher/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/fatih/color"
"net"
"net/http"
"os"
)

func HandleCamp(writer http.ResponseWriter, request *http.Request, d *Dispatcher) {
Expand All @@ -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"
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 2e4fc5d

Please sign in to comment.