From 1890c0bc0d92189cceeb1b245bc69e7c191456da Mon Sep 17 00:00:00 2001 From: Praneeth Bodduluri Date: Tue, 6 Oct 2015 20:21:04 +0530 Subject: [PATCH] Refactor api.go and systemd.go based on PR comments --- gosuper/gosuper/api.go | 23 +++++++++++------------ gosuper/systemd/systemd.go | 11 ++++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/gosuper/gosuper/api.go b/gosuper/gosuper/api.go index 0e589995..c9370b55 100644 --- a/gosuper/gosuper/api.go +++ b/gosuper/gosuper/api.go @@ -18,7 +18,7 @@ import ( // Use raw strings to avoid having to quote the backslashes. var dockerMatch = regexp.MustCompile(`(docker[0-9]+)|(rce[0-9]+)`) -// APIResponse sent from gosupervisor +// APIResponse The api response sent from go supervisor type APIResponse struct { Data interface{} Error string @@ -181,29 +181,28 @@ func IPAddressHandler(writer http.ResponseWriter, request *http.Request) { //VPNControl is used to control VPN service status with dbus func VPNControl(writer http.ResponseWriter, request *http.Request) { - sendResponse := responseSender(writer) - sendError := func(err string) { - sendResponse("Error", err, http.StatusInternalServerError) - } var body VPNBody if err := parseJsonBody(&body, request); err != nil { - log.Println(string(err.Error())) - sendError(string(err.Error())) + log.Println(err.Error()) + responseSender(writer)("Error", err.Error(), http.StatusBadRequest) + return } if body.Enable { _, err := systemd.Dbus.StartUnit("openvpn-resin.service", "fail", nil) if err != nil { - log.Println(string(err.Error())) - sendError(string(err.Error())) + log.Println(err.Error()) + responseSender(writer)("Error", err.Error(), http.StatusInternalServerError) + return } log.Println("VPN Enabled") } else { _, err := systemd.Dbus.StopUnit("openvpn-resin.service", "fail", nil) if err != nil { - log.Println(string(err.Error())) - sendError(string(err.Error())) + log.Println(err.Error()) + responseSender(writer)("Error", err.Error(), http.StatusInternalServerError) + return } log.Println("VPN Disabled") } - sendResponse("OK", "", http.StatusAccepted) + responseSender(writer)("OK", "", http.StatusAccepted) } diff --git a/gosuper/systemd/systemd.go b/gosuper/systemd/systemd.go index b44ad8c5..3eb863f2 100644 --- a/gosuper/systemd/systemd.go +++ b/gosuper/systemd/systemd.go @@ -7,11 +7,12 @@ import ( "resin-supervisor/gosuper/Godeps/_workspace/src/github.com/coreos/go-systemd/login1" ) -// Logind Systemd Login1 connection -var Logind *login1.Conn - -// Dbus Systems Dbus connection -var Dbus *dbus.Conn +var ( + // Logind Systemd Login1 connection + Logind *login1.Conn + // Dbus Systems Dbus connection + Dbus *dbus.Conn +) func init() { var err error