This commit is contained in:
Henrique Dias
2016-06-21 15:28:15 +01:00
parent 8879f22932
commit b43d8e2465
67 changed files with 977 additions and 3381 deletions

View File

@@ -0,0 +1,30 @@
package server
import (
"encoding/json"
"net/http"
)
type Response struct {
Code int
Err error
Content interface{}
}
// RespondJSON used to send JSON responses to the web server
func RespondJSON(w http.ResponseWriter, r *Response) (int, error) {
if r.Content == nil {
r.Content = map[string]string{}
}
msg, msgErr := json.Marshal(r.Content)
if msgErr != nil {
return 500, msgErr
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(r.Code)
w.Write(msg)
return 0, r.Err
}

View File

@@ -0,0 +1,26 @@
package server
import (
"net/http"
"strings"
)
// ParseURLComponents parses the components of an URL creating an array
func ParseURLComponents(r *http.Request) []string {
//The URL that the user queried.
path := r.URL.Path
path = strings.TrimSpace(path)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
if strings.HasPrefix(path, "/") {
path = path[1:]
}
//This cuts off the trailing forward slash.
if strings.HasSuffix(path, "/") {
cutOffLastCharLen := len(path) - 1
path = path[:cutOffLastCharLen]
}
//We need to isolate the individual components of the path.
components := strings.Split(path, "/")
return components
}