From 9e91c4050aacf6385ed95bda203e1fdfaed3dc38 Mon Sep 17 00:00:00 2001 From: George Shaw Date: Sat, 25 Nov 2017 15:30:16 -0600 Subject: [PATCH] account management from server panel has been set up in general, APIs just need to be configured on the front end side. Status API is already set up to make sure it works. --- pkg/api/server/status.go | 2 +- pkg/gpserver/apihandler.go | 34 ++++++++++++++- pkg/gpserver/gpserver.go | 2 +- .../js/panelHandlers/bundles/specific/open.js | 10 ++++- .../bundles/specific/server/status.js | 41 +++++++++++++++++++ server/document_root/gPanel.html | 2 + 6 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 server/document_root/assets/js/panelHandlers/bundles/specific/server/status.js diff --git a/pkg/api/server/status.go b/pkg/api/server/status.go index 1212937..abb4d3a 100644 --- a/pkg/api/server/status.go +++ b/pkg/api/server/status.go @@ -11,7 +11,7 @@ import ( // Status function is called from api/server/status and will return the current status of // the public server. func Status(res http.ResponseWriter, req *http.Request, publicServer *public.Controller) bool { - if req.Method != "GET" { + if req.Method != "GET" && req.Method != "POST" { http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } diff --git a/pkg/gpserver/apihandler.go b/pkg/gpserver/apihandler.go index f9ebd16..367f7c2 100644 --- a/pkg/gpserver/apihandler.go +++ b/pkg/gpserver/apihandler.go @@ -2,15 +2,19 @@ package gpserver import ( + "bytes" + "encoding/json" + "io/ioutil" "net/http" "strings" "github.com/Ennovar/gPanel/pkg/api/bundle" "github.com/Ennovar/gPanel/pkg/api/log" + "github.com/Ennovar/gPanel/pkg/api/server" "github.com/Ennovar/gPanel/pkg/api/user" ) -func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request, curBundle int) (bool, bool) { +func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (bool, bool) { path := req.URL.Path[1:] if len(path) == 0 { path = (con.Directory + "index.html") @@ -21,6 +25,34 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request, cu splitUrl := strings.SplitN(path, "api", 2) suspectApi := strings.ToLower(splitUrl[len(splitUrl)-1]) + if req.ContentLength > 0 { + var bundleRequestData struct { + BName string `json:"bundle_name,omitempty"` + } + + buf, err := ioutil.ReadAll(req.Body) + if err != nil { + return false, false + } + + b1 := ioutil.NopCloser(bytes.NewBuffer(buf)) + req.Body = ioutil.NopCloser(bytes.NewBuffer(buf)) + + err = json.NewDecoder(b1).Decode(&bundleRequestData) + if err != nil { + return false, false + } + + if specific, ok := con.Bundles[bundleRequestData.BName]; ok { + switch suspectApi { + case "/server/status": + return true, server.Status(res, req, specific.Public) + default: + return false, false + } + } + } + switch suspectApi { case "/user/auth": return true, user.Auth(res, req, con.Directory) diff --git a/pkg/gpserver/gpserver.go b/pkg/gpserver/gpserver.go index 7cacfb5..9c2e20e 100644 --- a/pkg/gpserver/gpserver.go +++ b/pkg/gpserver/gpserver.go @@ -81,7 +81,7 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { } } - isApi, _ := con.apiHandler(res, req, 0) + isApi, _ := con.apiHandler(res, req) if isApi { // API methods handle HTTP logic from here diff --git a/server/document_root/assets/js/panelHandlers/bundles/specific/open.js b/server/document_root/assets/js/panelHandlers/bundles/specific/open.js index d197cea..46bf052 100644 --- a/server/document_root/assets/js/panelHandlers/bundles/specific/open.js +++ b/server/document_root/assets/js/panelHandlers/bundles/specific/open.js @@ -1,9 +1,15 @@ +var specificBundleModal = jQuery('.specific-bundle-modal'); + jQuery(document).on('click', '._js_specific-bundle', function(e){ e.preventDefault(); var name = jQuery(this).attr('data'); jQuery('.manage-bundles-modal').modal('hide'); - jQuery('.specific-bundle-modal').find('.modal-title').html("Bundle \"" + name + "\" Management"); - jQuery('.specific-bundle-modal').modal('show'); + specificBundleModal.find('.modal-title').html("Bundle \"" + name + "\" Management"); + specificBundleModal.attr('data', name); + + getPublicServerStatus(name); + + specificBundleModal.modal('show'); }); diff --git a/server/document_root/assets/js/panelHandlers/bundles/specific/server/status.js b/server/document_root/assets/js/panelHandlers/bundles/specific/server/status.js new file mode 100644 index 0000000..e039005 --- /dev/null +++ b/server/document_root/assets/js/panelHandlers/bundles/specific/server/status.js @@ -0,0 +1,41 @@ +var statusSpan = jQuery("._js_specific-bundle-public-status"); + +function getPublicServerStatus(name) { + var xhr = new XMLHttpRequest(); + + var requestData = {}; + requestData["bundle_name"] = name; + console.log(requestData); + + xhr.open('POST', 'api/server/status', true); + xhr.send(JSON.stringify(requestData)); + + xhr.onloadend = function() { + if(xhr.status == 200) { + statusSpan.attr('class', '_js_specific-bundle-public-status'); + + switch(parseInt(xhr.response)) { + case 0: + statusSpan.addClass('text-danger').html("OFF"); + break; + case 1: + statusSpan.addClass('text-success').html("ON"); + break; + case 2: + statusSpan.addClass('text-warning').html("MAINTENANCE"); + break; + case 3: + statusSpan.addClass('text-info').html("RESTARTING"); + break; + default: + console.log(xhr.response); + statusSpan.addClass('text-danger').html('ERROR'); + break; + } + } + else { + console.log(xhr.response); + statusSpan.attr('class', '_js_specific-bundle-public-status').addClass('text-danger').html('ERROR'); + } + } +} diff --git a/server/document_root/gPanel.html b/server/document_root/gPanel.html index d6a2973..49d88ff 100644 --- a/server/document_root/gPanel.html +++ b/server/document_root/gPanel.html @@ -229,6 +229,8 @@ + +