Refactor api.go and systemd.go based on PR comments

This commit is contained in:
Praneeth Bodduluri 2015-10-06 20:21:04 +05:30 committed by Pablo Carranza Vélez
parent 8fe516253a
commit 1890c0bc0d
2 changed files with 17 additions and 17 deletions

View File

@ -18,7 +18,7 @@ import (
// Use raw strings to avoid having to quote the backslashes. // Use raw strings to avoid having to quote the backslashes.
var dockerMatch = regexp.MustCompile(`(docker[0-9]+)|(rce[0-9]+)`) 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 { type APIResponse struct {
Data interface{} Data interface{}
Error string Error string
@ -181,29 +181,28 @@ func IPAddressHandler(writer http.ResponseWriter, request *http.Request) {
//VPNControl is used to control VPN service status with dbus //VPNControl is used to control VPN service status with dbus
func VPNControl(writer http.ResponseWriter, request *http.Request) { func VPNControl(writer http.ResponseWriter, request *http.Request) {
sendResponse := responseSender(writer)
sendError := func(err string) {
sendResponse("Error", err, http.StatusInternalServerError)
}
var body VPNBody var body VPNBody
if err := parseJsonBody(&body, request); err != nil { if err := parseJsonBody(&body, request); err != nil {
log.Println(string(err.Error())) log.Println(err.Error())
sendError(string(err.Error())) responseSender(writer)("Error", err.Error(), http.StatusBadRequest)
return
} }
if body.Enable { if body.Enable {
_, err := systemd.Dbus.StartUnit("openvpn-resin.service", "fail", nil) _, err := systemd.Dbus.StartUnit("openvpn-resin.service", "fail", nil)
if err != nil { if err != nil {
log.Println(string(err.Error())) log.Println(err.Error())
sendError(string(err.Error())) responseSender(writer)("Error", err.Error(), http.StatusInternalServerError)
return
} }
log.Println("VPN Enabled") log.Println("VPN Enabled")
} else { } else {
_, err := systemd.Dbus.StopUnit("openvpn-resin.service", "fail", nil) _, err := systemd.Dbus.StopUnit("openvpn-resin.service", "fail", nil)
if err != nil { if err != nil {
log.Println(string(err.Error())) log.Println(err.Error())
sendError(string(err.Error())) responseSender(writer)("Error", err.Error(), http.StatusInternalServerError)
return
} }
log.Println("VPN Disabled") log.Println("VPN Disabled")
} }
sendResponse("OK", "", http.StatusAccepted) responseSender(writer)("OK", "", http.StatusAccepted)
} }

View File

@ -7,11 +7,12 @@ import (
"resin-supervisor/gosuper/Godeps/_workspace/src/github.com/coreos/go-systemd/login1" "resin-supervisor/gosuper/Godeps/_workspace/src/github.com/coreos/go-systemd/login1"
) )
// Logind Systemd Login1 connection var (
var Logind *login1.Conn // Logind Systemd Login1 connection
Logind *login1.Conn
// Dbus Systems Dbus connection // Dbus Systems Dbus connection
var Dbus *dbus.Conn Dbus *dbus.Conn
)
func init() { func init() {
var err error var err error