Browse Source

add page to use API for creating new items

master
Craig Stewart 5 months ago
parent
commit
8de7ca07fe
2 changed files with 47 additions and 0 deletions
  1. +14
    -0
      location-finder.go
  2. +33
    -0
      managelocations.html

+ 14
- 0
location-finder.go View File

@@ -49,6 +49,18 @@ func getlocation(w http.ResponseWriter, r *http.Request){
}
}

func managelocations(w http.ResponseWriter, r *http.Request){
t, err := template.ParseFiles("managelocations.html")
if err != nil {
log.Print("template parsing error: ", err)
}
err = t.Execute(w, nil)
if err != nil {
log.Print("template executing error: ", err)
}
}


func api_location(w http.ResponseWriter, r *http.Request){
switch r.Method {
case "GET":
@@ -159,6 +171,7 @@ func api_createid(w http.ResponseWriter, r *http.Request){
w.Write([]byte(http.StatusText(http.StatusInternalServerError) + "\n"))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(output))
return
default:
@@ -235,6 +248,7 @@ func genrandomstring(length int) (string, error) {
}

func main() {
http.HandleFunc("/managelocations.html", managelocations)
http.HandleFunc("/setlocation.html", setlocation)
http.HandleFunc("/getlocation.html", getlocation)
http.HandleFunc("/api/createitem", api_createid)


+ 33
- 0
managelocations.html View File

@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>Location</title>
<script>
function createItem() {
var post = new XMLHttpRequest();
post.open("GET", "/api/createitem", true);
post.send();
post.onload = function () {
switch ( this.status ) {
case 200:
document.getElementById("shared").innerHTML = "New Location Item Created";
var details = JSON.parse(this.response);
document.getElementById("fetch").innerHTML = "Get location from " + window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + "/getlocation.html?id=" + details.id + "&key=" + details.key;
document.getElementById("send").innerHTML = "Send link " + window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + "/setlocation.html?id=" + details.id + "&key=" + details.key + " to person whose location you want";
window.open(window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + "/getlocation.html?id=" + details.id + "&key=" + details.key);
break;
default:
document.getElementById("shared").innerHTML = "There was a problem";
setTimeout(createItem, 5000);
}
};
}
</script>
</head>
<body>
<button onclick="createItem()">Create new location item</button>
<p id="shared"></p>
<p id="fetch"></p>
<p id="send"></p>
</body>
</html>

Loading…
Cancel
Save