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.
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)
}

View File

@ -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