Merge pull request #29 from george-e-shaw-iv/master

Commenting, removing literals, and API reworking
This commit is contained in:
George Shaw 2017-10-25 14:54:27 -05:00 committed by GitHub
commit c6fa7d4352
4 changed files with 27 additions and 11 deletions

View file

@ -51,7 +51,7 @@
xhr.send(JSON.stringify(formData));
xhr.onloadend = function() {
console.log(xhr.response);
console.log(xhr.status);
}
}
</script>

View file

@ -6,25 +6,33 @@ import (
"net/http"
)
// auth struct is the structure of the JSON data to be retrieved from
// the authentication API request
var auth struct {
User string `json:"user"`
Pass string `json:"pass"`
}
// Authentication function is accessed by an API call from the webhost root
// by accessing /authentication and sending it a post request with
func Authentication(res http.ResponseWriter, req *http.Request) bool {
if req.Method != "POST" {
http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed)
return false
}
err := json.NewDecoder(req.Body).Decode(&auth)
if err != nil {
http.Error(res, err.Error(), 400)
http.Error(res, err.Error(), http.StatusBadRequest)
return false
} else {
if auth.User == "root" && auth.Pass == "root" {
res.WriteHeader(200)
res.Write([]byte("success"))
res.WriteHeader(http.StatusNoContent)
return true
} else {
http.Error(res, "Authentication failed", 401)
http.Error(res, "Authentication failed", http.StatusUnauthorized)
return false
}

View file

@ -21,9 +21,15 @@ func NewPublicWeb() PublicWeb {
}
}
// ServeHTTP function routes all requests for the public web server. It is used in the main
// function inside of the http.ListenAndServe() function for the public host.
func (pub *PublicWeb) ServeHTTP(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path[1:]
path = (pub.Directory + path)
if len(path) == 0 {
path = (pub.Directory + "index.html")
} else {
path = (pub.Directory + path)
}
f, err := os.Open(path)
@ -37,12 +43,12 @@ func (pub *PublicWeb) ServeHTTP(w http.ResponseWriter, req *http.Request) {
logging.Console(logging.PUBLIC_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 200 success.")
} else {
routing.HttpThrowStatus(404, w)
routing.HttpThrowStatus(http.StatusUnsupportedMediaType, w)
logging.Console(logging.PUBLIC_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" content type could not be determined, 404 error.")
}
} else {
routing.HttpThrowStatus(404, w)
routing.HttpThrowStatus(http.StatusNotFound, w)
logging.Console(logging.PUBLIC_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 404 error.")
}

View file

@ -24,6 +24,8 @@ func NewPrivateHost() PrivateHost {
}
}
// ServeHTTP function routes all requests for the private webhost server. It is used in the main
// function inside of the http.ListenAndServe() function for the private webhost host.
func (priv *PrivateHost) ServeHTTP(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path[1:]
if len(path) == 0 {
@ -33,7 +35,7 @@ func (priv *PrivateHost) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
if priv.Auth != 1 {
routing.HttpThrowStatus(404, w)
routing.HttpThrowStatus(http.StatusUnauthorized, w)
logging.Console(logging.PRIVATE_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 401 error.")
} else {
isApi, _ := api.HandleAPI(path, w, req)
@ -51,12 +53,12 @@ func (priv *PrivateHost) ServeHTTP(w http.ResponseWriter, req *http.Request) {
logging.Console(logging.PRIVATE_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 200 success.")
} else {
routing.HttpThrowStatus(404, w)
routing.HttpThrowStatus(http.StatusUnsupportedMediaType, w)
logging.Console(logging.PRIVATE_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" content type could not be determined, 404 error.")
}
} else {
routing.HttpThrowStatus(404, w)
routing.HttpThrowStatus(http.StatusNotFound, w)
logging.Console(logging.PRIVATE_PREFIX, logging.NORMAL_LOG, "Path \""+path+"\" rendered a 404 error.")
}